-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat_test.go
132 lines (112 loc) · 3.21 KB
/
format_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package echo
import (
"net/http"
"reflect"
"testing"
"github.com/labstack/echo/v4"
. "github.com/smartystreets/goconvey/convey"
)
type test1 struct {
A int `json:"a_custom" validate:"gt=0" message:"must be greater than 0"`
B int `form:"b_custom" validate:"required" message:"is required"`
C int
D int `query:"someValue" validate:"required"`
E int `json:"-" form:"-" query:"-" param:"-" xml:"-" yaml:"-" validate:"required"`
F int `json:"-" param:"f_custom" xml:"-" yaml:"-" validate:"required"`
G int `xml:"g_custom" yaml:"-" validate:"required"`
H int `yaml:"h_custom" validate:"required"`
}
type testCase struct {
Name string
Struct interface{}
Error error
}
var testCases = []testCase{
{
Name: "validate errors for all fields",
Struct: test1{A: -1},
Error: echo.NewHTTPError(http.StatusBadRequest, "bad value of `someValue`, `e`, `f_custom`, `g_custom`, `h_custom`; `a_custom` must be greater than 0; `b_custom` is required"),
},
{
Name: "validate errors for A field",
Struct: test1{A: 0, B: 1, D: 1, E: 1, F: 1, G: 1, H: 1},
Error: echo.NewHTTPError(http.StatusBadRequest, "`a_custom` must be greater than 0"),
},
{
Name: "validate errors for B field",
Struct: test1{A: 1, D: 1, E: 1, F: 1, G: 1, H: 1},
Error: echo.NewHTTPError(http.StatusBadRequest, "`b_custom` is required"),
},
{
Name: "validate errors for D, E, F, G, H fields",
Struct: test1{A: 1, B: 1},
Error: echo.NewHTTPError(http.StatusBadRequest, "bad value of `someValue`, `e`, `f_custom`, `g_custom`, `h_custom`"),
},
{
Name: "validate errors must be empty",
Struct: test1{A: 1, B: 1, D: 1, E: 1, F: 1, G: 1, H: 1},
Error: nil,
},
}
func TestCheckErrors(t *testing.T) {
Convey("Prepare validator", t, func(c C) {
v := NewValidator()
c.So(v, ShouldNotBeNil)
c.So(func() {
AddTagParsers(func(reflect.StructTag) string {
return "-"
})
}, ShouldNotPanic)
c.Convey("check custom formatter", func(c C) {
test := testCases[0]
errValidate := v.Validate(test.Struct)
if test.Error == nil {
c.So(errValidate, ShouldBeNil)
} else {
c.So(errValidate, ShouldBeError)
}
ok, err := CheckErrors(ValidateParams{
Struct: test.Struct,
Errors: errValidate,
Formatter: func(fields []*FieldError) string {
for _, item := range fields {
c.So(item, ShouldNotBeNil)
c.So(item, ShouldBeError)
}
return defaultFormatter(fields)
},
})
if test.Error == nil {
c.So(ok, ShouldBeFalse)
c.So(err, ShouldBeNil)
} else {
c.So(ok, ShouldBeTrue)
c.So(err, ShouldNotBeNil)
c.So(err.Error(), ShouldEqual, test.Error.Error())
}
})
c.So(len(testCases) > 0, ShouldBeTrue)
for _, test := range testCases {
c.Convey(test.Name, func(c C) {
errValidate := v.Validate(test.Struct)
if test.Error == nil {
c.So(errValidate, ShouldBeNil)
} else {
c.So(errValidate, ShouldBeError)
}
ok, err := CheckErrors(ValidateParams{
Struct: test.Struct,
Errors: errValidate,
})
if test.Error == nil {
c.So(ok, ShouldBeFalse)
c.So(err, ShouldBeNil)
} else {
c.So(ok, ShouldBeTrue)
c.So(err, ShouldNotBeNil)
c.So(err.Error(), ShouldEqual, test.Error.Error())
}
})
}
})
}