mirror of
https://github.com/go-kratos/kratos.git
synced 2025-03-17 21:07:54 +02:00
add middle/validate test (#1002)
This commit is contained in:
parent
48851a1ffd
commit
97946ddcbd
43
middleware/validate/validate_test.go
Normal file
43
middleware/validate/validate_test.go
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
package validate
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/go-kratos/kratos/v2/errors"
|
||||||
|
"github.com/go-kratos/kratos/v2/middleware"
|
||||||
|
)
|
||||||
|
|
||||||
|
// protoVali implement validate.validator
|
||||||
|
type protoVali struct {
|
||||||
|
name string
|
||||||
|
age int
|
||||||
|
isErr bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v protoVali) Validate() error {
|
||||||
|
if v.name == "" || v.age < 0 {
|
||||||
|
return fmt.Errorf("err")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTable(t *testing.T) {
|
||||||
|
var mock middleware.Handler = func(ctx context.Context, req interface{}) (interface{}, error) { return nil, nil }
|
||||||
|
|
||||||
|
tests := []protoVali{
|
||||||
|
{"v1", 365, false},
|
||||||
|
{"v2", -1, true},
|
||||||
|
{"", 365, true},
|
||||||
|
}
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
v := Validator()(mock)
|
||||||
|
_, err := v(context.Background(), test)
|
||||||
|
if want, have := test.isErr, errors.IsBadRequest(err); want != have {
|
||||||
|
t.Errorf("fail data %v, want %v, have %v", test, want, have)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user