-
Notifications
You must be signed in to change notification settings - Fork 1
/
minidb_test.go
1048 lines (996 loc) · 29 KB
/
minidb_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 minidb
import (
"bytes"
"encoding/base64"
"fmt"
"io/ioutil"
"math/rand"
"os"
"testing"
"time"
)
var db *MDB
var tmpfile *os.File
var tmpfile2 *os.File
func TestToBaseType(t *testing.T) {
tables := []struct {
in FieldType
out FieldType
}{
{DBIntList, DBInt},
{DBStringList, DBString},
{DBBlobList, DBBlob},
{DBDateList, DBDate},
}
for _, table := range tables {
result := ToBaseType(table.in)
if result != table.out {
t.Errorf("ToBaseType(%d) incorrect result, expected %d, given %d", table.in, table.out, result)
}
}
if ToBaseType(DBError) != DBError {
t.Errorf("ToBaseType(%d) incorrect result, expected %d, given %d", DBError, DBError, ToBaseType(DBError))
}
}
func TestFail(t *testing.T) {
var result error
result = Fail("This is a test %d %s %s", 42, "Hello", "World")
_ = result
}
func TestValue(t *testing.T) {
// integers
v := NewInt(2363867)
if v.Int() != 2363867 {
t.Errorf("NewInt(2363867) failed.")
}
v = NewInt(0)
if v.Int() != 0 {
t.Errorf("NewInt(0) failed.")
}
v = NewInt(-1)
if v.Int() != -1 {
t.Errorf("NewInt(-1) failed.")
}
v = NewInt(9223372036854775807)
if v.Int() != 9223372036854775807 {
t.Errorf("NewInt(9223372036854775807) failed.")
}
v = NewInt(-9223372036854775808)
if v.Int() != -9223372036854775808 {
t.Errorf("NewInt(-9223372036854775808) failed.")
}
if v.Sort != DBInt {
t.Errorf("value of type int64 has wrong type, should be %d, given %d", DBInt, v.Sort)
}
b := v.Bytes()
if b == nil {
t.Errorf("Int64 Value cannot be converted to bytes.")
}
if !bytes.Equal(b, []byte("\x00\x00\x00\x00\x00\x00\x00\x80")) {
t.Errorf("Int64 conversion to Bytes failed, given %q, expected %q", b, []byte("\x00\x00\x00\x00\x00\x00\x00\x80"))
}
if v.String() != "-9223372036854775808" {
t.Errorf("Bytes to String conversion failed in Value.String(), expected '%q', given '%q'", "-9223372036854775808", v.String())
}
// strings
v = NewString("")
if v.String() != "" {
t.Errorf("NewString failed.")
}
v = NewString("Hello world")
if v.String() != "Hello world" {
t.Errorf("NewString failed.")
}
v = NewString("Testing «ταБЬℓσ»: 1<2 & 4+1>3, now 20% off!")
if v.String() != "Testing «ταБЬℓσ»: 1<2 & 4+1>3, now 20% off!" {
t.Errorf("NewString unicode test failed.")
}
if !bytes.Equal(v.Bytes(), []byte(v.String())) {
t.Errorf("Cannot convert Value of type String to Bytes, given %q, expected %q", []byte(v.String()), v.Bytes())
}
if v.Sort != DBString {
t.Errorf("value of string type has wrong type, should be %d, given %d", DBString, v.Sort)
}
// bytes
v = NewBytes([]byte("Hello world"))
expect := base64.StdEncoding.EncodeToString(v.Bytes())
if v.String() != expect {
t.Errorf("NewBytes failed, given '%s', expected '%s'", v.String(), expect)
}
if !bytes.Equal(v.Bytes(), []byte("Hello world")) {
t.Errorf("NewBytes failed, not the same bytes array is stored than what was passed as an argument")
}
v = NewBytes([]byte(""))
if v.Bytes() == nil {
t.Errorf("NewBytes failed, empty bytes should be != nil")
}
// dates
d := time.Now()
v = NewDate(d)
if d.UTC().Format(time.RFC3339) != v.String() {
t.Errorf("NewDate failed, expected %s, given %s", d.UTC().Format(time.RFC3339), v.String())
}
v = NewDateStr("2018-12-24T18:00:00Z")
if v.String() != "2018-12-24T18:00:00Z" {
t.Errorf("NewDateStr failed, expected %s, given %s", "2018-12-24T18:00:00Z", v.String())
}
v = NewDateStr("2002-10-02T10:00:00-05:00")
if v.String() != "2002-10-02T10:00:00-05:00" {
t.Errorf("NewDateStr failed, expected %s, given %s", "2002-10-02T10:00:00-05:00", v.String())
}
defer func() {
if r := recover(); r != nil {
t.Errorf("NewDateStr failed, invalid input should have raised panic but passed.")
}
}()
v = NewDateStr("2002-10-02 10:00:00Z")
v = NewDateStr("2002-10-02T10:00:00")
}
func TestGetUserTypeString(t *testing.T) {
tables := []struct {
in FieldType
out string
}{
{DBIntList, "int-list"},
{DBStringList, "string-list"},
{DBBlobList, "blob-list"},
{DBDateList, "date-list"},
{DBInt, "int"},
{DBString, "string"},
{DBBlob, "blob"},
{DBDate, "date"},
}
for _, table := range tables {
result := GetUserTypeString(table.in)
if result != table.out {
t.Errorf("GetUserTypeString(%d) incorrect result, expected %s, given %s", table.in, table.out, result)
}
}
if GetUserTypeString(DBError) != "unknown" {
t.Errorf("GetUserTypeString(%d) incorrect result, expected 'unknown', given '%s'", DBError, GetUserTypeString(DBError))
}
}
func TestParseFieldDesc(t *testing.T) {
tables := []struct {
in []string
out []Field
}{
{[]string{"int", "Age"}, []Field{Field{"Age", DBInt}}},
{[]string{"string", "Name", "string-list", "Address"},
[]Field{Field{"Name", DBString}, Field{"Address", DBStringList}}},
{[]string{"date-list", "Meetings"}, []Field{Field{"Meetings", DBDateList}}},
{[]string{"blob", "foo"}, []Field{Field{"foo", DBBlob}}},
}
for _, table := range tables {
result, err := ParseFieldDesc(table.in)
if err != nil {
t.Errorf("ParseFieldDesc() failed on table test")
}
for i := range table.out {
if table.out[i] != result[i] {
t.Errorf("ParseFieldDesc(%s) incorrect result", table.in)
}
}
}
}
func TestMDB(t *testing.T) {
var tx *Tx
db, err := Open("sqlite3", tmpfile.Name())
if err != nil {
t.Errorf("Open() failed: %s", err)
}
// test code here
err = db.AddTable("test", []Field{
Field{"Name", DBStringList},
Field{"Email", DBString},
Field{"Age", DBInt},
Field{"Scores", DBIntList},
Field{"Modified", DBDate},
Field{"Misc", DBBlob},
Field{"Data", DBBlobList},
Field{"Schedules", DBDateList},
})
if err != nil {
t.Errorf("AddTable() failed: %s", err)
}
if !db.TableExists("test") {
t.Errorf("tx.TableExists() failed, should be true, given false")
}
if db.TableExists("humpty") {
t.Errorf("tx.TableExists() returned true instead of false for nonexistent table")
}
if db.ItemExists("test", 1) {
t.Errorf("tx.ItemExists() returned true instead of false for table 'test'")
}
item, err := db.NewItem("test")
if err != nil {
t.Errorf("db.NewItem() failed for table 'test'")
}
if !db.ItemExists("test", item) {
t.Errorf("ItemExists() returned false for existing item %d, expected true", item)
}
item2, err := db.UseItem("test", 299)
if err != nil {
t.Errorf("db.UseItem() failed for new item 299")
}
tx, err = db.Begin()
if err != nil {
t.Errorf("db.Begin() failed: %v", err)
}
err = tx.Set("test", item2, "Name", []Value{NewString("Johannes Hacker")})
if err != nil {
t.Errorf("db.Set() on item 299 failed")
}
if err := tx.Commit(); err != nil {
t.Errorf("db.Commit() failed: %v", err)
}
it1, err := db.Get("test", 299, "Name")
if err != nil {
t.Errorf("db.Get() failed for used item 299: %v", err)
}
if len(it1) != 1 {
t.Errorf("db.Get() returned the wrong number of results for 299 Name")
}
if it1[0].String() != "Johannes Hacker" {
t.Errorf("db.Get() returned wrong 299 Name, got '%s' expected 'Johannes Hacker'", it1[0].String())
}
item3, err := db.UseItem("test", 299)
if err != nil {
t.Errorf("db.UseItem() failed for existing item 299!")
}
it2, err := db.Get("test", item3, "Name")
if err != nil {
t.Errorf("db.Get() failed for used item 299: %v", err)
}
if len(it2) != 1 {
t.Errorf("db.Get() returned the wrong number of results for 299 Name")
}
if it2[0].String() != "Johannes Hacker" {
t.Errorf("db.Get() returned wrong 299 Name, got '%s' expected 'Johannes Hacker'", it2[0].String())
}
tx, err = db.Begin()
if err != nil {
t.Errorf("db.Begin() failed: %v", err)
}
if err := tx.RemoveItem("test", 299); err != nil {
t.Errorf("tx.RemoveItem() failed: %v", err)
}
err = tx.Commit()
if err != nil {
t.Errorf("db.Commit() failed: %v", err)
}
for _, field := range []string{"Name", "Email", "Age", "Scores", "Modified", "Misc", "Data", "Schedules"} {
if !db.FieldExists("test", field) {
t.Errorf("FieldExists(%s) is false, but should be true", field)
}
var c FieldType
k := db.MustGetFieldType("test", field)
switch field {
case "Name":
c = DBStringList
case "Email":
c = DBString
case "Age":
c = DBInt
case "Scores":
c = DBIntList
case "Modified":
c = DBDate
case "Misc":
c = DBBlob
case "Data":
c = DBBlobList
case "Schedules":
c = DBDateList
}
if k != c {
t.Errorf("MustGetFieldType() unexpected value, expected %d, given %d", c, k)
}
if !db.FieldIsNull("test", item, field) {
t.Errorf("FieldIsNull() returned true instead of false for NULL field '%s'", field)
}
if !db.FieldIsEmpty("test", item, field) {
t.Errorf("FieldIsEmpty() returned true instead of false for NULL field '%s'", field)
}
}
if db.ItemExists("test", 99) {
t.Errorf("ItemExists() returns true for fictitious item 99 (did the driver just assign this as first id?)")
}
if db.FieldExists("test", "blurbfoo") {
t.Errorf("FieldExists() returns true but should return false for nonexistent field")
}
if db.FieldExists("schmoo", "Name") {
t.Errorf("FieldExists() returns true but should return false for fictitious field in nonexistent table")
}
if db.IsListField("test", "Email") {
t.Errorf("IsListField() returns true for non-list field")
}
if !db.IsListField("test", "Name") {
t.Errorf("IsListField() returns false for list field, should be true")
}
if db.IsListField("test", "schmoo") {
t.Errorf("IsListField() returns true for nonexistent field, should be false")
}
if db.IsListField("schmmoo", "what") {
t.Errorf("IsListField() returns true for nonexistent field in nonexistent table, should be false")
}
if !db.IsEmptyListField("test", 1, "Name") {
t.Errorf("IsEmptyListField() returns false for empty list field")
}
if db.IsEmptyListField("test", 1, "Email") {
t.Errorf("IsEmptyListField() returns true for non list field")
}
if !db.FieldExists("test", "Email") {
t.Errorf("FieldExists() returns false for an existing field")
}
if !db.FieldExists("test", "Name") {
t.Errorf("FieldExists() returns false for an existing list field")
}
if db.FieldExists("test", "schmoo") {
t.Errorf("FieldExists() returns true for a nonexistent field")
}
if db.FieldExists("schmoo", "Name") {
t.Errorf("FieldExists() returns true for a field in a nonexistent table")
}
_, err = db.ParseFieldValues("schmoo", "test", []string{})
if err == nil {
t.Errorf("ParseFieldValues() returns no error for nonexistent table")
}
_, err = db.ParseFieldValues("test", "Name", []string{"John", "Theodore", "Smith"})
if err != nil {
t.Errorf("ParseFieldValues() returns error for correct input")
}
_, err = db.ParseFieldValues("test", "Name", []string{""})
if err != nil {
t.Errorf("ParseFieldValues() returns error for correct input")
}
_, err = db.ParseFieldValues("test", "Name", []string{})
if err == nil {
t.Errorf("ParseFieldValues() returns no error for empty input, should indicate an error")
}
_, err = db.ParseFieldValues("test", "Age", []string{"-1234"})
if err != nil {
t.Errorf("ParseFieldValues() returns error for correct numeric input")
}
_, err = db.ParseFieldValues("test", "Email", []string{"john", "hello"})
if err == nil {
t.Errorf("ParseFieldValues() returns no error for too long input, should indicate an error")
}
_, err = db.ParseFieldValues("test", "Age", []string{"0dude"})
if err == nil {
t.Errorf("ParseFieldValues() returns no error for incorrect numeric input, should indicate an error")
}
_, err = db.ParseFieldValues("test", "Scores", []string{"27", "23.3", "7"})
if err == nil {
t.Errorf("ParseFieldValues() returns no error for incorrect numeric input")
}
_, err = db.ParseFieldValues("test", "Scores", []string{"27", "23", "7"})
if err != nil {
t.Errorf("ParseFieldValues() returns an error for correct numeric input")
}
_, err = db.ParseFieldValues("test", "Misc", []string{"SGVsbG8gd29ybGQh"})
if err != nil {
t.Errorf("ParseFieldValues() returns an error for correct blob input")
}
_, err = db.ParseFieldValues("test", "Misc", []string{"SGVXsbG8gd29ybGQh"})
if err == nil {
t.Errorf("ParseFieldValues() returns no error for incorrect blob input")
}
_, err = db.ParseFieldValues("test", "Modified", []string{"1999-12-31T01:00:00Z"})
if err != nil {
t.Errorf("ParseFieldValues() returns error for correct date input")
}
_, err = db.ParseFieldValues("test", "Modified", []string{"1999-12-31T01:00:00+05:00"})
if err != nil {
t.Errorf("ParseFieldValues() returns error for correct date input")
}
_, err = db.ParseFieldValues("test", "Modified", []string{"1999-12-31T01:00:00"})
if err == nil {
t.Errorf("ParseFieldValues() returns no error for incorrect date input")
}
_, err = db.ParseFieldValues("test", "Modified", []string{"1999-12-31T01:00:00+"})
if err == nil {
t.Errorf("ParseFieldValues() returns no error for incorrect date input")
}
_, err = db.ParseFieldValues("test", "Modified", []string{"1999-12-31 01:00:00Z"})
if err == nil {
t.Errorf("ParseFieldValues() returns no error for incorrect date input")
}
if count, _ := db.Count("test"); count > 1 {
t.Errorf("Count() > 0 for empty table")
}
if count, _ := db.Count("schmoo"); count != 0 {
t.Errorf("Count() > 0 for nonextistent table")
}
items, _ := db.ListItems("test", 1024)
if items[0] != item {
t.Errorf("ListItems(), expected %d, given %d", item, items[0])
}
items, _ = db.ListItems("schmoo", 1024)
if err == nil {
t.Errorf("ListItems() should return error for nonextistent table, given no error")
}
tx, err = db.Begin()
if err != nil {
t.Errorf("Begin() transaction failed: %s", err)
}
defer tx.Rollback()
err = tx.Set("test", item, "Name", []Value{NewString("John"), NewString("Theodore"),
NewString("Smith")})
if err != nil {
t.Errorf("Set() returns error for correct data type: %s", err)
}
err = tx.Set("test", item, "Name", []Value{NewString("John"), NewInt(333),
NewString("Smith")})
if err == nil {
t.Errorf("Set() returns no error for incorrect data type")
}
err = tx.Index("test", "Name")
if err != nil {
t.Errorf("Index() failed")
}
err = tx.Commit()
if err != nil {
t.Errorf("Commit() failed: %s", err)
}
values, err := db.Get("test", item, "Name")
if err != nil {
t.Errorf("Get() returns error for valid request: %s", err)
}
if len(values) != 3 {
t.Errorf("Get() returns wrong slice length for string list of length 3: %d", len(values))
}
if values[0].String() != "John" || values[1].String() != "Theodore" || values[2].String() != "Smith" {
t.Errorf("Get() returns garbage instead of previously set string list data")
}
// setting and getting different types of data
tx, err = db.Begin()
if err != nil {
t.Errorf("Begin() transaction failed: %s", err)
}
err = tx.Set("test", item, "Age", []Value{NewInt(30)})
if err != nil {
t.Errorf("Set() failed: %s", err)
}
err = tx.Commit()
if err != nil {
t.Errorf("Commit() failed: %s", err)
}
values, err = db.Get("test", item, "Age")
if err != nil {
t.Errorf("Get() failed: %s", err)
}
if len(values) != 1 {
t.Errorf("MDG.Get() for single datum returned []Value of length %d", len(values))
}
if values[0].Int() != 30 {
t.Errorf("Get() failed, given %d, expected %d", values[0].Int(), 30)
}
tx, err = db.Begin()
if err != nil {
t.Errorf("Begin() transaction failed: %s", err)
}
err = tx.Set("test", item, "Email", []Value{NewString("Hello world")})
if err != nil {
t.Errorf("Set() failed: %s", err)
}
err = tx.Commit()
if err != nil {
t.Errorf("Commit() failed: %s", err)
}
values, err = db.Get("test", item, "Email")
if len(values) == 0 || values[0].String() != "Hello world" {
t.Errorf("Get() failed: %s", err)
}
tx, err = db.Begin()
if err != nil {
t.Errorf("Begin() transaction failed: %s", err)
}
err = tx.Set("test", item, "Scores", []Value{NewInt(10), NewInt(20), NewInt(30)})
if err != nil {
t.Errorf("Set() failed: %s", err)
}
err = tx.Commit()
if err != nil {
t.Errorf("Commit() failed: %s", err)
}
values, err = db.Get("test", item, "Scores")
if err != nil {
t.Errorf("Get() failed: %s", err)
}
if len(values) != 3 {
t.Errorf("Get() failed, expected int list of length 3, given length %d", len(values))
}
if values[0].Int() != 10 || values[1].Int() != 20 || values[2].Int() != 30 {
t.Errorf("Get() failed, returning garbage instead of the expected int slice 10, 20, 30")
}
tx, err = db.Begin()
if err != nil {
t.Errorf("Begin() transaction failed: %s", err)
}
d := time.Now()
err = tx.Set("test", item, "Modified", []Value{NewDate(d)})
if err != nil {
t.Errorf("Set() single date failed: %s", err)
}
err = tx.Commit()
if err != nil {
t.Errorf("Commit() failed: %s", err)
}
values, err = db.Get("test", item, "Modified")
if err != nil {
t.Errorf("Get() single date failed: %s", err)
}
if len(values) != 1 || d.UTC().Format(time.RFC3339) != values[0].Datetime().UTC().Format(time.RFC3339) {
t.Errorf("Get() or previous Set() failed for date, expected RFC3339 date %s, given %s",
d.UTC().Format(time.RFC3339), values[0].Datetime().Format(time.RFC3339))
}
tx, err = db.Begin()
if err != nil {
t.Errorf("Begin() transaction failed: %s", err)
}
b := []byte("This is \000a test")
err = tx.Set("test", item, "Misc", []Value{NewBytes(b)})
if err != nil {
t.Errorf("Set() failed for bytes with null in it: %s", err)
}
err = tx.Commit()
if err != nil {
t.Errorf("Commit() failed: %s", err)
}
values, err = db.Get("test", item, "Misc")
if err != nil {
t.Errorf("Get() single blob containing null failed: %s", err)
}
if len(values) != 1 {
t.Errorf("Get() single blob failed, length should be 1, given %d", len(values))
}
tx, err = db.Begin()
if err != nil {
t.Errorf("Begin() transaction failed: %s", err)
}
for i, b2 := range values[0].Bytes() {
if b[i] != b2 {
t.Errorf("Get() single blob or previous Set() failed, expected blob[%d]=%q, given blob[%d]=%q", i, b2, i, b[i])
break
}
}
err = tx.Set("test", item, "Data", []Value{NewBytes([]byte(""))})
if err != nil {
t.Errorf("Set() failed for empty values")
}
err = tx.Commit()
if err != nil {
t.Errorf("Commit() failed: %s", err)
}
values, err = db.Get("test", item, "Data")
if err != nil {
t.Errorf("Get() blob list failed: %s", err)
}
if len(values) != 1 {
t.Errorf("Get() blob list failed, expected return length 1, given %d", len(values))
}
if len(values[0].Bytes()) != 0 {
t.Errorf("Get() blob list with one empty blob, expected an empty []byte slice, given something else")
}
tx, err = db.Begin()
if err != nil {
t.Errorf("Begin() transaction failed: %s", err)
}
err = tx.Set("test", item, "Schedules", []Value{NewDateStr("2017-02-27T17:31:00Z"),
NewDateStr("1969-04-30T23:59:00+04:00"), NewDateStr("2140-12-23T18:00:00Z")})
if err != nil {
t.Errorf("Set() failed for list of dates: %s", err)
}
err = tx.Commit()
if err != nil {
t.Errorf("Commit() failed: %s", err)
}
values, err = db.Get("test", item, "Schedules")
if err != nil {
t.Errorf("Get() failed for date list: %s", err)
}
if len(values) != 3 {
t.Errorf("Get() expected date list of length 3, given length %d", len(values))
}
v := NewDateStr("2017-02-27T17:31:00Z")
expected := v.Datetime().UTC().Format(time.RFC3339)
given := values[0].String()
if given != expected {
t.Errorf("Get() failed for date list entry 0, given %s, expected %s", given, expected)
}
v = NewDateStr("1969-04-30T23:59:00+04:00")
expected = v.Datetime().UTC().Format(time.RFC3339)
given = values[1].String()
if given != expected {
t.Errorf("Get() failed for date list entry 0, given %s, expected %s", given, expected)
}
v = NewDateStr("2140-12-23T18:00:00Z")
expected = v.Datetime().UTC().Format(time.RFC3339)
given = values[2].String()
if given != expected {
t.Errorf("Get() failed for date list entry 0, given %s, expected %s", given, expected)
}
// GetFields
fields, err := db.GetFields("test")
if err != nil {
t.Errorf("GetFields() failed: %s", err)
}
if len(fields) != 8 {
t.Errorf("GetFields() failed, expected %d fields, returned %d", 8, len(fields))
}
e := make([]string, 8)
e[0] = "Name"
e[1] = "Email"
e[2] = "Age"
e[3] = "Scores"
e[4] = "Modified"
e[5] = "Misc"
e[6] = "Data"
e[7] = "Schedules"
for i := range fields {
found := false
for j := range e {
if fields[i].Name == e[j] {
found = true
}
}
if !found {
t.Errorf("GetFields() returned an unexpected result")
}
}
q := make([]FieldType, 8)
q[0] = DBStringList
q[1] = DBString
q[2] = DBInt
q[3] = DBIntList
q[4] = DBDate
q[5] = DBBlob
q[6] = DBBlobList
q[7] = DBDateList
for i := range fields {
found := false
for j := range q {
if fields[i].Sort == q[j] {
found = true
}
}
if !found {
t.Errorf("GetFields() returned an unexpected field type")
}
}
// GetTables
tables := db.GetTables()
if len(tables) != 1 || tables[0] != "test" {
t.Errorf("GetTables() returned garbage")
}
tx, err = db.Begin()
if err != nil {
t.Errorf("Begin() transaction failed: %s", err)
}
// RemoveItem
if !db.ItemExists("test", item) {
t.Errorf("ItemExists() returned false for existing item")
}
err = tx.RemoveItem("test", item)
if err != nil {
t.Errorf("RemoveItem() failed: %s", err)
}
err = tx.Commit()
if err != nil {
t.Errorf("Commit() failed: %s", err)
}
if db.ItemExists("test", item) {
t.Errorf("ItemExists() returned true for nonexistent item, should have returned false")
}
// close it
err = db.Close()
if err != nil {
t.Errorf("Close() failed: %s", err)
}
}
func TestParseQuery(t *testing.T) {
queries := []string{`test Name=r% or Name=John`,
"Person Name=Smith or not Name=John",
"Person Age=47 and Name=John%",
`Person Name="John"`,
`Person Email="[email protected]" and Name="%r%"`,
`Person every Name=%e%`,
`Person no Name=John`,
`Person not every Name=John`,
}
for _, query := range queries {
_, err := ParseQuery(query)
if err != nil {
t.Errorf(`ParseQuery("%s") should work but returns error: %s`, query, err)
}
}
}
// we create random strings for the Find test, setting this up first
const charset = "abcdefghijklmnopqrstuvwxyz" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
var seededRand *rand.Rand = rand.New(
rand.NewSource(time.Now().UnixNano()))
func StringWithCharset(length int, charset string) string {
b := make([]byte, length)
for i := range b {
b[i] = charset[seededRand.Intn(len(charset))]
}
return string(b)
}
func String(length int) string {
return StringWithCharset(length, charset)
}
type testin struct {
item Item
Names []string
Email string
Date string
Dates []string
Blob []byte
Blobs []string
Age int64
Scores []int64
}
func NewRandomDateStr() string {
s := fmt.Sprintf("%4d-%02d-%02dT%02d:%02d:%02d+%d:00",
1800+rand.Intn(400),
1+rand.Intn(12),
1+rand.Intn(27),
1+rand.Intn(24),
rand.Intn(60),
rand.Intn(60),
rand.Intn(24),
)
return s
}
const UintSize = 32 << (^uint(0) >> 32 & 1) // 32 or 64
const (
MaxInt = 1<<(UintSize-1) - 1 // 1<<31 - 1 or 1<<63 - 1
MinInt = -MaxInt - 1 // -1 << 31 or -1 << 63
MaxUint = 1<<UintSize - 1 // 1<<32 - 1 or 1<<64 - 1
)
func NewRandomTestin(n Item) testin {
var ti testin
ti.item = n
ns := make([]string, 3)
for i := range ns {
ns[i] = String(rand.Intn(20) + 1)
}
ti.Names = ns
ti.Email = String(rand.Intn(20) + 3)
ti.Date = NewRandomDateStr()
dates := make([]string, 3)
for i := range dates {
dates[i] = NewRandomDateStr()
}
ti.Dates = dates
ti.Blob = []byte(String(rand.Intn(20) + 3))
ti.Blobs = make([]string, 3)
for i := range ti.Blobs {
ti.Blobs[i] = String(rand.Intn(20) + 1)
}
ti.Age = int64(rand.Intn(100))
ti.Scores = make([]int64, 3)
for i := range ti.Scores {
ti.Scores[i] = int64(rand.Intn(MaxInt))
}
return ti
}
func TestFind(t *testing.T) {
db, err := Open("sqlite3", tmpfile.Name())
if err != nil {
t.Errorf("Open() failed: %s", err)
}
// we first create a number of test inputs
var in []testin
const maxtest = 30
in = make([]testin, maxtest)
for i := range in {
item, err := db.NewItem("test")
if err != nil {
t.Errorf("NewItem() failed: %s", err)
}
tx, err := db.Begin()
if err != nil {
t.Errorf("db.Begin transaction failed: %s", err)
}
in[i] = NewRandomTestin(item)
err = tx.Set("test", in[i].item, "Name", []Value{NewString(in[i].Names[0]),
NewString(in[i].Names[1]), NewString(in[i].Names[2])})
if err != nil {
t.Errorf("Set() failed: %s", err)
}
err = tx.Set("test", in[i].item, "Email", []Value{NewString(in[i].Email)})
if err != nil {
t.Errorf("Set() failed: %s", err)
}
err = tx.Set("test", in[i].item, "Modified", []Value{NewDateStr(in[i].Date)})
if err != nil {
t.Errorf("Set() failed: %s", err)
}
err = tx.Set("test", in[i].item, "Schedules", []Value{NewDateStr(in[i].Dates[0]),
NewDateStr(in[i].Dates[1]), NewDateStr(in[i].Dates[2])})
if err != nil {
t.Errorf("Set() failed: %s", err)
}
err = tx.Set("test", in[i].item, "Misc", []Value{NewBytes([]byte(in[i].Blob))})
if err != nil {
t.Errorf("Set() failed: %s", err)
}
err = tx.Set("test", in[i].item, "Data", []Value{NewBytes([]byte(in[i].Blobs[0])),
NewBytes([]byte(in[i].Blobs[1])), NewBytes([]byte(in[i].Blobs[2]))})
if err != nil {
t.Errorf("Set() failed: %s", err)
}
err = tx.Set("test", in[i].item, "Age", []Value{NewInt(in[i].Age)})
if err != nil {
t.Errorf("Set() failed: %s", err)
}
err = tx.Set("test", in[i].item, "Scores", []Value{NewInt(in[i].Scores[0]),
NewInt(in[i].Scores[1]), NewInt(in[i].Scores[2])})
if err != nil {
t.Errorf("Set() failed: %s", err)
}
err = tx.Commit()
if err != nil {
t.Errorf("Commit() failed: %s", err)
}
}
// now query everything directly first
for _, v := range in {
// Names string list field
for j := range v.Names {
query := `test Name=` + v.Names[j]
q, err := ParseQuery(query)
if err != nil {
t.Errorf(`ParseQuery("%s") failed: %s`, query, err)
}
results, err := db.Find(q, 200)
if err != nil {
t.Errorf(`Find() failed for query "%s", should have succeeded`, query)
}
found := false
for k := range results {
if results[k] == v.item {
found = true
}
}
if !found {
t.Errorf(`Find() failed to find Name for query "%s", should have succeeded`, query)
}
}
// Age int field
query := fmt.Sprintf("test Age=%d", v.Age)
q, err := ParseQuery(query)
if err != nil {
t.Errorf(`ParseQuery("%s") failed: %s`, query, err)
}
results, err := db.Find(q, 200)
if err != nil {
t.Errorf(`Find() failed for query "%s", should have succeeded`, query)
}
found := false
for k := range results {
if results[k] == v.item {
found = true
}
}
if !found {
t.Errorf(`Find() failed to find Name for query "%s", should have succeeded`, query)
}
// Email string field
query = fmt.Sprintf("test Email=%s", v.Email)
q, err = ParseQuery(query)
if err != nil {
t.Errorf(`ParseQuery("%s") failed: %s`, query, err)
}
results, err = db.Find(q, 200)
if err != nil {
t.Errorf(`Find() failed for query "%s", should have succeeded`, query)
}
found = false
for k := range results {
if results[k] == v.item {
found = true
}
}
if !found {
t.Errorf(`Find() failed to find Name for query "%s", should have succeeded`, query)
}
// Scores int list field
// Names string list field
for j := range v.Scores {
query := fmt.Sprintf(`test Scores=%d`, v.Scores[j])
q, err := ParseQuery(query)
if err != nil {
t.Errorf(`ParseQuery("%s") failed: %s`, query, err)
}
results, err := db.Find(q, 200)
if err != nil {
t.Errorf(`Find() failed for query "%s", should have succeeded`, query)
}
found := false
for k := range results {
if results[k] == v.item {
found = true
}
}
if !found {
t.Errorf(`Find() failed to find Name for query "%s", should have succeeded`, query)
}
}
// Data blob list field
for j := range v.Blobs {
query := fmt.Sprintf("test Data=%s", v.Blobs[j])
q, err := ParseQuery(query)
if err != nil {
t.Errorf(`ParseQuery("%s") failed: %s`, query, err)
}
results, err := db.Find(q, 200)
if err != nil {
t.Errorf(`Find() failed for query "%s", should have succeeded: %s`, query, err)
}
found := false
for k := range results {
if results[k] == v.item {
found = true
}
}
if !found {
t.Errorf(`Find() failed to find Blob for query "%s", should have succeeded`, query)
}
}
// Misc blob field
query = fmt.Sprintf("test Misc=%s", v.Blob)
q, err = ParseQuery(query)
if err != nil {
t.Errorf(`ParseQuery("%s") failed: %s`, query, err)
}
results, err = db.Find(q, 200)
if err != nil {
t.Errorf(`Find() failed for query "%s", should have succeeded`, query)