/
mutation_test.go
166 lines (147 loc) · 3.76 KB
/
mutation_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
package rel
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
type Testentity struct {
Field1 string `db:",primary"`
Field2 bool
Field3 *string
Field4 int
Field5 int
}
func TestApply(t *testing.T) {
var (
entity = Testentity{}
doc = NewDocument(&entity)
mutators = []Mutator{
Set("field1", "string"),
Set("field2", true),
Set("field3", "string pointer"),
IncBy("field4", 2),
DecBy("field5", 2),
SetFragment("field6=?", true),
}
mutation = Mutation{
Cascade: true,
Mutates: map[string]Mutate{
"field1": Set("field1", "string"),
"field2": Set("field2", true),
"field3": Set("field3", "string pointer"),
"field4": IncBy("field4", 2),
"field5": DecBy("field5", 2),
"field6=?": SetFragment("field6=?", true),
},
Reload: true,
}
)
assert.Equal(t, mutation, Apply(doc, mutators...))
assert.Equal(t, "string", entity.Field1)
assert.Equal(t, true, entity.Field2)
assert.Equal(t, "string pointer", *entity.Field3)
// non set op won't update the struct
assert.Equal(t, 0, entity.Field4)
assert.Equal(t, 0, entity.Field5)
}
func TestApply_Options(t *testing.T) {
var (
entity = Testentity{}
doc = NewDocument(&entity)
mutators = []Mutator{
Unscoped(true),
Reload(true),
Cascade(true),
OnConflictIgnore(),
}
mutation = Mutation{
Unscoped: true,
Cascade: true,
Mutates: map[string]Mutate{
"field2": Set("field2", false),
"field3": Set("field3", nil),
"field4": Set("field4", 0),
"field5": Set("field5", 0),
},
Reload: true,
OnConflict: OnConflict{Keys: []string{"field1"}, Ignore: true},
}
)
assert.Equal(t, mutation, Apply(doc, mutators...))
}
func TestApplyMutation_setValueError(t *testing.T) {
var (
entity = Testentity{}
doc = NewDocument(&entity)
)
assert.Panics(t, func() {
Apply(doc, Set("field1", 1))
})
assert.Equal(t, "", entity.Field1)
}
func TestApplyMutation_incValueError(t *testing.T) {
var (
entity = Testentity{}
doc = NewDocument(&entity)
)
assert.Panics(t, func() {
Apply(doc, Inc("field1"))
})
assert.Equal(t, "", entity.Field1)
}
func TestApplyMutation_unknownFieldValueError(t *testing.T) {
var (
entity = Testentity{}
doc = NewDocument(&entity)
)
assert.Panics(t, func() {
Apply(doc, Dec("field0"))
})
assert.Equal(t, "", entity.Field1)
}
func TestApplyMutation_Reload(t *testing.T) {
var (
entity = Testentity{}
doc = NewDocument(&entity)
mutators = []Mutator{
Set("field1", "string"),
Reload(true),
}
mutation = Mutation{
Mutates: map[string]Mutate{
"field1": Set("field1", "string"),
},
Reload: true,
Cascade: true,
}
)
assert.Equal(t, mutation, Apply(doc, mutators...))
assert.Equal(t, "string", entity.Field1)
}
func TestApplyMutation_Cascade(t *testing.T) {
var (
entity = Testentity{}
doc = NewDocument(&entity)
mutators = []Mutator{
Set("field1", "string"),
Cascade(false),
}
mutation = Mutation{
Mutates: map[string]Mutate{
"field1": Set("field1", "string"),
},
Cascade: false,
}
)
assert.Equal(t, mutation, Apply(doc, mutators...))
assert.Equal(t, "string", entity.Field1)
}
func TestMutator_String(t *testing.T) {
assert.Equal(t, "rel.Set(\"field\", 1)", fmt.Sprint(Set("field", 1)))
assert.Equal(t, "rel.Set(\"field\", true)", fmt.Sprint(Set("field", true)))
assert.Equal(t, "rel.Set(\"field\", \"value\")", fmt.Sprint(Set("field", "value")))
assert.Equal(t, "rel.IncBy(\"count\", -1)", fmt.Sprint(Dec("count")))
assert.Equal(t, "rel.IncBy(\"count\", 1)", fmt.Sprint(Inc("count")))
assert.Equal(t, "rel.SetFragment(\"field = (?, ?, ?)\", 1, true, \"value\")", fmt.Sprint(SetFragment("field = (?, ?, ?)", 1, true, "value")))
assert.Equal(t, "rel.Cascade(true)", fmt.Sprint(Cascade(true)))
}