-
Notifications
You must be signed in to change notification settings - Fork 1
/
store_test.go
3605 lines (2987 loc) · 81.8 KB
/
store_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
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package bstore
import (
"bytes"
"encoding/binary"
"encoding/gob"
"errors"
"fmt"
"io/fs"
mathrand "math/rand"
"os"
"reflect"
"strconv"
"strings"
"testing"
"time"
bolt "go.etcd.io/bbolt"
)
func TestOpenOptions(t *testing.T) {
type User struct {
ID int
}
const path = "testdata/tmp.openoptions.db"
os.Remove(path)
_, err := Open(ctxbg, path, &Options{MustExist: true}, User{})
tneed(t, err, fs.ErrNotExist, "open with MustExist on absent file")
db, err := Open(ctxbg, path, &Options{Perm: 0700}, User{})
tcheck(t, err, "open")
fi, err := os.Stat(path)
tcheck(t, err, "stat")
if fi.Mode()&fs.ModePerm != 0700 {
t.Fatalf("mode of new db is %o, expected 0700", fi.Mode()&fs.ModePerm)
}
// not closing DB yet, for timeout
_, err = Open(ctxbg, path, &Options{Timeout: time.Second}, User{})
tneed(t, err, bolt.ErrTimeout, "open with timeout")
tclose(t, db)
db, err = Open(ctxbg, path, &Options{MustExist: true}, User{})
tcheck(t, err, "open with MustExist")
tclose(t, db)
}
func TestStore(t *testing.T) {
type Sub struct {
ID int
Email string
}
type Mapkey struct {
K1 int
K2 string
}
type Mapvalue struct {
Data []byte
Time *time.Time
}
type User struct {
ID int64
Name string `bstore:"unique"`
Registered time.Time `bstore:"nonzero"`
Byte byte
Int8 int8
Int16 int16
Int32 int32
Int64 int64
Uint64 uint64
Float32 float32
Float64 float64
String string
Bytes []byte
Struct Sub
Slice []string
Map map[string]struct{}
Map2 map[Mapkey]Mapvalue
Time time.Time
Array [2]int
Byteptr *byte
Int8ptr *int8
Int16ptr *int16
Int32ptr *int32
Int64ptr *int64
Uint64ptr *uint64
Float32ptr *float32
Float64ptr *float64
Stringptr *string
Bytesptr *[]byte
Structptr *Sub
Sliceptr *[]string
Mapptr *map[string]struct{}
Map2ptr *map[Mapkey]Mapvalue
Timeptr *time.Time
Arrayptr *[2]int
Ignore int `bstore:"-"`
private int
}
tv, err := gatherTypeVersion(reflect.TypeOf(User{}))
tcheck(t, err, "gatherinTypeVersions")
if tv.name != "User" {
t.Fatalf("name %q, expected User", tv.name)
}
tv.Version = 1
st := storeType{tv.name, reflect.TypeOf(User{}), tv, map[uint32]*typeVersion{tv.Version: tv}}
now := time.Now().Round(0) // time without monotonic time, for later deepequal comparison
u := User{
ID: 123,
Name: "mjl",
Registered: now,
Ignore: 2,
private: 4,
}
ubuf, err := st.pack(reflect.ValueOf(u))
tcheck(t, err, "pack")
err = st.parse(reflect.ValueOf(&User{}).Elem(), ubuf)
tcheck(t, err, "parse")
const path = "testdata/tmp.test.db"
os.Remove(path)
db, err := topen(t, path, nil, User{})
tcheck(t, err, "open")
defer tclose(t, db)
err = db.Write(ctxbg, func(tx *Tx) error {
nu := User{Registered: now}
err := tx.Insert(&nu)
tcheck(t, err, "insert new user")
if nu.ID == 0 {
t.Fatalf("no id assigned for new record")
}
u = User{
ID: 123,
Name: "mjl",
Registered: now,
Byte: 'a',
Int8: 20,
Int16: 1000,
Int32: 1 << 20,
Int64: -1,
Uint64: ^uint64(0),
Float32: 1.123,
Float64: 0.000001,
String: "test",
Bytes: []byte("hi"),
Struct: Sub{1, "[email protected]"},
Slice: []string{"a", "b"},
Time: now,
Map: map[string]struct{}{"a": {}},
Map2: map[Mapkey]Mapvalue{
{1, "a"}: {[]byte("test"), nil},
{1, "b"}: {nil, &now},
{2, "a"}: {nil, nil},
{3, "d"}: {[]byte("hi"), &now},
},
Array: [2]int{-1, 1},
}
err = tx.Insert(&u)
tcheck(t, err, "insert user")
err = tx.Insert(&u)
tneed(t, err, ErrUnique, "insert dup")
tcompare(t, nil, u.ID, int64(123), "id unchanged")
uptrs := User{
Name: "mjl2",
Registered: now,
Byteptr: &u.Byte,
Int8ptr: &u.Int8,
Int16ptr: &u.Int16,
Int32ptr: &u.Int32,
Int64ptr: &u.Int64,
Uint64ptr: &u.Uint64,
Float32ptr: &u.Float32,
Float64ptr: &u.Float64,
Stringptr: &u.String,
Bytesptr: &u.Bytes,
Structptr: &u.Struct,
Sliceptr: &u.Slice,
Mapptr: &u.Map,
Map2ptr: &u.Map2,
Timeptr: &u.Time,
Arrayptr: &u.Array,
}
err = tx.Insert(&uptrs)
tcheck(t, err, "inserting user with pointers")
u2 := User{ID: u.ID + 999}
err = tx.Get(&u2)
tneed(t, err, ErrAbsent, "fetching non-existing id")
err = tx.Get(nil)
tneed(t, err, ErrParam, "nil get")
err = tx.Insert(nil)
tneed(t, err, ErrParam, "nil insert")
u.Name = "nmjl"
err = tx.Update(&u)
tcheck(t, err, "update")
err = tx.Update(&User{ID: u.ID + 999})
tneed(t, err, ErrAbsent, "update on absent record")
err = tx.Update(nil)
tneed(t, err, ErrParam, "nil update")
u2 = User{ID: u.ID}
err = tx.Get(&u2)
tcheck(t, err, "get")
if u2.Name != "nmjl" {
t.Fatalf("save did not update")
}
nuptrs := User{ID: uptrs.ID}
err = tx.Get(&nuptrs)
tcompare(t, err, nuptrs, uptrs, "get of user with pointers")
err = tx.Delete(&u)
tcheck(t, err, "delete")
err = tx.Delete(&u)
tneed(t, err, ErrAbsent, "deleting non-existing id")
err = tx.Delete(nil)
tneed(t, err, ErrParam, "nil delete")
err = tx.Delete((*User)(nil))
tneed(t, err, ErrParam, "nil delete ptr")
err = tx.Delete("bogus")
tneed(t, err, ErrParam, "delete on non-struct/structptr")
u3 := User{Name: "unique1"}
err = tx.Insert(&u3)
tneed(t, err, ErrZero, "inserting with zero value")
tcompare(t, nil, u3.ID, int64(0), "zero id")
return nil
})
tcheck(t, err, "write")
n, err := QueryDB[User](ctxbg, db).Count()
tcheck(t, err, "count")
if n != 2 {
t.Fatalf("got %d records, expected 2", n)
}
}
func TestBoolptr(t *testing.T) {
type Boolptr struct {
ID uint32
Value *bool
}
type Boolptr2 struct {
ID uint32 `bstore:"typename Boolptr"`
Value bool
}
type Boolptr3 struct {
ID uint32 `bstore:"typename Boolptr"`
}
const path = "testdata/tmp.boolptr.db"
os.Remove(path)
db, err := topen(t, path, nil, Boolptr{})
tcheck(t, err, "open")
var n, tr, f Boolptr
xtrue := true
xfalse := false
err = db.Write(ctxbg, func(tx *Tx) error {
n = Boolptr{Value: nil}
err := tx.Insert(&n)
tcheck(t, err, "insert nil")
tr = Boolptr{Value: &xtrue}
err = tx.Insert(&tr)
tcheck(t, err, "insert true ptr")
f = Boolptr{Value: &xfalse}
err = tx.Insert(&f)
tcheck(t, err, "insert false ptr")
n = Boolptr{ID: n.ID}
err = tx.Get(&n)
tcompare(t, err, n, Boolptr{ID: n.ID}, "nil boolptr")
tr = Boolptr{ID: tr.ID}
err = tx.Get(&tr)
tcompare(t, err, tr, Boolptr{ID: tr.ID, Value: &xtrue}, "true boolptr")
f = Boolptr{ID: f.ID}
err = tx.Get(&f)
tcompare(t, err, f, Boolptr{ID: f.ID, Value: &xfalse}, "false boolptr")
return nil
})
tcheck(t, err, "write")
tclose(t, db)
db, err = topen(t, path, nil, Boolptr2{})
tcheck(t, err, "open")
err = db.Write(ctxbg, func(tx *Tx) error {
n2 := Boolptr2{ID: n.ID}
err = tx.Get(&n2)
tcompare(t, err, n2, Boolptr2{ID: n.ID}, "nil")
tr2 := Boolptr2{ID: tr.ID}
err = tx.Get(&tr2)
tcompare(t, err, tr2, Boolptr2{ID: tr.ID, Value: true}, "true")
f2 := Boolptr2{ID: f.ID}
err = tx.Get(&f2)
tcompare(t, err, f2, Boolptr2{ID: f.ID}, "false")
for _, e := range []*Boolptr2{&n2, &tr2, &f2} {
err = tx.Delete(e)
tcheck(t, err, "delete")
err = tx.Insert(e)
tcheck(t, err, "insert")
}
return nil
})
tcheck(t, err, "write")
tclose(t, db)
db, err = topen(t, path, nil, Boolptr{})
tcheck(t, err, "open")
err = db.Write(ctxbg, func(tx *Tx) error {
n = Boolptr{ID: n.ID}
err = tx.Get(&n)
tcompare(t, err, n, Boolptr{ID: n.ID}, "nil")
tr = Boolptr{ID: tr.ID}
err = tx.Get(&tr)
tcompare(t, err, tr, Boolptr{ID: tr.ID, Value: &xtrue}, "true")
f = Boolptr{ID: f.ID}
err = tx.Get(&f)
tcompare(t, err, f, Boolptr{ID: f.ID}, "false") // Now nil...
f.Value = &xfalse
// Insert again, so we can test skipping the fields in the next read.
for _, e := range []*Boolptr{&n, &tr, &f} {
err = tx.Delete(e)
tcheck(t, err, "delete")
err = tx.Insert(e)
tcheck(t, err, "insert")
}
return nil
})
tcheck(t, err, "write")
tclose(t, db)
db, err = topen(t, path, nil, Boolptr3{})
tcheck(t, err, "open")
err = db.Write(ctxbg, func(tx *Tx) error {
n3 := Boolptr3{ID: n.ID}
err = tx.Get(&n3)
tcompare(t, err, n3, Boolptr3{ID: n.ID}, "nil")
tr3 := Boolptr3{ID: tr.ID}
err = tx.Get(&tr3)
tcompare(t, err, tr3, Boolptr3{ID: tr.ID}, "true")
f3 := Boolptr3{ID: f.ID}
err = tx.Get(&f3)
tcompare(t, err, f3, Boolptr3{ID: f.ID}, "false")
return nil
})
tcheck(t, err, "write")
tclose(t, db)
}
func TestRegister(t *testing.T) {
type OK struct {
ID int
}
type Empty struct {
}
type Private1 struct {
id int // First field must be PK.
ID int
}
type Private2 struct {
ID int `bstore:"-"` // Cannot skip first field.
}
type Noauto struct {
ID int
Text string `bstore:"noauto"` // noauto is only for PK.
}
type Badtag struct {
ID int `bstore:"bogus"`
}
type Badindex1 struct {
ID int
Name string `bstore:"index ID"` // Must start with own field.
}
type Badindex2 struct {
ID int
Name string `bstore:"index Name Name Superfluous"` // One word too many.
}
type Badindex3 struct {
ID int
Name string `bstore:"index Name+Absent"` // Field does not exist.
}
type BadPKPtr struct {
ID *int
}
type BadIndexPtr struct {
ID int
Name *string `bstore:"index"`
}
type BadIndexDupfield struct {
ID int
Name *string `bstore:"index Name+Name"`
}
type BadPKNonzero struct {
ID int `bstore:"nonzero"` // Superfluous nonzero tag.
}
type BadRefA struct {
ID int
XID int `bstore:"ref BadRefB,ref BadRefB"`
}
type BadRefB struct {
ID int
}
type Embed struct {
ID int
}
type BadEmbed struct {
Embed
}
type BadEmbedNonzero struct {
PK int
Embed `bstore:"nonzero"`
}
type BadEmbedIgnore struct {
PK int
Embed `bstore:"-"`
}
type BadName struct {
ID int `bstore:"name "`
}
type BadTypeDefault struct {
ID int
Field Embed `bstore:"default xyz"`
}
type Badptrptr struct {
ID int
Ptrptr **int
}
type Badptrptrslice struct {
ID int
Slice []**int
}
type Badptrptrstruct struct {
ID int
Struct **Embed
}
type Badptrptrmap2 struct {
ID int
Map map[int]**int
}
type Badptrmapkey struct {
ID int
Map map[*int]int
}
const path = "testdata/tmp.register.db"
os.Remove(path)
_, err := topen(t, path, nil, "not a struct")
tneed(t, err, ErrParam, "bad type")
_, err = topen(t, path, nil, OK{}, OK{})
tneed(t, err, ErrParam, "duplicate name")
_, err = topen(t, path, nil, &OK{})
tneed(t, err, ErrParam, "pointer")
_, err = topen(t, path, nil, Empty{})
tneed(t, err, ErrType, "no PK")
_, err = topen(t, path, nil, struct{}{})
tneed(t, err, ErrType, "no PK")
_, err = topen(t, path, nil, Private1{id: 1})
tneed(t, err, ErrType, "bad Private1")
_, err = topen(t, path, nil, Private2{})
tneed(t, err, ErrType, "bad Private2")
_, err = topen(t, path, nil, Noauto{})
tneed(t, err, ErrType, "bad Noauto")
_, err = topen(t, path, nil, Badtag{})
tneed(t, err, ErrType, "bad Badtag")
_, err = topen(t, path, nil, Badindex1{})
tneed(t, err, ErrType, "bad Badindex1")
_, err = topen(t, path, nil, Badindex2{})
tneed(t, err, ErrType, "bad Badindex2")
_, err = topen(t, path, nil, Badindex3{})
tneed(t, err, ErrType, "bad Badindex3")
_, err = topen(t, path, nil, BadPKPtr{})
tneed(t, err, ErrType, "bad BadPKPtr")
_, err = topen(t, path, nil, BadIndexPtr{})
tneed(t, err, ErrType, "bad BadIndexPtr")
_, err = topen(t, path, nil, BadIndexDupfield{})
tneed(t, err, ErrType, "bad BadIndexDupfield")
_, err = topen(t, path, nil, BadPKNonzero{})
tneed(t, err, ErrType, "bad BadPKNonzero")
_, err = topen(t, path, nil, BadRefA{}, BadRefB{})
tneed(t, err, ErrType, "bad dup ref")
_, err = topen(t, path, nil, BadEmbed{})
tneed(t, err, ErrType, "bad embed for Pk")
_, err = topen(t, path, nil, BadEmbedNonzero{})
tneed(t, err, ErrType, "bad nonzero tag on embed")
_, err = topen(t, path, nil, BadEmbedIgnore{})
tneed(t, err, ErrType, "bad ignore for embed")
_, err = topen(t, path, nil, BadName{})
tneed(t, err, ErrType, "bad name on field")
_, err = topen(t, path, nil, BadTypeDefault{})
tneed(t, err, ErrType, "bad type with default")
_, err = topen(t, path, nil, Badptrptr{})
tneed(t, err, ErrType, "bad type with ptr to ptr")
_, err = topen(t, path, nil, Badptrptrslice{})
tneed(t, err, ErrType, "bad type with ptr to ptr")
_, err = topen(t, path, nil, Badptrptrstruct{})
tneed(t, err, ErrType, "bad type with ptr to ptr")
_, err = topen(t, path, nil, Badptrptrmap2{})
tneed(t, err, ErrType, "bad type with ptr to ptr")
_, err = topen(t, path, nil, Badptrmapkey{})
tneed(t, err, ErrType, "bad type with ptr key in map")
}
func TestUnique(t *testing.T) {
type User struct {
ID int
Name string `bstore:"unique"`
}
const path = "testdata/tmp.unique.db"
os.Remove(path)
db, err := topen(t, path, nil, User{})
tcheck(t, err, "open")
defer tclose(t, db)
err = db.Write(ctxbg, func(tx *Tx) error {
a := User{Name: "a"}
b := User{Name: "b"}
dup := User{Name: "a"}
err := tx.Insert(&a)
tcheck(t, err, "insert")
err = tx.Insert(&b)
tcheck(t, err, "insert")
return tx.Insert(&dup)
})
tneed(t, err, ErrUnique, "inserting with existing key")
err = db.Write(ctxbg, func(tx *Tx) error {
a := User{Name: "a"}
b := User{Name: "b"}
err := tx.Insert(&a)
tcheck(t, err, "insert")
err = tx.Insert(&b)
tcheck(t, err, "insert")
b.Name = "a"
return tx.Update(&b)
})
tneed(t, err, ErrUnique, "updating with existing key")
err = db.Write(ctxbg, func(tx *Tx) error {
return tx.Insert(&User{Name: "test\u0000"})
})
tneed(t, err, ErrParam, "cannot have string with zero byte")
}
func TestReference(t *testing.T) {
type User struct {
ID int
Name string
GroupID int `bstore:"ref Group"`
}
type Group struct {
ID int
Name string
}
type Group2 struct {
ID uint `bstore:"typename Group"`
Name string
}
const path = "testdata/tmp.reference.db"
os.Remove(path)
_, err := topen(t, path, nil, User{})
tneed(t, err, ErrType, "missing reference type")
_, err = topen(t, path, nil, User{}, Group2{})
tneed(t, err, ErrType, "reference field type mismatch")
db, err := topen(t, path, nil, User{}, Group{})
tcheck(t, err, "open")
defer tclose(t, db)
err = db.Write(ctxbg, func(tx *Tx) error {
a := User{}
// This is fine because GroupID has the zero value.
err := tx.Insert(&a)
tcheck(t, err, "insert")
err = tx.Insert(&User{GroupID: 999})
tneed(t, err, ErrReference, "inserting with reference to non-existing id")
err = tx.Insert(&Group{ID: 999})
tcheck(t, err, "insert group")
// Same user insert as before, now it is valid.
u := &User{GroupID: 999}
err = tx.Insert(u)
tcheck(t, err, "insert user")
// Cannot remove group yet, user is still referencing it.
err = tx.Delete(&Group{ID: 999})
tneed(t, err, ErrReference, "removing record that is still referenced")
err = tx.Delete(u)
tcheck(t, err, "delete user")
err = tx.Delete(&Group{ID: 999})
tcheck(t, err, "delete group")
return nil
})
tcheck(t, err, "db update")
}
func TestCreateIndex(t *testing.T) {
type User struct {
ID int
Name string
}
// Now with index.
type User2 struct {
ID int `bstore:"typename User"`
Name string `bstore:"unique"`
}
const path = "testdata/tmp.createindex.db"
os.Remove(path)
db, err := topen(t, path, nil, User{})
tcheck(t, err, "open")
u0 := User{Name: "a"}
u1 := User{Name: "a"}
err = db.Write(ctxbg, func(tx *Tx) error {
err := tx.Insert(&u0)
tcheck(t, err, "insert a")
err = tx.Insert(&u1)
tcheck(t, err, "insert a")
return nil
})
tcheck(t, err, "write")
tclose(t, db)
_, err = topen(t, path, nil, User2{})
tneed(t, err, ErrUnique, "open with new unique index with duplicate value")
os.Remove(path)
db, err = topen(t, path, nil, User{})
tcheck(t, err, "open")
u0 = User{Name: "a"}
u1 = User{Name: "b"}
err = db.Insert(ctxbg, &u0)
tcheck(t, err, "insert u0")
err = db.Insert(ctxbg, &u1)
tcheck(t, err, "insert u1")
tclose(t, db)
db, err = topen(t, path, nil, User2{})
tcheck(t, err, "open")
var ids []int
err = QueryDB[User2](ctxbg, db).FilterNonzero(User2{Name: "a"}).IDs(&ids)
tcompare(t, err, ids, []int{u0.ID}, "list")
tclose(t, db)
}
func TestTypeVersions(t *testing.T) {
type User struct {
ID int
Name string
}
type User2 struct {
ID int `bstore:"typename User"`
Name string `bstore:"unique"`
}
type User3 struct {
ID int `bstore:"typename User"`
Name string `bstore:"nonzero,unique"`
}
type User4 struct {
ID int `bstore:"typename User"`
Name string `bstore:"nonzero,unique"`
Age int
}
type User5 struct {
ID int `bstore:"typename User"`
Name string `bstore:"default test"`
}
type User6 struct {
ID int `bstore:"typename User"`
Name string `bstore:"default test"`
GroupID int `bstore:"ref Group"`
}
type Group struct {
ID int
}
const path = "testdata/tmp.typeversions.db"
os.Remove(path)
db, err := topen(t, path, nil, User{})
tcheck(t, err, "open")
reopen := func(v ...any) {
t.Helper()
var err error
tclose(t, db)
// Register a different type. Insert the other type. List the two records with different typeVersions.
db, err = topen(t, path, nil, v...)
tcheck(t, err, "open")
}
checkTypes := func(exp string) {
err := db.bdb.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("User"))
if b == nil {
t.Fatalf("missing bucket User")
}
tb := b.Bucket([]byte("types"))
if tb == nil {
t.Fatalf("missing types bucket")
}
var versions []string
err := tb.ForEach(func(bk, bv []byte) error {
version := binary.BigEndian.Uint32(bk)
versions = append(versions, fmt.Sprintf("%d", version))
return nil
})
tcheck(t, err, "reading types")
if strings.Join(versions, ",") != exp {
t.Fatalf(`got typeversions %q, expected %q`, strings.Join(versions, ","), exp)
}
return nil
})
tcheck(t, err, "view")
}
checkTypes("1")
reopen(User{})
checkTypes("1")
u := User{Name: "hi"}
err = db.Insert(ctxbg, &u)
tcheck(t, err, "insert user")
reopen(User2{})
checkTypes("1,2")
err = db.Write(ctxbg, func(tx *Tx) error {
u := User2{Name: "hi2"}
if err := tx.Insert(&u); err != nil {
t.Fatalf("inserting user: %v", err)
}
_, err := QueryTx[User2](tx).List()
tcheck(t, err, "list")
return nil
})
tcheck(t, err, "insert/list second user")
reopen(User{})
checkTypes("1,2,3")
reopen(User2{})
checkTypes("1,2,3,4")
_, err = QueryDB[User2](ctxbg, db).List()
tcheck(t, err, "all")
reopen(User3{})
checkTypes("1,2,3,4,5")
reopen(User4{})
checkTypes("1,2,3,4,5,6")
reopen(User{})
checkTypes("1,2,3,4,5,6,7")
reopen(User5{})
checkTypes("1,2,3,4,5,6,7,8")
reopen(User6{}, Group{})
checkTypes("1,2,3,4,5,6,7,8,9")
tclose(t, db)
}
func TestInsertSeqdup(t *testing.T) {
type User struct {
ID int
}
const path = "testdata/tmp.insertseqdup.db"
os.Remove(path)
db, err := topen(t, path, nil, User{})
tcheck(t, err, "open")
defer tclose(t, db)
err = db.Write(ctxbg, func(tx *Tx) error {
u1 := User{ID: int(tx.btx.Bucket([]byte("User")).Bucket([]byte("records")).Sequence() + 1)}
err := tx.Insert(&u1)
tcheck(t, err, "insert")
u2 := User{}
err = tx.Insert(&u2)
// This now works because we increase the sequence number when user inserts a higher id then current sequence.
tcheck(t, err, "inserting record where next sequence seems already present")
return nil
})
tcheck(t, err, "write")
}
func TestRemoveNoautoSeq(t *testing.T) {
type User struct {
ID int `bstore:"noauto"`
}
type User2 struct {
ID int `bstore:"typename User"`
}
const path = "testdata/tmp.removenoautoseq.db"
os.Remove(path)
db, err := topen(t, path, nil, User{})
tcheck(t, err, "open")
defer tclose(t, db)
var u0, u1 User
var seq0 int
err = db.Write(ctxbg, func(tx *Tx) error {
err := tx.btx.Bucket([]byte("User")).Bucket([]byte("records")).SetSequence(1)
tcheck(t, err, "setsequence")
seq0 = 1
u0 = User{ID: seq0}
u1 = User{ID: seq0 + 1}
err = tx.Insert(&u0, &u1)
tcheck(t, err, "insert")
return nil
})
tcheck(t, err, "write")
tclose(t, db)
// Reopening with User2 (without "noauto") should set the sequence past the highest sequence.
db, err = topen(t, path, nil, User2{})
tcheck(t, err, "open")
defer tclose(t, db)
var seq1 int
err = db.Write(ctxbg, func(tx *Tx) error {
seq1 = int(tx.btx.Bucket([]byte("User")).Bucket([]byte("records")).Sequence())
if seq1 == seq0 {
t.Fatalf("sequence did not change %d", seq1)
}
err := tx.Insert(&User2{})
tcheck(t, err, "insert")
return nil
})
tcheck(t, err, "write")
}
func TestPtrZero(t *testing.T) {
type Sub struct {
Name string
}
type User struct {
ID int
String *string
Sub *Sub
}
const path = "testdata/tmp.ptrzero.db"
os.Remove(path)
db, err := topen(t, path, nil, User{})
tcheck(t, err, "open")
defer tclose(t, db)
err = db.Write(ctxbg, func(tx *Tx) error {
u0 := User{}
err := tx.Insert(&u0)
tcheck(t, err, "insert")
var str string
var sub Sub
u1 := User{String: &str, Sub: &sub}
err = tx.Insert(&u1)
tcheck(t, err, "insert")
s2 := "hi"
sub2 := Sub{Name: "hi"}
u2 := User{String: &s2, Sub: &sub2}
err = tx.Insert(&u2)
tcheck(t, err, "insert")
x0 := User{ID: u0.ID}
err = tx.Get(&x0)
tcompare(t, err, x0, u0, "get")
x1 := User{ID: u1.ID}
err = tx.Get(&x1)
tcompare(t, err, x1, u1, "get")
x2 := User{ID: u2.ID}
err = tx.Get(&x2)
tcompare(t, err, x2, u2, "get")
return nil
})
tcheck(t, err, "write")
}
func TestIDTypes(t *testing.T) {