-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrelations_test.go
450 lines (376 loc) · 12.4 KB
/
relations_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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
package tormenta_test
import (
"testing"
"github.com/jpincas/gouuidv6"
"github.com/jpincas/tormenta/testtypes"
"github.com/jpincas/tormenta"
)
func Test_Relations_LoadByID(t *testing.T) {
// Open the DB
db, _ := tormenta.OpenTestWithOptions("data/tests", testDBOptions)
defer db.Close()
doubleNestedStruct1 := testtypes.DoubleNestedRelatedStruct{}
doubleNestedStruct2 := testtypes.DoubleNestedRelatedStruct{}
db.Save(&doubleNestedStruct1, &doubleNestedStruct2)
// Created some nested structs and save
nestedStruct1 := testtypes.NestedRelatedStruct{
NestedID: doubleNestedStruct2.ID,
}
nestedStruct2 := testtypes.NestedRelatedStruct{
NestedID: doubleNestedStruct1.ID,
}
db.Save(&nestedStruct1, &nestedStruct2)
// Create some related structs which nest the above and save
relatedStruct1 := testtypes.RelatedStruct{
NestedID: nestedStruct2.ID,
}
relatedStruct2 := testtypes.RelatedStruct{
NestedID: nestedStruct1.ID,
}
db.Save(&relatedStruct1, &relatedStruct2)
// Create some full structs including these relations
struct1 := testtypes.FullStruct{
HasOneID: relatedStruct1.ID,
HasAnotherOneID: relatedStruct1.ID,
HasManyIDs: []gouuidv6.UUID{relatedStruct1.ID, relatedStruct2.ID},
}
struct2 := testtypes.FullStruct{
HasOneID: relatedStruct2.ID,
HasAnotherOneID: relatedStruct2.ID,
HasManyIDs: []gouuidv6.UUID{relatedStruct1.ID, relatedStruct2.ID},
}
struct3 := testtypes.FullStruct{
HasOneID: relatedStruct1.ID,
HasAnotherOneID: relatedStruct1.ID,
HasManyIDs: []gouuidv6.UUID{relatedStruct1.ID, relatedStruct2.ID},
}
db.Save(&struct1, &struct2, &struct3)
// A) Single invalid relation, no nesting
// Reload
fullStructs := []testtypes.FullStruct{}
if n, err := db.Find(&fullStructs).Run(); err != nil || n != 3 {
t.Errorf("Save/retrieve failed. Err: %v; n: %v", err, n)
}
// Convert to records
records := []tormenta.Record{}
for i := range fullStructs {
records = append(records, &fullStructs[i])
}
testName := "non existent relation"
if err := tormenta.LoadByID(db, []string{"DoesntHaveOne"}, records...); err == nil {
t.Errorf("Testing %s. Should have error but didn't", testName)
}
// 1) Single relation, no nesting
// Reload
fullStructs = []testtypes.FullStruct{}
if n, err := db.Find(&fullStructs).Run(); err != nil || n != 3 {
t.Errorf("Save/retrieve failed. Err: %v; n: %v", err, n)
}
// Convert to records
records = []tormenta.Record{}
for i := range fullStructs {
records = append(records, &fullStructs[i])
}
testName = "single, one level relation"
if err := tormenta.LoadByID(db, []string{"HasOne"}, records...); err != nil {
t.Errorf("Testing %s. Error loading relations: %s", testName, err)
}
for i, fullStruct := range fullStructs {
if fullStruct.HasOneID != fullStruct.HasOne.ID {
t.Errorf(
"Testing %s. Comparing HasOneID to HasOne.ID for order %v and they are not the same: %v vs %v",
testName,
i,
fullStruct.HasOneID,
fullStruct.HasOne.ID,
)
}
}
// 2) Two relations, but repeated
// Reload
fullStructs = []testtypes.FullStruct{}
if n, err := db.Find(&fullStructs).Run(); err != nil || n != 3 {
t.Errorf("Save/retrieve failed. Err: %v; n: %v", err, n)
}
// Convert to records
records = []tormenta.Record{}
for i := range fullStructs {
records = append(records, &fullStructs[i])
}
testName = "two relations, one level, repeated"
if err := tormenta.LoadByID(db, []string{"HasOne", "HasOne"}, records...); err != nil {
t.Errorf("Testing %s. Error loading relations: %s", testName, err)
}
for i, fullStruct := range fullStructs {
if fullStruct.HasOneID != fullStruct.HasOne.ID {
t.Errorf(
"Testing %s. Comparing HasOneID to HasOne.ID for order %v and they are not the same: %v vs %v",
testName,
i,
fullStruct.HasOneID,
fullStruct.HasOne.ID,
)
}
}
// 3) Two relations
// Reload
fullStructs = []testtypes.FullStruct{}
if n, err := db.Find(&fullStructs).Run(); err != nil || n != 3 {
t.Errorf("Save/retrieve failed. Err: %v; n: %v", err, n)
}
// Convert to records
records = []tormenta.Record{}
for i := range fullStructs {
records = append(records, &fullStructs[i])
}
testName = "two relations, one level, repeated"
if err := tormenta.LoadByID(db, []string{"HasOne", "HasAnotherOne"}, records...); err != nil {
t.Errorf("Testing %s. Error loading relations: %s", testName, err)
}
for i, fullStruct := range fullStructs {
if fullStruct.HasOneID != fullStruct.HasOne.ID {
t.Errorf(
"Testing %s. Comparing HasOneID to HasOne.ID for order %v and they are not the same: %v vs %v",
testName,
i,
fullStruct.HasOneID,
fullStruct.HasOne.ID,
)
}
if fullStruct.HasAnotherOneID != fullStruct.HasAnotherOne.ID {
t.Errorf(
"Testing %s. Comparing HasAnotherOneID to HasAnotherOne.ID for order %v and they are not the same: %v vs %v",
testName,
i,
fullStruct.HasAnotherOneID,
fullStruct.HasAnotherOne.ID,
)
}
}
// 4) Single relation, 2 level nesting
// Reload
fullStructs = []testtypes.FullStruct{}
if n, err := db.Find(&fullStructs).Run(); err != nil || n != 3 {
t.Errorf("Save/retrieve failed. Err: %v; n: %v", err, n)
}
// Convert to records
records = []tormenta.Record{}
for i := range fullStructs {
records = append(records, &fullStructs[i])
}
testName = "single, nested relation"
if err := tormenta.LoadByID(db, []string{"HasOne.Nested"}, records...); err != nil {
t.Errorf("Testing %s. Error loading relations: %s", testName, err)
}
for i, fullStruct := range fullStructs {
if fullStruct.HasOneID != fullStruct.HasOne.ID {
t.Errorf(
"Testing %s. Comparing HasOneID to HasOne.ID for order %v and they are not the same: %v vs %v",
testName,
i,
fullStruct.HasOneID,
fullStruct.HasOne.ID,
)
}
if fullStruct.HasOne.Nested == nil {
t.Fatalf("Testing %s - index %v. HasOne.Nested is nil - the relation didn't load", testName, i)
}
if fullStruct.HasOne.NestedID != fullStruct.HasOne.Nested.ID {
t.Errorf(
"Testing %s. Comparing HasOne.NestedID to HasOne.Nested.ID for order %v and they are not the same: %v vs %v",
testName,
i,
fullStruct.HasOne.NestedID,
fullStruct.HasOne.Nested.ID,
)
}
}
// 4) Single relation, 3 level nesting
// Reload
fullStructs = []testtypes.FullStruct{}
if n, err := db.Find(&fullStructs).Run(); err != nil || n != 3 {
t.Errorf("Save/retrieve failed. Err: %v; n: %v", err, n)
}
// Convert to records
records = []tormenta.Record{}
for i := range fullStructs {
records = append(records, &fullStructs[i])
}
testName = "single, 3 level nested relation"
if err := tormenta.LoadByID(db, []string{"HasOne.Nested.Nested"}, records...); err != nil {
t.Errorf("Testing %s. Error loading relations: %s", testName, err)
}
for i, fullStruct := range fullStructs {
if fullStruct.HasOneID != fullStruct.HasOne.ID {
t.Errorf(
"Testing %s. Comparing HasOneID to HasOne.ID for order %v and they are not the same: %v vs %v",
testName,
i,
fullStruct.HasOneID,
fullStruct.HasOne.ID,
)
}
// L1
if fullStruct.HasOne.Nested == nil {
t.Fatalf("Testing %s - index %v. HasOne.Nested is nil - the relation didn't load", testName, i)
}
if fullStruct.HasOne.NestedID != fullStruct.HasOne.Nested.ID {
t.Errorf(
"Testing %s. Comparing HasOne.NestedID to HasOne.Nested.ID for order %v and they are not the same: %v vs %v",
testName,
i,
fullStruct.HasOne.NestedID,
fullStruct.HasOne.Nested.ID,
)
}
// L2
if fullStruct.HasOne.Nested.Nested == nil {
t.Fatalf("Testing %s - index %v. HasOne.Nested.Nested is nil - the relation didn't load", testName, i)
}
if fullStruct.HasOne.Nested.NestedID != fullStruct.HasOne.Nested.Nested.ID {
t.Errorf(
"Testing %s. Comparing HasOne.Nested.NestedID to HasOne.Nested.Nested.ID for order %v and they are not the same: %v vs %v",
testName,
i,
fullStruct.HasOne.NestedID,
fullStruct.HasOne.Nested.ID,
)
}
}
// 5) Multiple IDs
// Reload
fullStructs = []testtypes.FullStruct{}
if n, err := db.Find(&fullStructs).Run(); err != nil || n != 3 {
t.Errorf("Save/retrieve failed. Err: %v; n: %v", err, n)
}
// Convert to records
records = []tormenta.Record{}
for i := range fullStructs {
records = append(records, &fullStructs[i])
}
testName = "multiple ids"
if err := tormenta.LoadByID(db, []string{"HasMany"}, records...); err != nil {
t.Fatalf("Testing %s. Error loading relations: %s", testName, err)
}
for i, fullStruct := range fullStructs {
if len(fullStruct.HasMany) == 0 {
t.Fatalf("Testing %s. No HasMany relations loaded", testName)
}
for j, hasManyId := range fullStruct.HasManyIDs {
if hasManyId != fullStruct.HasMany[j].ID {
t.Errorf(
"Testing %s. Comparing each HasManyID to the retrieved record ID for i:%v and they are not the same: %v vs %v",
testName,
i,
hasManyId,
fullStruct.HasMany[j].ID,
)
}
}
}
}
func Test_Relations_LoadByID_InvalidID(t *testing.T) {
// Open the DB
db, _ := tormenta.OpenTestWithOptions("data/tests", testDBOptions)
defer db.Close()
// Save a compleletely blank struct
myStruct := &testtypes.FullStruct{}
if _, err := db.Save(myStruct); err != nil {
t.Errorf("Testing relations load by ID (invalid ID) - could not save: %s", err)
}
// Attempt load by ID using the "HasOne" and "HasMany" fields,
// but since the ids on those fields are going to be blank, there is no relation to get,
// That should not cause an error or crash
if err := tormenta.LoadByID(db, []string{"HasOne", "HasMany"}, myStruct); err != nil {
t.Errorf("Testing relations load by ID (invalid ID) - got error: %s", err)
}
}
func Test_Relations_LoadByQuery_Basic(t *testing.T) {
// Open the DB
db, _ := tormenta.OpenTestWithOptions("data/tests", testDBOptions)
defer db.Close()
// Create a full struct
struct1 := testtypes.FullStruct{}
if _, err := db.Save(&struct1); err != nil {
t.Error("Saving error")
}
// Some related structs that reference the above full structs
relatedStruct1 := testtypes.RelatedStruct{
FullStructID: struct1.ID,
}
relatedStruct2 := testtypes.RelatedStruct{
FullStructID: struct1.ID,
}
if _, err := db.Save(&relatedStruct1, &relatedStruct2); err != nil {
t.Error("Saving error")
}
tormenta.LoadByQuery(db, "RelatedStructsByQuery", nil, &struct1)
if len(struct1.RelatedStructsByQuery) != 2 {
t.Errorf("Did not get 2 related structs, got %v", len(struct1.RelatedStructsByQuery))
}
}
func Test_Relations_LoadByQuery_MultipleEntities(t *testing.T) {
// Open the DB
db, _ := tormenta.OpenTestWithOptions("data/tests", testDBOptions)
defer db.Close()
var fullStructs []testtypes.FullStruct
for i := 0; i < 2; i++ {
// Start with a struct
struct1 := testtypes.FullStruct{}
if _, err := db.Save(&struct1); err != nil {
t.Fatal("Saving error")
}
// Keep a record of it
fullStructs = append(fullStructs, struct1)
// Some related structs that reference the above full structs
relatedStruct1 := testtypes.RelatedStruct{
FullStructID: struct1.ID,
}
relatedStruct2 := testtypes.RelatedStruct{
FullStructID: struct1.ID,
}
if _, err := db.Save(&relatedStruct1, &relatedStruct2); err != nil {
t.Fatal("Saving error")
}
}
var records []tormenta.Record
for i := range fullStructs {
records = append(records, &fullStructs[i])
}
tormenta.LoadByQuery(db, "RelatedStructsByQuery", nil, records...)
for _, f := range fullStructs {
if len(f.RelatedStructsByQuery) != 2 {
t.Errorf("Expected 2 related structs, got %v", len(f.RelatedStructsByQuery))
}
}
}
func Test_Relations_LoadByQuery_Additional_Clauses(t *testing.T) {
// Open the DB
db, _ := tormenta.OpenTestWithOptions("data/tests", testDBOptions)
defer db.Close()
// Create a full struct
struct1 := testtypes.FullStruct{}
if _, err := db.Save(&struct1); err != nil {
t.Error("Saving error")
}
// Some related structs that reference the above full structs
relatedStruct1 := testtypes.RelatedStruct{
FullStructID: struct1.ID,
StructBoolField: false,
}
relatedStruct2 := testtypes.RelatedStruct{
FullStructID: struct1.ID,
StructBoolField: true,
}
if _, err := db.Save(&relatedStruct1, &relatedStruct2); err != nil {
t.Error("Saving error")
}
conditions := func(q *tormenta.Query) *tormenta.Query {
q.Match("StructBoolField", true)
return q
}
tormenta.LoadByQuery(db, "RelatedStructsByQuery", conditions, &struct1)
if len(struct1.RelatedStructsByQuery) != 1 {
t.Error("Did not get 1 related structs")
}
}