forked from pocketbase/pocketbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
record_test.go
824 lines (743 loc) · 23.1 KB
/
record_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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
package daos_test
import (
"context"
"database/sql"
"errors"
"fmt"
"regexp"
"strings"
"testing"
"time"
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/daos"
"github.com/pocketbase/pocketbase/models"
"github.com/pocketbase/pocketbase/models/schema"
"github.com/pocketbase/pocketbase/tests"
"github.com/pocketbase/pocketbase/tools/list"
"github.com/pocketbase/pocketbase/tools/types"
)
func TestRecordQuery(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
collection, err := app.Dao().FindCollectionByNameOrId("demo1")
if err != nil {
t.Fatal(err)
}
expected := fmt.Sprintf("SELECT `%s`.* FROM `%s`", collection.Name, collection.Name)
sql := app.Dao().RecordQuery(collection).Build().SQL()
if sql != expected {
t.Errorf("Expected sql %s, got %s", expected, sql)
}
}
func TestFindRecordById(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
scenarios := []struct {
collectionIdOrName string
id string
filter1 func(q *dbx.SelectQuery) error
filter2 func(q *dbx.SelectQuery) error
expectError bool
}{
{"demo2", "missing", nil, nil, true},
{"missing", "0yxhwia2amd8gec", nil, nil, true},
{"demo2", "0yxhwia2amd8gec", nil, nil, false},
{"demo2", "0yxhwia2amd8gec", func(q *dbx.SelectQuery) error {
q.AndWhere(dbx.HashExp{"title": "missing"})
return nil
}, nil, true},
{"demo2", "0yxhwia2amd8gec", func(q *dbx.SelectQuery) error {
return errors.New("test error")
}, nil, true},
{"demo2", "0yxhwia2amd8gec", func(q *dbx.SelectQuery) error {
q.AndWhere(dbx.HashExp{"title": "test3"})
return nil
}, nil, false},
{"demo2", "0yxhwia2amd8gec", func(q *dbx.SelectQuery) error {
q.AndWhere(dbx.HashExp{"title": "test3"})
return nil
}, func(q *dbx.SelectQuery) error {
q.AndWhere(dbx.HashExp{"active": false})
return nil
}, true},
{"sz5l5z67tg7gku0", "0yxhwia2amd8gec", func(q *dbx.SelectQuery) error {
q.AndWhere(dbx.HashExp{"title": "test3"})
return nil
}, func(q *dbx.SelectQuery) error {
q.AndWhere(dbx.HashExp{"active": true})
return nil
}, false},
}
for i, scenario := range scenarios {
record, err := app.Dao().FindRecordById(
scenario.collectionIdOrName,
scenario.id,
scenario.filter1,
scenario.filter2,
)
hasErr := err != nil
if hasErr != scenario.expectError {
t.Errorf("(%d) Expected hasErr to be %v, got %v (%v)", i, scenario.expectError, hasErr, err)
}
if record != nil && record.Id != scenario.id {
t.Errorf("(%d) Expected record with id %s, got %s", i, scenario.id, record.Id)
}
}
}
func TestFindRecordsByIds(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
scenarios := []struct {
collectionIdOrName string
ids []string
filter1 func(q *dbx.SelectQuery) error
filter2 func(q *dbx.SelectQuery) error
expectTotal int
expectError bool
}{
{"demo2", []string{}, nil, nil, 0, false},
{"demo2", []string{""}, nil, nil, 0, false},
{"demo2", []string{"missing"}, nil, nil, 0, false},
{"missing", []string{"0yxhwia2amd8gec"}, nil, nil, 0, true},
{"demo2", []string{"0yxhwia2amd8gec"}, nil, nil, 1, false},
{"sz5l5z67tg7gku0", []string{"0yxhwia2amd8gec"}, nil, nil, 1, false},
{
"demo2",
[]string{"0yxhwia2amd8gec", "llvuca81nly1qls"},
nil,
nil,
2,
false,
},
{
"demo2",
[]string{"0yxhwia2amd8gec", "llvuca81nly1qls"},
func(q *dbx.SelectQuery) error {
return nil // empty filter
},
func(q *dbx.SelectQuery) error {
return errors.New("test error")
},
0,
true,
},
{
"demo2",
[]string{"0yxhwia2amd8gec", "llvuca81nly1qls"},
func(q *dbx.SelectQuery) error {
q.AndWhere(dbx.HashExp{"active": true})
return nil
},
nil,
1,
false,
},
{
"sz5l5z67tg7gku0",
[]string{"0yxhwia2amd8gec", "llvuca81nly1qls"},
func(q *dbx.SelectQuery) error {
q.AndWhere(dbx.HashExp{"active": true})
return nil
},
func(q *dbx.SelectQuery) error {
q.AndWhere(dbx.Not(dbx.HashExp{"title": ""}))
return nil
},
1,
false,
},
}
for i, scenario := range scenarios {
records, err := app.Dao().FindRecordsByIds(
scenario.collectionIdOrName,
scenario.ids,
scenario.filter1,
scenario.filter2,
)
hasErr := err != nil
if hasErr != scenario.expectError {
t.Errorf("(%d) Expected hasErr to be %v, got %v (%v)", i, scenario.expectError, hasErr, err)
}
if len(records) != scenario.expectTotal {
t.Errorf("(%d) Expected %d records, got %d", i, scenario.expectTotal, len(records))
continue
}
for _, r := range records {
if !list.ExistInSlice(r.Id, scenario.ids) {
t.Errorf("(%d) Couldn't find id %s in %v", i, r.Id, scenario.ids)
}
}
}
}
func TestFindRecordsByExpr(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
scenarios := []struct {
collectionIdOrName string
expressions []dbx.Expression
expectIds []string
expectError bool
}{
{
"missing",
nil,
[]string{},
true,
},
{
"demo2",
nil,
[]string{
"achvryl401bhse3",
"llvuca81nly1qls",
"0yxhwia2amd8gec",
},
false,
},
{
"demo2",
[]dbx.Expression{
nil,
dbx.HashExp{"id": "123"},
},
[]string{},
false,
},
{
"sz5l5z67tg7gku0",
[]dbx.Expression{
dbx.Like("title", "test").Match(true, true),
dbx.HashExp{"active": true},
},
[]string{
"achvryl401bhse3",
"0yxhwia2amd8gec",
},
false,
},
}
for i, scenario := range scenarios {
records, err := app.Dao().FindRecordsByExpr(scenario.collectionIdOrName, scenario.expressions...)
hasErr := err != nil
if hasErr != scenario.expectError {
t.Errorf("(%d) Expected hasErr to be %v, got %v (%v)", i, scenario.expectError, hasErr, err)
}
if len(records) != len(scenario.expectIds) {
t.Errorf("(%d) Expected %d records, got %d", i, len(scenario.expectIds), len(records))
continue
}
for _, r := range records {
if !list.ExistInSlice(r.Id, scenario.expectIds) {
t.Errorf("(%d) Couldn't find id %s in %v", i, r.Id, scenario.expectIds)
}
}
}
}
func TestFindFirstRecordByData(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
scenarios := []struct {
collectionIdOrName string
key string
value any
expectId string
expectError bool
}{
{
"missing",
"id",
"llvuca81nly1qls",
"llvuca81nly1qls",
true,
},
{
"demo2",
"",
"llvuca81nly1qls",
"",
true,
},
{
"demo2",
"id",
"invalid",
"",
true,
},
{
"demo2",
"id",
"llvuca81nly1qls",
"llvuca81nly1qls",
false,
},
{
"sz5l5z67tg7gku0",
"title",
"test3",
"0yxhwia2amd8gec",
false,
},
}
for i, scenario := range scenarios {
record, err := app.Dao().FindFirstRecordByData(scenario.collectionIdOrName, scenario.key, scenario.value)
hasErr := err != nil
if hasErr != scenario.expectError {
t.Errorf("(%d) Expected hasErr to be %v, got %v (%v)", i, scenario.expectError, hasErr, err)
continue
}
if !scenario.expectError && record.Id != scenario.expectId {
t.Errorf("(%d) Expected record with id %s, got %v", i, scenario.expectId, record.Id)
}
}
}
func TestIsRecordValueUnique(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
testManyRelsId1 := "bgs820n361vj1qd"
testManyRelsId2 := "4q1xlclmfloku33"
testManyRelsId3 := "oap640cot4yru2s"
scenarios := []struct {
collectionIdOrName string
key string
value any
excludeIds []string
expected bool
}{
{"demo2", "", "", nil, false},
{"demo2", "", "", []string{""}, false},
{"demo2", "missing", "unique", nil, false},
{"demo2", "title", "unique", nil, true},
{"demo2", "title", "unique", []string{}, true},
{"demo2", "title", "unique", []string{""}, true},
{"demo2", "title", "test1", []string{""}, false},
{"demo2", "title", "test1", []string{"llvuca81nly1qls"}, true},
{"demo1", "rel_many", []string{testManyRelsId3}, nil, false},
{"wsmn24bux7wo113", "rel_many", []any{testManyRelsId3}, []string{""}, false},
{"wsmn24bux7wo113", "rel_many", []any{testManyRelsId3}, []string{"84nmscqy84lsi1t"}, true},
// mixed json array order
{"demo1", "rel_many", []string{testManyRelsId1, testManyRelsId3, testManyRelsId2}, nil, true},
// username special case-insensitive match
{"users", "username", "test2_username", nil, false},
{"users", "username", "TEST2_USERNAME", nil, false},
{"users", "username", "new_username", nil, true},
{"users", "username", "TEST2_USERNAME", []string{"oap640cot4yru2s"}, true},
}
for i, scenario := range scenarios {
result := app.Dao().IsRecordValueUnique(
scenario.collectionIdOrName,
scenario.key,
scenario.value,
scenario.excludeIds...,
)
if result != scenario.expected {
t.Errorf("(%d) Expected %v, got %v", i, scenario.expected, result)
}
}
}
func TestFindAuthRecordByToken(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
scenarios := []struct {
token string
baseKey string
expectedEmail string
expectError bool
}{
// invalid auth token
{
"eyJhbGciOiJIUzI1NiJ9.eyJpZCI6IjRxMXhsY2xtZmxva3UzMyIsInR5cGUiOiJhdXRoUmVjb3JkIiwiY29sbGVjdGlvbklkIjoiX3BiX3VzZXJzX2F1dGhfIiwiZXhwIjoyMjA4OTg1MjYxfQ.H2KKcIXiAfxvuXMFzizo1SgsinDP4hcWhD3pYoP4Nqw",
app.Settings().RecordAuthToken.Secret,
"",
true,
},
// expired token
{
"eyJhbGciOiJIUzI1NiJ9.eyJpZCI6IjRxMXhsY2xtZmxva3UzMyIsInR5cGUiOiJhdXRoUmVjb3JkIiwiY29sbGVjdGlvbklkIjoiX3BiX3VzZXJzX2F1dGhfIiwiZXhwIjoxNjQwOTkxNjYxfQ.HqvpCpM0RAk3Qu9PfCMuZsk_DKh9UYuzFLwXBMTZd1w",
app.Settings().RecordAuthToken.Secret,
"",
true,
},
// wrong base key (password reset token secret instead of auth secret)
{
"eyJhbGciOiJIUzI1NiJ9.eyJpZCI6IjRxMXhsY2xtZmxva3UzMyIsInR5cGUiOiJhdXRoUmVjb3JkIiwiY29sbGVjdGlvbklkIjoiX3BiX3VzZXJzX2F1dGhfIiwiZXhwIjoyMjA4OTg1MjYxfQ.UwD8JvkbQtXpymT09d7J6fdA0aP9g4FJ1GPh_ggEkzc",
app.Settings().RecordPasswordResetToken.Secret,
"",
true,
},
// valid token and base key but with deleted/missing collection
{
"eyJhbGciOiJIUzI1NiJ9.eyJpZCI6IjRxMXhsY2xtZmxva3UzMyIsInR5cGUiOiJhdXRoUmVjb3JkIiwiY29sbGVjdGlvbklkIjoibWlzc2luZyIsImV4cCI6MjIwODk4NTI2MX0.0oEHQpdpHp0Nb3VN8La0ssg-SjwWKiRl_k1mUGxdKlU",
app.Settings().RecordAuthToken.Secret,
true,
},
// valid token
{
"eyJhbGciOiJIUzI1NiJ9.eyJpZCI6IjRxMXhsY2xtZmxva3UzMyIsInR5cGUiOiJhdXRoUmVjb3JkIiwiY29sbGVjdGlvbklkIjoiX3BiX3VzZXJzX2F1dGhfIiwiZXhwIjoyMjA4OTg1MjYxfQ.UwD8JvkbQtXpymT09d7J6fdA0aP9g4FJ1GPh_ggEkzc",
app.Settings().RecordAuthToken.Secret,
false,
},
}
for i, scenario := range scenarios {
record, err := app.Dao().FindAuthRecordByToken(scenario.token, scenario.baseKey)
hasErr := err != nil
if hasErr != scenario.expectError {
t.Errorf("(%d) Expected hasErr to be %v, got %v (%v)", i, scenario.expectError, hasErr, err)
continue
}
if !scenario.expectError && record.Email() != scenario.expectedEmail {
t.Errorf("(%d) Expected record model %s, got %s", i, scenario.expectedEmail, record.Email())
}
}
}
func TestFindAuthRecordByEmail(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
scenarios := []struct {
collectionIdOrName string
email string
expectError bool
}{
{"missing", "[email protected]", true},
{"demo2", "[email protected]", true},
{"users", "[email protected]", true},
{"users", "[email protected]", false},
{"clients", "[email protected]", false},
}
for i, scenario := range scenarios {
record, err := app.Dao().FindAuthRecordByEmail(scenario.collectionIdOrName, scenario.email)
hasErr := err != nil
if hasErr != scenario.expectError {
t.Errorf("(%d) Expected hasErr to be %v, got %v (%v)", i, scenario.expectError, hasErr, err)
continue
}
if !scenario.expectError && record.Email() != scenario.email {
t.Errorf("(%d) Expected record with email %s, got %s", i, scenario.email, record.Email())
}
}
}
func TestFindAuthRecordByUsername(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
scenarios := []struct {
collectionIdOrName string
username string
expectError bool
}{
{"missing", "test_username", true},
{"demo2", "test_username", true},
{"users", "missing", true},
{"users", "test2_username", false},
{"users", "TEST2_USERNAME", false}, // case insensitive check
{"clients", "clients43362", false},
}
for i, scenario := range scenarios {
record, err := app.Dao().FindAuthRecordByUsername(scenario.collectionIdOrName, scenario.username)
hasErr := err != nil
if hasErr != scenario.expectError {
t.Errorf("(%d) Expected hasErr to be %v, got %v (%v)", i, scenario.expectError, hasErr, err)
continue
}
if !scenario.expectError && !strings.EqualFold(record.Username(), scenario.username) {
t.Errorf("(%d) Expected record with username %s, got %s", i, scenario.username, record.Username())
}
}
}
func TestSuggestUniqueAuthRecordUsername(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
scenarios := []struct {
collectionIdOrName string
baseUsername string
expectedPattern string
}{
// missing collection
{"missing", "test2_username", `^test2_username\d{12}$`},
// not an auth collection
{"demo2", "test2_username", `^test2_username\d{12}$`},
// auth collection with unique base username
{"users", "new_username", `^new_username$`},
{"users", "NEW_USERNAME", `^NEW_USERNAME$`},
// auth collection with existing username
{"users", "test2_username", `^test2_username\d{3}$`},
{"users", "TEST2_USERNAME", `^TEST2_USERNAME\d{3}$`},
}
for i, scenario := range scenarios {
username := app.Dao().SuggestUniqueAuthRecordUsername(
scenario.collectionIdOrName,
scenario.baseUsername,
)
pattern, err := regexp.Compile(scenario.expectedPattern)
if err != nil {
t.Errorf("[%d] Invalid username pattern %q: %v", i, scenario.expectedPattern, err)
}
if !pattern.MatchString(username) {
t.Fatalf("Expected username to match %s, got username %s", scenario.expectedPattern, username)
}
}
}
func TestSaveRecord(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
collection, _ := app.Dao().FindCollectionByNameOrId("demo2")
// create
// ---
r1 := models.NewRecord(collection)
r1.Set("title", "test_new")
err1 := app.Dao().SaveRecord(r1)
if err1 != nil {
t.Fatal(err1)
}
newR1, _ := app.Dao().FindFirstRecordByData(collection.Id, "title", "test_new")
if newR1 == nil || newR1.Id != r1.Id || newR1.GetString("title") != r1.GetString("title") {
t.Fatalf("Expected to find record %v, got %v", r1, newR1)
}
// update
// ---
r2, _ := app.Dao().FindFirstRecordByData(collection.Id, "id", "0yxhwia2amd8gec")
r2.Set("title", "test_update")
err2 := app.Dao().SaveRecord(r2)
if err2 != nil {
t.Fatal(err2)
}
newR2, _ := app.Dao().FindFirstRecordByData(collection.Id, "title", "test_update")
if newR2 == nil || newR2.Id != r2.Id || newR2.GetString("title") != r2.GetString("title") {
t.Fatalf("Expected to find record %v, got %v", r2, newR2)
}
}
func TestSaveRecordWithIdFromOtherCollection(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
baseCollection, _ := app.Dao().FindCollectionByNameOrId("demo2")
authCollection, _ := app.Dao().FindCollectionByNameOrId("nologin")
// base collection test
r1 := models.NewRecord(baseCollection)
r1.Set("title", "test_new")
r1.Set("id", "mk5fmymtx4wsprk") // existing id of demo3 record
r1.MarkAsNew()
if err := app.Dao().SaveRecord(r1); err != nil {
t.Fatalf("Expected nil, got error %v", err)
}
// auth collection test
r2 := models.NewRecord(authCollection)
r2.Set("username", "test_new")
r2.Set("id", "gk390qegs4y47wn") // existing id of "clients" record
r2.MarkAsNew()
if err := app.Dao().SaveRecord(r2); err == nil {
t.Fatal("Expected error, got nil")
}
// try again with unique id
r2.Set("id", "unique_id")
if err := app.Dao().SaveRecord(r2); err != nil {
t.Fatalf("Expected nil, got error %v", err)
}
}
func TestDeleteRecord(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
demoCollection, _ := app.Dao().FindCollectionByNameOrId("demo2")
// delete unsaved record
// ---
rec0 := models.NewRecord(demoCollection)
if err := app.Dao().DeleteRecord(rec0); err == nil {
t.Fatal("(rec0) Didn't expect to succeed deleting unsaved record")
}
// delete existing record + external auths
// ---
rec1, _ := app.Dao().FindRecordById("users", "4q1xlclmfloku33")
if err := app.Dao().DeleteRecord(rec1); err != nil {
t.Fatalf("(rec1) Expected nil, got error %v", err)
}
// check if it was really deleted
if refreshed, _ := app.Dao().FindRecordById(rec1.Collection().Id, rec1.Id); refreshed != nil {
t.Fatalf("(rec1) Expected record to be deleted, got %v", refreshed)
}
// check if the external auths were deleted
if auths, _ := app.Dao().FindAllExternalAuthsByRecord(rec1); len(auths) > 0 {
t.Fatalf("(rec1) Expected external auths to be deleted, got %v", auths)
}
// delete existing record while being part of a non-cascade required relation
// ---
rec2, _ := app.Dao().FindRecordById("demo3", "7nwo8tuiatetxdm")
if err := app.Dao().DeleteRecord(rec2); err == nil {
t.Fatalf("(rec2) Expected error, got nil")
}
// delete existing record + cascade
// ---
calledQueries := []string{}
app.Dao().NonconcurrentDB().(*dbx.DB).QueryLogFunc = func(ctx context.Context, t time.Duration, sql string, rows *sql.Rows, err error) {
calledQueries = append(calledQueries, sql)
}
app.Dao().ConcurrentDB().(*dbx.DB).QueryLogFunc = func(ctx context.Context, t time.Duration, sql string, rows *sql.Rows, err error) {
calledQueries = append(calledQueries, sql)
}
app.Dao().NonconcurrentDB().(*dbx.DB).ExecLogFunc = func(ctx context.Context, t time.Duration, sql string, result sql.Result, err error) {
calledQueries = append(calledQueries, sql)
}
app.Dao().ConcurrentDB().(*dbx.DB).ExecLogFunc = func(ctx context.Context, t time.Duration, sql string, result sql.Result, err error) {
calledQueries = append(calledQueries, sql)
}
rec3, _ := app.Dao().FindRecordById("users", "oap640cot4yru2s")
// delete
if err := app.Dao().DeleteRecord(rec3); err != nil {
t.Fatalf("(rec3) Expected nil, got error %v", err)
}
// check if it was really deleted
rec3, _ = app.Dao().FindRecordById(rec3.Collection().Id, rec3.Id)
if rec3 != nil {
t.Fatalf("(rec3) Expected record to be deleted, got %v", rec3)
}
// check if the operation cascaded
rel, _ := app.Dao().FindRecordById("demo1", "84nmscqy84lsi1t")
if rel != nil {
t.Fatalf("(rec3) Expected the delete to cascade, found relation %v", rel)
}
// ensure that the json rel fields were prefixed
joinedQueries := strings.Join(calledQueries, " ")
expectedRelManyJoin := "`demo1` INNER JOIN json_each(CASE WHEN json_valid([[demo1.rel_many]]) THEN [[demo1.rel_many]] ELSE json_array([[demo1.rel_many]]) END)"
if !strings.Contains(joinedQueries, expectedRelManyJoin) {
t.Fatalf("(rec3) Expected the cascade delete to call the query \n%v, got \n%v", expectedRelManyJoin, calledQueries)
}
expectedRelOneJoin := "`demo1` INNER JOIN json_each(CASE WHEN json_valid([[demo1.rel_one]]) THEN [[demo1.rel_one]] ELSE json_array([[demo1.rel_one]]) END)"
if !strings.Contains(joinedQueries, expectedRelOneJoin) {
t.Fatalf("(rec3) Expected the cascade delete to call the query \n%v, got \n%v", expectedRelOneJoin, calledQueries)
}
}
func TestDeleteRecordBatchProcessing(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
if err := createMockBatchProcessingData(app.Dao()); err != nil {
t.Fatal(err)
}
// find and delete the first c1 record to trigger cascade
mainRecord, _ := app.Dao().FindRecordById("c1", "a")
if err := app.Dao().DeleteRecord(mainRecord); err != nil {
t.Fatal(err)
}
// check if the main record was deleted
_, err := app.Dao().FindRecordById(mainRecord.Collection().Id, mainRecord.Id)
if err == nil {
t.Fatal("The main record wasn't deleted")
}
// check if the c1 b rel field were updated
c1RecordB, err := app.Dao().FindRecordById("c1", "b")
if err != nil || c1RecordB.GetString("rel") != "" {
t.Fatalf("Expected c1RecordB.rel to be nil, got %v", c1RecordB.GetString("rel"))
}
// check if the c2 rel fields were updated
c2Records, err := app.Dao().FindRecordsByExpr("c2", nil)
if err != nil || len(c2Records) == 0 {
t.Fatalf("Failed to fetch c2 records: %v", err)
}
for _, r := range c2Records {
ids := r.GetStringSlice("rel")
if len(ids) != 1 || ids[0] != "b" {
t.Fatalf("Expected only 'b' rel id, got %v", ids)
}
}
// check if all c3 relations were deleted
c3Records, err := app.Dao().FindRecordsByExpr("c3", nil)
if err != nil {
t.Fatalf("Failed to fetch c3 records: %v", err)
}
if total := len(c3Records); total != 0 {
t.Fatalf("Expected c3 records to be deleted, found %d", total)
}
}
func createMockBatchProcessingData(dao *daos.Dao) error {
// create mock collection without relation
c1 := &models.Collection{}
c1.Id = "c1"
c1.Name = c1.Id
c1.Schema = schema.NewSchema(
&schema.SchemaField{
Name: "text",
Type: schema.FieldTypeText,
},
// self reference
&schema.SchemaField{
Name: "rel",
Type: schema.FieldTypeRelation,
Options: &schema.RelationOptions{
MaxSelect: types.Pointer(1),
CollectionId: "c1",
CascadeDelete: false, // should unset all rel fields
},
},
)
if err := dao.SaveCollection(c1); err != nil {
return err
}
// create mock collection with a multi-rel field
c2 := &models.Collection{}
c2.Id = "c2"
c2.Name = c2.Id
c2.Schema = schema.NewSchema(
&schema.SchemaField{
Name: "rel",
Type: schema.FieldTypeRelation,
Options: &schema.RelationOptions{
MaxSelect: types.Pointer(10),
CollectionId: "c1",
CascadeDelete: false, // should unset all rel fields
},
},
)
if err := dao.SaveCollection(c2); err != nil {
return err
}
// create mock collection with a single-rel field
c3 := &models.Collection{}
c3.Id = "c3"
c3.Name = c3.Id
c3.Schema = schema.NewSchema(
&schema.SchemaField{
Name: "rel",
Type: schema.FieldTypeRelation,
Options: &schema.RelationOptions{
MaxSelect: types.Pointer(1),
CollectionId: "c1",
CascadeDelete: true, // should delete all c3 records
},
},
)
if err := dao.SaveCollection(c3); err != nil {
return err
}
// insert mock records
c1RecordA := models.NewRecord(c1)
c1RecordA.Id = "a"
c1RecordA.Set("rel", c1RecordA.Id) // self reference
if err := dao.Save(c1RecordA); err != nil {
return err
}
c1RecordB := models.NewRecord(c1)
c1RecordB.Id = "b"
c1RecordB.Set("rel", c1RecordA.Id) // rel to another record from the same collection
if err := dao.Save(c1RecordB); err != nil {
return err
}
for i := 0; i < 4500; i++ {
c2Record := models.NewRecord(c2)
c2Record.Set("rel", []string{c1RecordA.Id, c1RecordB.Id})
if err := dao.Save(c2Record); err != nil {
return err
}
c3Record := models.NewRecord(c3)
c3Record.Set("rel", c1RecordA.Id)
if err := dao.Save(c3Record); err != nil {
return err
}
}
// set the same id as the relation for at least 1 record
// to check whether the correct condition will be added
c3Record := models.NewRecord(c3)
c3Record.Set("rel", c1RecordA.Id)
c3Record.Id = c1RecordA.Id
if err := dao.Save(c3Record); err != nil {
return err
}
return nil
}