forked from jmoiron/modl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modl_test.go
1003 lines (850 loc) · 22 KB
/
modl_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 modl
import (
"bytes"
"database/sql"
"fmt"
"log"
"os"
"reflect"
"testing"
"time"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
)
var _ = log.Fatal
type Invoice struct {
ID int64
Created int64 `db:"date_created"`
Updated int64
Memo string
PersonID int64
IsPaid bool
}
type Person struct {
ID int64
Created int64
Updated int64
FName string
LName string
Version int64
}
type InvoicePersonView struct {
InvoiceID int64
PersonID int64
Memo string
FName string
LegacyVersion int64
}
type TableWithNull struct {
ID int64
Str sql.NullString
Int64 sql.NullInt64
Float64 sql.NullFloat64
Bool sql.NullBool
Bytes []byte
}
type WithIgnoredColumn struct {
internal int64 `db:"-"`
ID int64
Created int64
}
type WithStringPk struct {
ID string
Name string
}
type CustomStringType string
func (p *Person) PreInsert(s SqlExecutor) error {
p.Created = time.Now().UnixNano()
p.Updated = p.Created
if p.FName == "badname" {
return fmt.Errorf("invalid name: %s", p.FName)
}
return nil
}
func (p *Person) PostInsert(s SqlExecutor) error {
p.LName = "postinsert"
return nil
}
func (p *Person) PreUpdate(s SqlExecutor) error {
p.FName = "preupdate"
return nil
}
func (p *Person) PostUpdate(s SqlExecutor) error {
p.LName = "postupdate"
return nil
}
func (p *Person) PreDelete(s SqlExecutor) error {
p.FName = "predelete"
return nil
}
func (p *Person) PostDelete(s SqlExecutor) error {
p.LName = "postdelete"
return nil
}
func (p *Person) PostGet(s SqlExecutor) error {
p.LName = "postget"
return nil
}
type PersistentUser struct {
Key int32 `db:"mykey"`
ID string
PassedTraining bool
}
func TestCreateTablesIfNotExists(t *testing.T) {
dbmap := initDbMap()
defer dbmap.Cleanup()
err := dbmap.CreateTablesIfNotExists()
if err != nil {
t.Error(err)
}
}
func TestPersistentUser(t *testing.T) {
dbmap := newDbMap()
dbmap.Exec("drop table if exists persistentuser")
if len(os.Getenv("MODL_TEST_TRACE")) > 0 {
dbmap.TraceOn("test", log.New(os.Stdout, "modltest: ", log.Lmicroseconds))
}
dbmap.AddTable(PersistentUser{}).SetKeys(false, "mykey")
err := dbmap.CreateTablesIfNotExists()
if err != nil {
panic(err)
}
defer dbmap.Cleanup()
pu := &PersistentUser{43, "33r", false}
err = dbmap.Insert(pu)
if err != nil {
panic(err)
}
// prove we can pass a pointer into Get
pu2 := &PersistentUser{}
err = dbmap.Get(pu2, pu.Key)
if err != nil {
panic(err)
}
if !reflect.DeepEqual(pu, pu2) {
t.Errorf("%v!=%v", pu, pu2)
}
arr := []*PersistentUser{}
err = dbmap.Select(&arr, "select * from persistentuser")
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(pu, arr[0]) {
t.Errorf("%v!=%v", pu, arr[0])
}
// prove we can get the results back in a slice
puArr := []PersistentUser{}
err = dbmap.Select(&puArr, "select * from persistentuser")
if err != nil {
t.Error(err)
}
if len(puArr) != 1 {
t.Errorf("Expected one persistentuser, found none")
}
if !reflect.DeepEqual(pu, &puArr[0]) {
t.Errorf("%v!=%v", pu, puArr[0])
}
}
func TestOverrideVersionCol(t *testing.T) {
dbmap := initDbMap()
dbmap.DropTables()
t1 := dbmap.AddTable(InvoicePersonView{}).SetKeys(false, "invoiceid", "personid")
err := dbmap.CreateTables()
if err != nil {
panic(err)
}
defer dbmap.Cleanup()
c1 := t1.SetVersionCol("legacyversion")
if c1.ColumnName != "legacyversion" {
t.Errorf("Wrong col returned: %v", c1)
}
ipv := &InvoicePersonView{1, 2, "memo", "fname", 0}
_update(dbmap, ipv)
if ipv.LegacyVersion != 1 {
t.Errorf("LegacyVersion not updated: %d", ipv.LegacyVersion)
}
}
func TestDontPanicOnInsert(t *testing.T) {
var err error
dbmap := initDbMap()
defer dbmap.Cleanup()
err = dbmap.Insert(&TableWithNull{ID: 10})
if err == nil {
t.Errorf("Should have received an error for inserting without a known table.")
}
}
func TestOptimisticLocking(t *testing.T) {
var err error
dbmap := initDbMap()
defer dbmap.Cleanup()
p1 := &Person{0, 0, 0, "Bob", "Smith", 0}
dbmap.Insert(p1) // Version is now 1
if p1.Version != 1 {
t.Errorf("Insert didn't incr Version: %d != %d", 1, p1.Version)
return
}
if p1.ID == 0 {
t.Errorf("Insert didn't return a generated PK")
return
}
p2 := &Person{}
err = dbmap.Get(p2, p1.ID)
if err != nil {
panic(err)
}
p2.LName = "Edwards"
_, err = dbmap.Update(p2) // Version is now 2
if err != nil {
panic(err)
}
if p2.Version != 2 {
t.Errorf("Update didn't incr Version: %d != %d", 2, p2.Version)
}
p1.LName = "Howard"
count, err := dbmap.Update(p1)
if _, ok := err.(OptimisticLockError); !ok {
t.Errorf("update - Expected OptimisticLockError, got: %v", err)
}
if count != -1 {
t.Errorf("update - Expected -1 count, got: %d", count)
}
count, err = dbmap.Delete(p1)
if _, ok := err.(OptimisticLockError); !ok {
t.Errorf("delete - Expected OptimisticLockError, got: %v", err)
}
if count != -1 {
t.Errorf("delete - Expected -1 count, got: %d", count)
}
}
// what happens if a legacy table has a null value?
func TestDoubleAddTable(t *testing.T) {
dbmap := newDbMap()
t1 := dbmap.AddTable(TableWithNull{}).SetKeys(false, "ID")
t2 := dbmap.AddTable(TableWithNull{})
if t1 != t2 {
t.Errorf("%v != %v", t1, t2)
}
}
// test overriding the create sql
func TestColMapCreateSql(t *testing.T) {
dbmap := newDbMap()
t1 := dbmap.AddTable(TableWithNull{})
b := t1.ColMap("Bytes")
custom := "bytes text NOT NULL"
b.SetSqlCreate(custom)
var buf bytes.Buffer
writeColumnSql(&buf, b)
s := buf.String()
if s != custom {
t.Errorf("Expected custom sql `%s`, got %s", custom, s)
}
err := dbmap.CreateTables()
defer dbmap.Cleanup()
if err != nil {
t.Error(err)
}
}
// what happens if a legacy table has a null value?
func TestNullValues(t *testing.T) {
dbmap := initDbMapNulls()
defer dbmap.Cleanup()
// insert a row directly
_, err := dbmap.Exec(`insert into tablewithnull values (10, null, null, null, null, null)`)
if err != nil {
panic(err)
}
// try to load it
expected := &TableWithNull{ID: 10}
t1 := &TableWithNull{}
MustGet(dbmap, t1, 10)
if !reflect.DeepEqual(expected, t1) {
t.Errorf("%v != %v", expected, t1)
}
// update it
t1.Str = sql.NullString{"hi", true}
expected.Str = t1.Str
t1.Int64 = sql.NullInt64{999, true}
expected.Int64 = t1.Int64
t1.Float64 = sql.NullFloat64{53.33, true}
expected.Float64 = t1.Float64
t1.Bool = sql.NullBool{true, true}
expected.Bool = t1.Bool
t1.Bytes = []byte{1, 30, 31, 33}
expected.Bytes = t1.Bytes
_update(dbmap, t1)
MustGet(dbmap, t1, 10)
if t1.Str.String != "hi" {
t.Errorf("%s != hi", t1.Str.String)
}
if !reflect.DeepEqual(expected, t1) {
t.Errorf("%v != %v", expected, t1)
}
}
func TestColumnProps(t *testing.T) {
dbmap := newDbMap()
//dbmap.TraceOn("", log.New(os.Stdout, "modltest: ", log.Lmicroseconds))
t1 := dbmap.AddTable(Invoice{}).SetKeys(true, "ID")
//t1.ColMap("Created").Rename("date_created")
t1.ColMap("Updated").SetTransient(true)
t1.ColMap("Memo").SetMaxSize(10)
t1.ColMap("PersonID").SetUnique(true)
err := dbmap.CreateTables()
if err != nil {
panic(err)
}
defer dbmap.Cleanup()
// test transient
inv := &Invoice{0, 0, 1, "my invoice", 0, true}
_insert(dbmap, inv)
inv2 := Invoice{}
MustGet(dbmap, &inv2, inv.ID)
if inv2.Updated != 0 {
t.Errorf("Saved transient column 'Updated'")
}
// test max size
inv2.Memo = "this memo is too long"
err = dbmap.Insert(inv2)
if err == nil {
t.Errorf("max size exceeded, but Insert did not fail.")
}
// test unique - same person id
inv = &Invoice{0, 0, 1, "my invoice2", 0, false}
err = dbmap.Insert(inv)
if err == nil {
t.Errorf("same PersonID inserted, but Insert did not fail.")
}
}
func TestRawSelect(t *testing.T) {
dbmap := initDbMap()
defer dbmap.Cleanup()
p1 := &Person{0, 0, 0, "bob", "smith", 0}
_insert(dbmap, p1)
inv1 := &Invoice{0, 0, 0, "xmas order", p1.ID, true}
_insert(dbmap, inv1)
expected := &InvoicePersonView{inv1.ID, p1.ID, inv1.Memo, p1.FName, 0}
query := "select i.id invoiceid, p.id personid, i.memo, p.fname " +
"from invoice_test i, person_test p " +
"where i.personid = p.id"
list := []InvoicePersonView{}
MustSelect(dbmap, &list, query)
if len(list) != 1 {
t.Errorf("len(list) != 1: %d", len(list))
} else if !reflect.DeepEqual(expected, &list[0]) {
t.Errorf("%v != %v", expected, list[0])
}
}
func TestHooks(t *testing.T) {
dbmap := initDbMap()
defer dbmap.Cleanup()
p1 := &Person{0, 0, 0, "bob", "smith", 0}
_insert(dbmap, p1)
if p1.Created == 0 || p1.Updated == 0 {
t.Errorf("p1.PreInsert() didn't run: %v", p1)
} else if p1.LName != "postinsert" {
t.Errorf("p1.PostInsert() didn't run: %v", p1)
}
MustGet(dbmap, p1, p1.ID)
if p1.LName != "postget" {
t.Errorf("p1.PostGet() didn't run: %v", p1)
}
p1.LName = "smith"
_update(dbmap, p1)
if p1.FName != "preupdate" {
t.Errorf("p1.PreUpdate() didn't run: %v", p1)
} else if p1.LName != "postupdate" {
t.Errorf("p1.PostUpdate() didn't run: %v", p1)
}
var persons []*Person
bindVar := dbmap.Dialect.BindVar(0)
MustSelect(dbmap, &persons, "select * from person_test where id = "+bindVar, p1.ID)
if persons[0].LName != "postget" {
t.Errorf("p1.PostGet() didn't run after select: %v", p1)
}
_del(dbmap, p1)
if p1.FName != "predelete" {
t.Errorf("p1.PreDelete() didn't run: %v", p1)
} else if p1.LName != "postdelete" {
t.Errorf("p1.PostDelete() didn't run: %v", p1)
}
// Test error case
p2 := &Person{0, 0, 0, "badname", "", 0}
err := dbmap.Insert(p2)
if err == nil {
t.Errorf("p2.PreInsert() didn't return an error")
}
}
func TestTransaction(t *testing.T) {
dbmap := initDbMap()
defer dbmap.Cleanup()
inv1 := &Invoice{0, 100, 200, "t1", 0, true}
inv2 := &Invoice{0, 100, 200, "t2", 0, false}
trans, err := dbmap.Begin()
if err != nil {
panic(err)
}
trans.Insert(inv1, inv2)
err = trans.Commit()
if err != nil {
panic(err)
}
obj := &Invoice{}
err = dbmap.Get(obj, inv1.ID)
if err != nil {
panic(err)
}
if !reflect.DeepEqual(inv1, obj) {
t.Errorf("%v != %v", inv1, obj)
}
err = dbmap.Get(obj, inv2.ID)
if err != nil {
panic(err)
}
if !reflect.DeepEqual(inv2, obj) {
t.Errorf("%v != %v", inv2, obj)
}
}
func TestMultiple(t *testing.T) {
dbmap := initDbMap()
defer dbmap.Cleanup()
inv1 := &Invoice{0, 100, 200, "a", 0, false}
inv2 := &Invoice{0, 100, 200, "b", 0, true}
_insert(dbmap, inv1, inv2)
inv1.Memo = "c"
inv2.Memo = "d"
_update(dbmap, inv1, inv2)
count := _del(dbmap, inv1, inv2)
if count != 2 {
t.Errorf("%d != 2", count)
}
}
func TestCrud(t *testing.T) {
dbmap := initDbMap()
defer dbmap.Cleanup()
inv := &Invoice{0, 100, 200, "first order", 0, true}
// INSERT row
_insert(dbmap, inv)
if inv.ID == 0 {
t.Errorf("inv.ID was not set on INSERT")
return
}
// SELECT row
inv2 := &Invoice{}
MustGet(dbmap, inv2, inv.ID)
if !reflect.DeepEqual(inv, inv2) {
t.Errorf("%v != %v", inv, inv2)
}
// UPDATE row and SELECT
inv.Memo = "second order"
inv.Created = 999
inv.Updated = 11111
count := _update(dbmap, inv)
if count != 1 {
t.Errorf("update 1 != %d", count)
}
MustGet(dbmap, inv2, inv.ID)
if !reflect.DeepEqual(inv, inv2) {
t.Errorf("%v != %v", inv, inv2)
}
// DELETE row
deleted := _del(dbmap, inv)
if deleted != 1 {
t.Errorf("Did not delete row with ID: %d", inv.ID)
return
}
// VERIFY deleted
err := dbmap.Get(inv2, inv.ID)
if err != sql.ErrNoRows {
t.Errorf("Found invoice with id: %d after Delete()", inv.ID)
}
}
func TestWithIgnoredColumn(t *testing.T) {
dbmap := initDbMap()
defer dbmap.Cleanup()
ic := &WithIgnoredColumn{-1, 0, 1}
_insert(dbmap, ic)
expected := &WithIgnoredColumn{0, 1, 1}
ic2 := &WithIgnoredColumn{}
MustGet(dbmap, ic2, ic.ID)
if !reflect.DeepEqual(expected, ic2) {
t.Errorf("%v != %v", expected, ic2)
}
if _del(dbmap, ic) != 1 {
t.Errorf("Did not delete row with ID: %d", ic.ID)
return
}
err := dbmap.Get(ic2, ic.ID)
if err != sql.ErrNoRows {
t.Errorf("Found id: %d after Delete() (%#v)", ic.ID, ic2)
}
}
func TestVersionMultipleRows(t *testing.T) {
dbmap := initDbMap()
defer dbmap.Cleanup()
persons := []*Person{
&Person{0, 0, 0, "Bob", "Smith", 0},
&Person{0, 0, 0, "Jane", "Smith", 0},
&Person{0, 0, 0, "Mike", "Smith", 0},
}
_insert(dbmap, persons[0], persons[1], persons[2])
for x, p := range persons {
if p.Version != 1 {
t.Errorf("person[%d].Version != 1: %d", x, p.Version)
}
}
}
func TestWithStringPk(t *testing.T) {
dbmap := newDbMap()
//dbmap.TraceOn("", log.New(os.Stdout, "modltest: ", log.Lmicroseconds))
dbmap.AddTableWithName(WithStringPk{}, "string_pk_test").SetKeys(true, "ID")
_, err := dbmap.Exec("create table string_pk_test (ID varchar(255), Name varchar(255));")
if err != nil {
t.Errorf("couldn't create string_pk_test: %v", err)
}
defer dbmap.Cleanup()
row := &WithStringPk{"1", "foo"}
err = dbmap.Insert(row)
if err == nil {
t.Errorf("Expected error when inserting into table w/non Int PK and autoincr set true")
}
}
func BenchmarkNativeCrud(b *testing.B) {
var err error
b.StopTimer()
dbmap := initDbMapBench()
defer dbmap.Cleanup()
b.StartTimer()
insert := "insert into invoice_test (date_created, updated, memo, personid) values (?, ?, ?, ?)"
sel := "select id, date_created, updated, memo, personid from invoice_test where id=?"
update := "update invoice_test set date_created=?, updated=?, memo=?, personid=? where id=?"
delete := "delete from invoice_test where id=?"
suffix := dbmap.Dialect.AutoIncrInsertSuffix(&ColumnMap{ColumnName: "id"})
insert = ReBind(insert, dbmap.Dialect) + suffix
sel = ReBind(sel, dbmap.Dialect)
update = ReBind(update, dbmap.Dialect)
delete = ReBind(delete, dbmap.Dialect)
inv := &Invoice{0, 100, 200, "my memo", 0, false}
for i := 0; i < b.N; i++ {
if len(suffix) == 0 {
res, err := dbmap.Db.Exec(insert, inv.Created, inv.Updated, inv.Memo, inv.PersonID)
if err != nil {
panic(err)
}
newid, err := res.LastInsertId()
if err != nil {
panic(err)
}
inv.ID = newid
} else {
rows, err := dbmap.Db.Query(insert, inv.Created, inv.Updated, inv.Memo, inv.PersonID)
if err != nil {
panic(err)
}
if rows.Next() {
err = rows.Scan(&inv.ID)
if err != nil {
panic(err)
}
}
rows.Close()
}
row := dbmap.Db.QueryRow(sel, inv.ID)
err = row.Scan(&inv.ID, &inv.Created, &inv.Updated, &inv.Memo, &inv.PersonID)
if err != nil {
panic(err)
}
inv.Created = 1000
inv.Updated = 2000
inv.Memo = "my memo 2"
inv.PersonID = 3000
_, err = dbmap.Db.Exec(update, inv.Created, inv.Updated, inv.Memo,
inv.PersonID, inv.ID)
if err != nil {
panic(err)
}
_, err = dbmap.Db.Exec(delete, inv.ID)
if err != nil {
panic(err)
}
}
}
func BenchmarkModlCrud(b *testing.B) {
b.StopTimer()
dbmap := initDbMapBench()
defer dbmap.Cleanup()
//dbmap.TraceOn("", log.New(os.Stdout, "modltest: ", log.Lmicroseconds))
b.StartTimer()
inv := &Invoice{0, 100, 200, "my memo", 0, true}
for i := 0; i < b.N; i++ {
err := dbmap.Insert(inv)
if err != nil {
panic(err)
}
inv2 := Invoice{}
err = dbmap.Get(&inv2, inv.ID)
if err != nil {
panic(err)
}
inv2.Created = 1000
inv2.Updated = 2000
inv2.Memo = "my memo 2"
inv2.PersonID = 3000
_, err = dbmap.Update(&inv2)
if err != nil {
panic(err)
}
_, err = dbmap.Delete(&inv2)
if err != nil {
panic(err)
}
}
}
func initDbMapBench() *DbMap {
dbmap := newDbMap()
dbmap.Db.Exec("drop table if exists invoice_test")
dbmap.AddTableWithName(Invoice{}, "invoice_test").SetKeys(true, "id")
err := dbmap.CreateTables()
if err != nil {
panic(err)
}
return dbmap
}
func (d *DbMap) Cleanup() {
err := d.DropTables()
if err != nil {
panic(err)
}
err = d.Dbx.Close()
if err != nil {
panic(err)
}
}
func initDbMap() *DbMap {
dbmap := newDbMap()
//dbmap.TraceOn("", log.New(os.Stdout, "modltest: ", log.Lmicroseconds))
dbmap.AddTableWithName(Invoice{}, "invoice_test").SetKeys(true, "id")
dbmap.AddTableWithName(Person{}, "person_test").SetKeys(true, "id")
dbmap.AddTableWithName(WithIgnoredColumn{}, "ignored_column_test").SetKeys(true, "id")
dbmap.AddTableWithName(WithTime{}, "time_test").SetKeys(true, "ID")
err := dbmap.CreateTables()
if err != nil {
panic(err)
}
return dbmap
}
func TestTruncateTables(t *testing.T) {
dbmap := initDbMap()
defer dbmap.Cleanup()
err := dbmap.CreateTablesIfNotExists()
if err != nil {
t.Error(err)
}
// Insert some data
p1 := &Person{0, 0, 0, "Bob", "Smith", 0}
dbmap.Insert(p1)
inv := &Invoice{0, 0, 1, "my invoice", 0, true}
dbmap.Insert(inv)
err = dbmap.TruncateTables()
if err != nil {
t.Error(err)
}
// Make sure all rows are deleted
people := []Person{}
invoices := []Invoice{}
dbmap.Select(&people, "SELECT * FROM person_test")
if len(people) != 0 {
t.Errorf("Expected 0 person rows, got %d", len(people))
}
dbmap.Select(&invoices, "SELECT * FROM invoice_test")
if len(invoices) != 0 {
t.Errorf("Expected 0 invoice rows, got %d", len(invoices))
}
}
func TestTruncateTablesIdentityRestart(t *testing.T) {
dbmap := initDbMap()
defer dbmap.Cleanup()
err := dbmap.CreateTablesIfNotExists()
if err != nil {
t.Error(err)
}
// Insert some data
p1 := &Person{0, 0, 0, "Bob", "Smith", 0}
dbmap.Insert(p1)
inv := &Invoice{0, 0, 1, "my invoice", 0, true}
dbmap.Insert(inv)
err = dbmap.TruncateTablesIdentityRestart()
if err != nil {
t.Error(err)
}
// Make sure all rows are deleted
people := []Person{}
invoices := []Invoice{}
dbmap.Select(&people, "SELECT * FROM person_test")
if len(people) != 0 {
t.Errorf("Expected 0 person rows, got %d", len(people))
}
dbmap.Select(&invoices, "SELECT * FROM invoice_test")
if len(invoices) != 0 {
t.Errorf("Expected 0 invoice rows, got %d", len(invoices))
}
p2 := &Person{0, 0, 0, "Other", "Person", 0}
dbmap.Insert(p2)
if p2.ID != int64(1) {
t.Errorf("Expected new person ID to be equal to 1, was %d", p2.ID)
}
}
func TestSelectBehavior(t *testing.T) {
db := initDbMap()
defer db.Cleanup()
p := Person{}
// check that SelectOne with no rows returns ErrNoRows
err := db.SelectOne(&p, "select * from person_test")
if err == nil || err != sql.ErrNoRows {
t.Fatal(err)
}
// insert and ensure SelectOne works properly
bob := Person{0, 0, 0, "Bob", "Smith", 0}
db.Insert(&bob)
err = db.SelectOne(&p, "select * from person_test")
if err != nil {
t.Fatal(err)
}
if p.FName != "Bob" {
t.Errorf("Wrong FName: %s", p.FName)
}
// there's a post hook on this that sets it to postget, ensure it ran
if p.LName != "postget" {
t.Errorf("Wrong LName: %s", p.LName)
}
// insert again and ensure SelectOne *does not* error in rows > 1
ben := Person{0, 0, 0, "Ben", "Smith", 0}
db.Insert(&ben)
err = db.SelectOne(&p, "select * from person_test ORDER BY fname ASC")
if err != nil {
t.Fatal(err)
}
if p.FName != "Ben" {
t.Errorf("Wrong FName: %s", p.FName)
}
}
func TestQuoteTableNames(t *testing.T) {
dbmap := initDbMap()
defer dbmap.Cleanup()
quotedTableName := dbmap.Dialect.QuoteField("person_test")
// Use a buffer to hold the log to check generated queries
var logBuffer bytes.Buffer
dbmap.TraceOn("", log.New(&logBuffer, "modltest:", log.Lmicroseconds))
// Create some rows
p1 := &Person{0, 0, 0, "bob", "smith", 0}
errorTemplate := "Expected quoted table name %v in query but didn't find it"
// Check if Insert quotes the table name
id := dbmap.Insert(p1)
if !bytes.Contains(logBuffer.Bytes(), []byte(quotedTableName)) {
t.Log("log:", logBuffer.String())
t.Errorf(errorTemplate, quotedTableName)
}
logBuffer.Reset()
// Check if Get quotes the table name
dbmap.Get(Person{}, id)
if !bytes.Contains(logBuffer.Bytes(), []byte(quotedTableName)) {
t.Errorf(errorTemplate, quotedTableName)
}
logBuffer.Reset()
}
type WithTime struct {
ID int64
Time time.Time
}
func TestWithTime(t *testing.T) {
dbmap := initDbMap()
defer dbmap.Cleanup()
// FIXME: there seems to be a bug with go-sql-driver and timezones?
// MySQL doesn't have any timestamp support, but since it is not
// sending any, the scan assumes UTC, so the scanner should
// probably convert to UTC before storing. Also, note that time.Time
// support requires a special bit to be added to the DSN
t1, err := time.Parse("2006-01-02 15:04:05 -0700 MST",
"2013-08-09 21:30:43 +0000 UTC")
if err != nil {
t.Fatal(err)
}
w1 := WithTime{1, t1}
dbmap.Insert(&w1)
w2 := WithTime{}
dbmap.Get(&w2, w1.ID)
if w1.Time.UnixNano() != w2.Time.UnixNano() {
t.Errorf("%v != %v", w1, w2)
}
}
func initDbMapNulls() *DbMap {
dbmap := newDbMap()
//dbmap.TraceOn("", log.New(os.Stdout, "modltest: ", log.Lmicroseconds))
dbmap.AddTable(TableWithNull{}).SetKeys(false, "id")
err := dbmap.CreateTables()
if err != nil {
panic(err)
}
return dbmap
}
func newDbMap() *DbMap {
dialect, driver := dialectAndDriver()
return NewDbMap(connect(driver), dialect)
}
func connect(driver string) *sql.DB {
dsn := os.Getenv("MODL_TEST_DSN")
if dsn == "" {
panic("MODL_TEST_DSN env variable is not set. Please see README.md")
}
db, err := sql.Open(driver, dsn)
if err != nil {
panic("Error connecting to db: " + err.Error())
}
err = db.Ping()
if err != nil {
panic("Error connecting to db: " + err.Error())
}
return db
}
func dialectAndDriver() (Dialect, string) {
switch os.Getenv("MODL_TEST_DIALECT") {
case "mysql":
return MySQLDialect{"InnoDB", "UTF8"}, "mysql"
case "postgres":
return PostgresDialect{}, "postgres"
case "sqlite":
return SqliteDialect{}, "sqlite3"
}
panic("MODL_TEST_DIALECT env variable is not set or is invalid. Please see README.md")
}
func _insert(dbmap *DbMap, list ...interface{}) {
err := dbmap.Insert(list...)
if err != nil {
panic(err)
}
}
func _update(dbmap *DbMap, list ...interface{}) int64 {
count, err := dbmap.Update(list...)
if err != nil {
panic(err)
}
return count
}
func _del(dbmap *DbMap, list ...interface{}) int64 {
count, err := dbmap.Delete(list...)
if err != nil {
panic(err)
}
return count
}
func MustGet(dbmap *DbMap, i interface{}, keys ...interface{}) {
err := dbmap.Get(i, keys...)
if err != nil {
panic(err)
}
}
func MustSelect(dbmap *DbMap, dest interface{}, query string, args ...interface{}) {
err := dbmap.Select(dest, query, args...)
if err != nil {