-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patharray_test.go
125 lines (101 loc) · 3.09 KB
/
array_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
package vjson
import (
"encoding/json"
"github.com/stretchr/testify/assert"
"testing"
)
func TestArrayField_GetName(t *testing.T) {
field := Array("foo", Integer("bar"))
assert.Equal(t, "foo", field.GetName())
}
func TestArrayField_Validate(t *testing.T) {
t.Run("required", func(t *testing.T) {
t.Run("nil_value", func(t *testing.T) {
field := Array("foo", Integer("bar")).Required()
err := field.Validate(nil)
assert.NotNil(t, err)
})
t.Run("valid_value", func(t *testing.T) {
field := Array("foo", Integer("bar")).Required()
// input should be an array of interface
err := field.Validate([]interface{}{1, 2})
assert.Nil(t, err)
})
t.Run("invalid_value", func(t *testing.T) {
t.Run("int_array", func(t *testing.T) {
field := Array("foo", Integer("bar")).Required()
// input should be an array of interface
err := field.Validate([]int{1, 2})
assert.NotNil(t, err)
})
t.Run("str_array", func(t *testing.T) {
field := Array("foo", Integer("bar")).Required()
err := field.Validate([]interface{}{"1", "2"})
assert.NotNil(t, err)
})
})
})
t.Run("not_required", func(t *testing.T) {
t.Run("nil_value", func(t *testing.T) {
field := Array("foo", Integer("bar"))
err := field.Validate(nil)
assert.Nil(t, err)
})
t.Run("valid_value", func(t *testing.T) {
field := Array("foo", Integer("bar"))
err := field.Validate([]interface{}{1, 2})
assert.Nil(t, err)
})
t.Run("invalid_value", func(t *testing.T) {
t.Run("int_array", func(t *testing.T) {
field := Array("foo", Integer("bar"))
err := field.Validate([]int{1, 2})
assert.NotNil(t, err)
})
t.Run("str_array", func(t *testing.T) {
field := Array("foo", Integer("bar"))
err := field.Validate([]interface{}{"1", "2"})
assert.NotNil(t, err)
})
})
})
t.Run("min_length", func(t *testing.T) {
field := Array("foo", Integer("bar")).MinLength(3)
err := field.Validate([]interface{}{1, 2, 3})
assert.Nil(t, err)
err = field.Validate([]interface{}{1, 2, 3, 4})
assert.Nil(t, err)
err = field.Validate([]interface{}{1, 2})
assert.NotNil(t, err)
})
t.Run("max_length", func(t *testing.T) {
field := Array("foo", Integer("bar")).MaxLength(3)
err := field.Validate([]interface{}{1, 2, 3})
assert.Nil(t, err)
err = field.Validate([]interface{}{1, 2, 3, 4})
assert.NotNil(t, err)
err = field.Validate([]interface{}{1, 2})
assert.Nil(t, err)
})
}
func TestArrayField_MarshalJSON(t *testing.T) {
field := Array("foo", String("bar"))
b, err := json.Marshal(field)
assert.Nil(t, err)
data := map[string]interface{}{}
err = json.Unmarshal(b, &data)
assert.Nil(t, err)
assert.Equal(t, "foo", data["name"])
assert.Equal(t, string(arrayType), data["type"])
assert.Equal(t, "bar", data["items"].(map[string]interface{})["name"])
}
func TestNewArray(t *testing.T) {
field := NewArray(ArrayFieldSpec{
Name: "bar",
Required: true,
}, String("foo"), false, false)
assert.NotNil(t, field)
assert.Equal(t, "bar", field.name)
assert.Equal(t, false, field.minLengthValidation)
assert.Equal(t, false, field.maxLengthValidation)
}