-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_test.go
294 lines (243 loc) · 5.66 KB
/
data_source_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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package validate
import (
"fmt"
"mime/multipart"
"net/url"
"reflect"
"testing"
"time"
"github.com/gookit/goutil/dump"
"github.com/gookit/goutil/testutil/assert"
)
func TestData(t *testing.T) {
is := assert.New(t)
// MapData
d := FromMap(M{
"age": 45,
})
nval, err := d.Set("name", "inhere")
is.Nil(err)
is.Equal("inhere", nval)
val, ok := d.Get("name")
is.True(ok)
is.Equal("inhere", val)
is.Nil(d.BindJSON(nil))
// mp := map[string]any{"age": "45"}
// d = FromMap(&mp)
// StructData
sd, err := FromStruct(&UserForm{Name: "abc"})
is.Nil(err)
val, ok = sd.Get("name")
is.True(ok)
is.Equal("abc", val)
// 上面的 &UserForm 必须使用地址,下面的set才能成功
nval, err = sd.Set("name", "def")
is.Nil(err)
is.Equal("def", nval)
val, ok = sd.Get("name")
is.True(ok)
is.Equal("def", val)
_, err = sd.Set("notExist", "val")
is.Error(err)
}
func TestFormData(t *testing.T) {
is := assert.New(t)
d := FromURLValues(url.Values{
"name": {"inhere"},
"age": {"30"},
"notify": {"true"},
"money": {"23.4"},
})
is.True(d.Has("notify"))
is.True(d.HasField("notify"))
is.False(d.Has("not-exist"))
is.False(d.HasFile("file"))
is.False(d.HasField("file"))
is.True(d.Bool("notify"))
is.False(d.Bool("not-exist"))
is.Equal(30, d.Int("age"))
is.Equal([]string{"30"}, d.Strings("age"))
is.Equal(int64(30), d.Int64("age"))
is.Equal(int64(0), d.Int64("not-exist"))
is.Equal(0, d.Int("not-exist"))
is.Equal(23.4, d.Float("money"))
is.Equal(float64(0), d.Float("not-exist"))
is.Equal("inhere", d.String("name"))
is.Equal("age=30&money=23.4&name=inhere¬ify=true", d.Encode())
val, exist, zero := d.TryGet("name")
is.True(exist)
is.False(zero)
is.Equal("inhere", val)
val, exist = d.Get("name")
is.True(exist)
is.Equal("inhere", val)
nval, err := d.Set("newKey", "strVal")
is.NoErr(err)
is.Equal("strVal", nval)
is.Equal("strVal", d.String("newKey"))
_, err = d.Set("newInt", 23)
is.NoErr(err)
is.Equal(23, d.Int("newInt"))
_, err = d.Set("invalid", []int{2})
is.Error(err)
is.Equal("set value failure for field: invalid", err.Error())
// form
d.Add("newKey1", "new val1")
is.NotEmpty(d.Src())
is.Equal("new val1", d.String("newKey1"))
d.Del("newKey1")
is.Equal("", d.String("newKey1"))
d.AddValues(url.Values{
"newKey2": {"v2"},
"newKey3": {"v3"},
})
is.Equal("v3", d.String("newKey3"))
// file
d.AddFile("file", &multipart.FileHeader{Filename: "test.txt"})
is.True(d.Has("file"))
is.True(d.HasFile("file"))
is.NotEmpty(d.GetFile("file"))
d.DelFile("file")
is.False(d.HasFile("file"))
}
func TestStructData_Create(t *testing.T) {
is := assert.New(t)
_, err := FromStruct(time.Now())
is.Error(err)
_, err = FromStruct("invalid")
is.Error(err)
u := &UserForm{
Name: "new name",
Status: 3,
UpdateAt: time.Now(),
protected: "text",
Extra: []ExtraInfo{
{"xxx", 2},
},
}
d, err := FromStruct(u)
is.Nil(err)
v := New(d, "test")
is.Equal("test", v.Scene())
// create validation
v = d.Create(fmt.Errorf("a error"))
is.False(v.Validate())
is.Equal("a error", v.Errors.One())
d.ValidateTag = ""
v = d.Validation()
is.True(v.Validate())
// get field value
str, ok := d.Get("Name")
is.True(ok)
is.Equal("new name", str)
str, ok = d.Get("Name.NotExist")
is.False(ok)
is.Nil(str)
iVal, ok := d.Get("Extra.0.Status1")
is.True(ok)
is.Equal(2, iVal)
// not exist
ret, ok := d.Get("NotExist")
is.False(ok)
is.Nil(ret)
ret, ok = d.Get("NotExist.SubKey")
is.False(ok)
is.Nil(ret)
ret, ok = d.Get("Extra.0.NotExist")
is.False(ok)
is.Nil(ret)
// set value
_, err = d.Set("protected", "new text")
is.Error(err)
nval, err := d.Set("Name", "inhere")
is.Nil(err)
is.Equal("inhere", nval)
str, ok = d.Get("Name")
is.True(ok)
is.Equal("inhere", str)
}
func TestStructData_Set(t *testing.T) {
is := assert.New(t)
u := &UserForm{
Name: "new name",
Status: 3,
UpdateAt: time.Now(),
protected: "text",
Extra: []ExtraInfo{
{"xxx", 2},
},
}
d, err := FromStruct(u)
is.Nil(err)
v := d.Validation()
is.True(v.Errors.Empty())
dump.P(d.fieldNames)
_, err = d.Set("Extra.0.Github", "new url")
is.NoErr(err)
val, ok := d.Get("Extra.0.Github")
is.True(ok)
is.Equal("new url", val)
}
func TestStructData_Get_ptrVal(t *testing.T) {
type Struct1 struct {
Name string `validate:"required"`
Age *int `json:"age" validate:"required"`
}
age := 0
st := &Struct1{
Name: "tom",
Age: &age,
}
assert.False(t, reflect.ValueOf(&age).IsZero())
d, err := FromStruct(st)
assert.NoError(t, err)
assert.NotEmpty(t, d.Src())
val, ok := d.Get("Age")
assert.True(t, ok)
assert.Equal(t, "*int", fmt.Sprintf("%T", val))
assert.Equal(t, 0, *val.(*int))
}
func TestValidatePrivateFieldsWhenTrue(t *testing.T) {
type foo struct {
Field1 int `validate:"required|min:5|max:20" message:"Field1 outside of range"`
}
type bar struct {
foo
Field2 int `validate:"required|int" message:"Field2 outside of range"`
}
fooInt := 4
barInt := 25
myFoo := foo{Field1: fooInt}
barz := &bar{
foo: myFoo,
Field2: barInt,
}
Config(func(opt *GlobalOption) {
opt.ValidatePrivateFields = true
})
v := Struct(barz)
v.Validate()
assert.Equal(t, v.hasError, true)
}
func TestValidatePrivateFieldsWhenFalse(t *testing.T) {
type foo struct {
Field1 int `validate:"required|min:5|max:20" message:"Field1 outside of range"`
}
type bar struct {
foo
Field2 int `validate:"required|int" message:"Field2 outside of range"`
}
fooInt := 4
barInt := 25
myFoo := foo{Field1: fooInt}
barz := &bar{
foo: myFoo,
Field2: barInt,
}
Config(func(opt *GlobalOption) {
opt.ValidatePrivateFields = false
})
v := Struct(barz)
v.Validate()
assert.Equal(t, v.hasError, false)
}