-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathput_default_test.go
163 lines (143 loc) · 3.49 KB
/
put_default_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
package changeset
import (
"reflect"
"testing"
"github.com/go-rel/changeset/params"
"github.com/stretchr/testify/assert"
)
func TestPutDefaultEmptyChanges(t *testing.T) {
ch := &Changeset{
changes: make(map[string]interface{}),
values: map[string]interface{}{
"field1": 0,
"field2": 5,
},
types: map[string]reflect.Type{
"field1": reflect.TypeOf(0),
"field2": reflect.TypeOf(5),
},
}
PutDefault(ch, "field1", 10)
PutDefault(ch, "field2", 10)
assert.Nil(t, ch.Error())
assert.Nil(t, ch.Errors())
assert.Equal(t, 1, len(ch.Changes()))
assert.Equal(t, 10, ch.Changes()["field1"])
assert.Nil(t, ch.Changes()["field2"])
}
func TestPutDefaultConditions(t *testing.T) {
ch := &Changeset{
types: map[string]reflect.Type{
"field1": reflect.TypeOf(0),
"field2": reflect.TypeOf(0),
"field3": reflect.TypeOf(0),
"field4": reflect.TypeOf(0),
"field5": reflect.TypeOf(0),
"field6": reflect.TypeOf(0),
},
values: map[string]interface{}{
"field1": 0,
"field2": 1,
"field3": 0,
"field4": 1,
"field5": 0,
"field6": 1,
},
params: params.Map{
"field1": 0,
"field2": 0,
"field3": 1,
"field4": 1,
},
changes: map[string]interface{}{
"field2": 0,
"field3": 1,
},
}
// existing | input | changes = changes after put default
// 0 | 0 | nil = nil
// 1 | 0 | 0 = 0
// 0 | 1 | 1 = 1
// 1 | 1 | nil = nil
// 0 | nil | nil = 10
// 1 | nil | nil = nil
tts := []struct {
Field string
Result interface{}
}{
{"field1", nil},
{"field2", 0},
{"field3", 1},
{"field4", nil},
{"field5", 10},
{"field6", nil},
}
for _, tt := range tts {
t.Run(tt.Field, func(t *testing.T) {
PutDefault(ch, tt.Field, 10)
assert.Equal(t, tt.Result, ch.Changes()[tt.Field])
})
}
}
func TestPutDefaultExisting(t *testing.T) {
ch := &Changeset{
changes: map[string]interface{}{
"field2": "default_val",
},
values: map[string]interface{}{
"field2": "default_val",
},
types: map[string]reflect.Type{
"field2": reflect.TypeOf(""),
},
}
PutDefault(ch, "field2", "must not be changed")
assert.Nil(t, ch.Error())
assert.Nil(t, ch.Errors())
assert.Equal(t, 1, len(ch.Changes()))
assert.Equal(t, "default_val", ch.Changes()["field2"])
}
func TestPutDefaultNoValueWithInput(t *testing.T) {
ch := &Changeset{
changes: map[string]interface{}{
"field2": "input_val",
},
values: map[string]interface{}{},
types: map[string]reflect.Type{
"field2": reflect.TypeOf(""),
},
}
PutDefault(ch, "field2", "must not be changed")
assert.Nil(t, ch.Error())
assert.Nil(t, ch.Errors())
assert.Equal(t, 1, len(ch.Changes()))
assert.Equal(t, "input_val", ch.Changes()["field2"])
}
func TestPutDefaultNoValueNoChange(t *testing.T) {
ch := &Changeset{
changes: map[string]interface{}{},
values: map[string]interface{}{},
types: map[string]reflect.Type{
"field2": reflect.TypeOf(""),
},
}
PutDefault(ch, "field2", "must not be changed")
assert.Nil(t, ch.Error())
assert.Nil(t, ch.Errors())
assert.Equal(t, 1, len(ch.Changes()))
assert.Equal(t, "must not be changed", ch.Changes()["field2"])
}
func TestPutDefaultInvalid(t *testing.T) {
ch := &Changeset{
changes: make(map[string]interface{}),
values: map[string]interface{}{
"field3": 0,
},
types: map[string]reflect.Type{
"field3": reflect.TypeOf(0),
},
}
PutDefault(ch, "field3", "invalid value")
assert.NotNil(t, ch.Error())
assert.NotNil(t, ch.Errors())
}