forked from go-rel/rel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert.go
261 lines (223 loc) · 6.41 KB
/
insert.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package specs
import (
"testing"
"github.com/Fs02/rel"
"github.com/Fs02/rel/where"
"github.com/stretchr/testify/assert"
)
// Insert tests specification for database insertion.
func Insert(t *testing.T, repo rel.Repository) {
var (
note = "swordsman"
user = User{
Name: "insert",
Gender: "male",
Age: 23,
Note: ¬e,
}
)
err := repo.Insert(ctx, &user)
assert.Nil(t, err)
assert.NotZero(t, user.ID)
assert.Equal(t, "insert", user.Name)
assert.Equal(t, "male", user.Gender)
assert.Equal(t, 23, user.Age)
assert.Equal(t, ¬e, user.Note)
var (
queried User
)
user.Addresses = nil
err = repo.Find(ctx, &queried, where.Eq("id", user.ID))
assert.Nil(t, err)
assert.Equal(t, user, queried)
}
// InsertHasMany tests specification insertion with has many association.
func InsertHasMany(t *testing.T, repo rel.Repository) {
var (
result User
user = User{
Name: "insert has many",
Gender: "male",
Age: 23,
Addresses: []Address{
{Name: "primary"},
{Name: "work"},
},
}
)
err := repo.Insert(ctx, &user)
assert.Nil(t, err)
assert.NotZero(t, user.ID)
assert.Equal(t, "insert has many", user.Name)
assert.Equal(t, "male", user.Gender)
assert.Equal(t, 23, user.Age)
assert.Len(t, user.Addresses, 2)
assert.NotZero(t, user.Addresses[0].ID)
assert.NotZero(t, user.Addresses[1].ID)
assert.Equal(t, user.ID, *user.Addresses[0].UserID)
assert.Equal(t, user.ID, *user.Addresses[1].UserID)
assert.Equal(t, "primary", user.Addresses[0].Name)
assert.Equal(t, "work", user.Addresses[1].Name)
repo.MustFind(ctx, &result, where.Eq("id", user.ID))
repo.MustPreload(ctx, &result, "addresses")
assert.Equal(t, result, user)
}
// InsertHasOne tests specification for insertion with has one association.
func InsertHasOne(t *testing.T, repo rel.Repository) {
var (
result User
user = User{
Name: "insert has one",
Gender: "male",
Age: 23,
PrimaryAddress: &Address{Name: "primary"},
}
)
err := repo.Insert(ctx, &user)
assert.Nil(t, err)
assert.NotZero(t, user.ID)
assert.Equal(t, "insert has one", user.Name)
assert.Equal(t, "male", user.Gender)
assert.Equal(t, 23, user.Age)
assert.NotZero(t, user.PrimaryAddress.ID)
assert.Equal(t, user.ID, *user.PrimaryAddress.UserID)
assert.Equal(t, "primary", user.PrimaryAddress.Name)
repo.MustFind(ctx, &result, where.Eq("id", user.ID))
repo.MustPreload(ctx, &result, "primary_address")
assert.Equal(t, result, user)
}
// InsertBelongsTo tests specification for insertion with belongs to association.
func InsertBelongsTo(t *testing.T, repo rel.Repository) {
var (
result Address
address = Address{
Name: "insert belongs to",
User: User{
Name: "zoro",
Gender: "male",
Age: 23,
},
}
)
err := repo.Insert(ctx, &address)
assert.Nil(t, err)
assert.NotZero(t, address.ID)
assert.Equal(t, address.User.ID, *address.UserID)
assert.Equal(t, "insert belongs to", address.Name)
assert.NotZero(t, address.User.ID)
assert.Equal(t, "zoro", address.User.Name)
assert.Equal(t, "male", address.User.Gender)
assert.Equal(t, 23, address.User.Age)
repo.MustFind(ctx, &result, where.Eq("id", address.ID))
repo.MustPreload(ctx, &result, "user")
assert.Equal(t, result, address)
}
// Inserts tests insert specifications.
func Inserts(t *testing.T, repo rel.Repository) {
var (
user User
note = "note"
)
repo.MustInsert(ctx, &user)
tests := []interface{}{
&User{},
&User{Name: "insert", Age: 100},
&User{Name: "insert", Age: 100, Note: ¬e},
&User{Note: ¬e},
&User{ID: 123, Name: "insert", Age: 100, Note: ¬e},
&Address{},
&Address{Name: "work"},
&Address{UserID: &user.ID},
&Address{Name: "work", UserID: &user.ID},
&Address{ID: 123, Name: "work", UserID: &user.ID},
&Composite{Primary1: 1, Primary2: 2, Data: "data-1-2"},
}
for _, record := range tests {
t.Run("Insert", func(t *testing.T) {
assert.Nil(t, repo.Insert(ctx, record))
assertRecord(t, repo, record)
})
}
}
func assertRecord(t *testing.T, repo rel.Repository, record interface{}) {
switch v := record.(type) {
case *User:
var found User
repo.MustFind(ctx, &found, where.Eq("id", v.ID))
assert.Equal(t, found, *v)
case *Address:
var found Address
repo.MustFind(ctx, &found, where.Eq("id", v.ID))
assert.Equal(t, found, *v)
case *Composite:
var found Composite
repo.MustFind(ctx, &found, where.Eq("primary1", v.Primary1).AndEq("primary2", v.Primary2))
assert.Equal(t, found, *v)
}
}
// InsertAll tests insert multiple specifications.
func InsertAll(t *testing.T, repo rel.Repository) {
var (
user User
note = "note"
)
repo.MustInsert(ctx, &user)
tests := []interface{}{
&[]User{{}},
&[]User{{Name: "insert", Age: 100}},
&[]User{{Name: "insert", Age: 100, Note: ¬e}},
&[]User{{Note: ¬e}},
&[]User{{Name: "insert", Age: 100}, {Name: "insert too"}},
&[]User{{ID: 224, Name: "insert", Age: 100}, {ID: 234, Name: "insert too"}},
&[]Address{{}},
&[]Address{{Name: "work"}},
&[]Address{{UserID: &user.ID}},
&[]Address{{Name: "work", UserID: &user.ID}},
&[]Address{{Name: "work"}, {Name: "home"}},
&[]Address{{ID: 233, Name: "work"}, {ID: 235, Name: "home"}},
}
for _, record := range tests {
t.Run("InsertAll", func(t *testing.T) {
assert.Nil(t, repo.InsertAll(ctx, record))
assertRecords(t, repo, record)
})
}
}
// InsertAllPartialCustomPrimary tests insert multiple specifications.
func InsertAllPartialCustomPrimary(t *testing.T, repo rel.Repository) {
tests := []interface{}{
&[]User{{ID: 300, Name: "insert 300", Age: 100}, {Name: "insert 300+?"}},
&[]User{{Name: "insert 305-?", Age: 100}, {ID: 305, Name: "insert 305+?"}},
&[]User{{Name: "insert 310-?"}, {ID: 310, Name: "insert 310", Age: 100}, {Name: "insert 300+?"}},
}
for _, record := range tests {
t.Run("InsertAll", func(t *testing.T) {
assert.Nil(t, repo.InsertAll(ctx, record))
assertRecords(t, repo, record)
})
}
}
func assertRecords(t *testing.T, repo rel.Repository, records interface{}) {
switch v := records.(type) {
case *[]User:
var (
found []User
ids = make([]int, len(*v))
)
for i := range *v {
ids[i] = int((*v)[i].ID)
}
repo.MustFindAll(ctx, &found, where.InInt("id", ids))
assert.Equal(t, found, *v)
case *[]Address:
var (
found []Address
ids = make([]int, len(*v))
)
for i := range *v {
ids[i] = int((*v)[i].ID)
}
repo.MustFindAll(ctx, &found, where.InInt("id", ids))
assert.Equal(t, found, *v)
}
}