From 97946ddcbd265fe3ff1bc65f82cabdcbde5babb8 Mon Sep 17 00:00:00 2001 From: miya Date: Thu, 3 Jun 2021 09:51:40 -0500 Subject: [PATCH] add middle/validate test (#1002) --- middleware/validate/validate_test.go | 43 ++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 middleware/validate/validate_test.go diff --git a/middleware/validate/validate_test.go b/middleware/validate/validate_test.go new file mode 100644 index 000000000..f1597820a --- /dev/null +++ b/middleware/validate/validate_test.go @@ -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) + } + }) + } +}