forked from go-rel/rel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_test.go
243 lines (222 loc) · 4.68 KB
/
map_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
package rel
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestMap(t *testing.T) {
var (
user User
doc = NewDocument(&user)
data = Map{
"name": "Luffy",
"age": 20,
"transactions": []Map{
{"item": "Sword"},
{"item": "Shield"},
},
"address": Map{
"street": "Grove Street",
},
}
userMutation = Apply(NewDocument(&User{}),
Set("name", "Luffy"),
Set("age", 20),
)
transaction1Mutation = Apply(NewDocument(&Transaction{}),
Set("item", "Sword"),
)
transaction2Mutation = Apply(NewDocument(&Transaction{}),
Set("item", "Shield"),
)
addressMutation = Apply(NewDocument(&Address{}),
Set("street", "Grove Street"),
)
)
userMutation.SetAssoc("transactions", transaction1Mutation, transaction2Mutation)
userMutation.SetAssoc("address", addressMutation)
userMutation.SetDeletedIDs("transactions", []interface{}{})
assert.Equal(t, userMutation, Apply(doc, data))
assert.Equal(t, User{
Name: "Luffy",
Age: 20,
Transactions: []Transaction{
{Item: "Sword"},
{Item: "Shield"},
},
Address: Address{
Street: "Grove Street",
},
}, user)
}
func TestMap_CascadeDisabled(t *testing.T) {
var (
user User
doc = NewDocument(&user)
data = Map{
"name": "Luffy",
"age": 20,
"transactions": []Map{
{"item": "Sword"},
{"item": "Shield"},
},
"address": Map{
"street": "Grove Street",
},
}
userMutation = Apply(NewDocument(&User{}),
Cascade(false),
Set("name", "Luffy"),
Set("age", 20),
)
)
assert.Equal(t, userMutation, Apply(doc, Cascade(false), data))
assert.Equal(t, User{
Name: "Luffy",
Age: 20,
}, user)
}
func TestMap_update(t *testing.T) {
var (
user = User{
ID: 1,
Transactions: []Transaction{
{ID: 2},
{ID: 3},
},
Address: Address{
ID: 4,
},
}
doc = NewDocument(&user)
data = Map{
"name": "Luffy",
"age": 20,
"transactions": []Map{
{"id": 2, "item": "Sword"},
{"id": 3, "item": "Shield"},
},
"address": Map{
"street": "Grove Street",
},
}
userMutation = Apply(NewDocument(&User{}),
Set("name", "Luffy"),
Set("age", 20),
)
transaction1Mutation = Apply(NewDocument(&Transaction{}),
Set("item", "Sword"),
)
transaction2Mutation = Apply(NewDocument(&Transaction{}),
Set("item", "Shield"),
)
addressMutation = Apply(NewDocument(&Address{}),
Set("street", "Grove Street"),
)
)
userMutation.SetAssoc("transactions", transaction1Mutation, transaction2Mutation)
userMutation.SetAssoc("address", addressMutation)
userMutation.SetDeletedIDs("transactions", []interface{}{})
assert.Equal(t, userMutation, Apply(doc, data))
assert.Equal(t, User{
ID: 1,
Name: "Luffy",
Age: 20,
Transactions: []Transaction{
{ID: 2, Item: "Sword"},
{ID: 3, Item: "Shield"},
},
Address: Address{
ID: 4,
Street: "Grove Street",
},
}, user)
}
func TestMap_hasManyUpdateDeleteInsert(t *testing.T) {
var (
user = User{
Transactions: []Transaction{
{ID: 2},
{ID: 3},
},
}
doc = NewDocument(&user)
data = Map{
"transactions": []Map{
{"item": "Sword"},
{"id": 3, "item": "Shield"},
},
}
userMutation = Mutation{Cascade: true}
transaction1Mutation = Apply(NewDocument(&Transaction{}),
Set("item", "Sword"),
)
transaction2Mutation = Apply(NewDocument(&Transaction{}),
Set("item", "Shield"),
)
)
userMutation.SetAssoc("transactions", transaction2Mutation, transaction1Mutation)
userMutation.SetDeletedIDs("transactions", []interface{}{2})
assert.Equal(t, userMutation, Apply(doc, data))
assert.Equal(t, User{
Transactions: []Transaction{
{ID: 3, Item: "Shield"},
{Item: "Sword"},
},
}, user)
}
func TestMap_hasManyUpdateNotLoaded(t *testing.T) {
var (
user = User{
Transactions: []Transaction{
{ID: 2},
},
}
doc = NewDocument(&user)
data = Map{
"transactions": []Map{
{"id": 3, "item": "Sword"},
},
}
)
assert.PanicsWithValue(t, "rel: cannot update has many assoc that is not loaded or doesn't belong to this record", func() {
Apply(doc, data)
})
}
func TestMap_hasManyWrongType(t *testing.T) {
var (
user = User{
Transactions: []Transaction{},
}
doc = NewDocument(&user)
data = Map{
"transactions": Map{"item": "Sword"},
}
)
assert.Panics(t, func() {
Apply(doc, data)
})
}
func TestMap_wrongType(t *testing.T) {
var (
user User
doc = NewDocument(&user)
data = Map{
"name": false,
}
)
assert.Panics(t, func() {
Apply(doc, Cascade(false), data)
})
}
func TestMap_replacingPrimaryKey(t *testing.T) {
var (
user = User{ID: 1}
doc = NewDocument(&user)
data = Map{
"id": 2,
}
)
assert.Panics(t, func() {
Apply(doc, Cascade(false), data)
})
}