forked from go-rel/rel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutation_test.go
128 lines (112 loc) · 2.56 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
package rel
import (
"testing"
"github.com/stretchr/testify/assert"
)
type TestRecord struct {
Field1 string
Field2 bool
Field3 *string
Field4 int
Field5 int
}
func TestApplyMutation(t *testing.T) {
var (
record = TestRecord{}
doc = NewDocument(&record)
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", record.Field1)
assert.Equal(t, true, record.Field2)
assert.Equal(t, "string pointer", *record.Field3)
// non set op won't update the struct
assert.Equal(t, 0, record.Field4)
assert.Equal(t, 0, record.Field5)
}
func TestApplyMutation_setValueError(t *testing.T) {
var (
record = TestRecord{}
doc = NewDocument(&record)
)
assert.Panics(t, func() {
Apply(doc, Set("field1", 1))
})
assert.Equal(t, "", record.Field1)
}
func TestApplyMutation_incValueError(t *testing.T) {
var (
record = TestRecord{}
doc = NewDocument(&record)
)
assert.Panics(t, func() {
Apply(doc, Inc("field1"))
})
assert.Equal(t, "", record.Field1)
}
func TestApplyMutation_unknownFieldValueError(t *testing.T) {
var (
record = TestRecord{}
doc = NewDocument(&record)
)
assert.Panics(t, func() {
Apply(doc, Dec("field0"))
})
assert.Equal(t, "", record.Field1)
}
func TestApplyMutation_Reload(t *testing.T) {
var (
record = TestRecord{}
doc = NewDocument(&record)
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", record.Field1)
}
func TestApplyMutation_Cascade(t *testing.T) {
var (
record = TestRecord{}
doc = NewDocument(&record)
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", record.Field1)
}