-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha_sqac_test.go
executable file
·1958 lines (1660 loc) · 63.1 KB
/
a_sqac_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 sqac_test
import (
"database/sql"
"flag"
"fmt"
"log"
"os"
"reflect"
"strings"
"testing"
"time"
"github.com/1414C/sqac"
"github.com/1414C/sqac/common"
_ "github.com/SAP/go-hdb/driver"
_ "github.com/denisenkom/go-mssqldb"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
)
var (
Handle sqac.PublicDB
)
// ============================================================================================================================
// GetEntities test artifacts
// ============================================================================================================================
type DepotGetEntities2 struct {
DepotNum int `json:"depot_num" db:"depot_num" sqac:"primary_key:inc;start:90000000"`
DepotBay int `json:"depot_bay" db:"depot_bay" sqac:"primary_key:"`
CreateDate time.Time `json:"create_date" db:"create_date" sqac:"nullable:false;default:now();index:nonUnique"`
Region string `json:"region" db:"region" sqac:"nullable:false;default:YYC"`
Province string `json:"province" db:"province" sqac:"nullable:false;default:AB"`
Country string `json:"country" db:"country" sqac:"nullable:true;default:CA"`
NewColumn1 string `json:"new_column1" db:"new_column1" sqac:"nullable:false"`
NewColumn2 int64 `json:"new_column2" db:"new_column2" sqac:"nullable:false"`
NewColumn3 float64 `json:"new_column3" db:"new_column3" sqac:"nullable:false;default:0.0"`
IntDefaultZero int `json:"int_default_zero" db:"int_default_zero" sqac:"nullable:false;default:0"`
IntDefault42 int `json:"int_default42" db:"int_default42" sqac:"nullable:false;default:42"`
IntZeroValNoDefault int `json:"int_zero_val_no_default" db:"int_zero_val_no_default" sqac:"nullable:false"`
NonPersistentColumn string `json:"non_persistent_column" db:"non_persistent_column" sqac:"-"`
}
type DepotGetEntitiesTab struct {
ents []DepotGetEntities2
sqac.GetEnt
}
// Implement the sqac.GetEnt{} interface for DepotGetEntities2
func (dget *DepotGetEntitiesTab) Exec(sqh sqac.PublicDB) error {
selQuery := "SELECT * FROM depotgetentities2;"
// read the table rows
rows, err := sqh.ExecuteQueryx(selQuery)
if err != nil {
log.Printf("GetEntities for table depotgetentities2 returned error: %v\n", err.Error())
return err
}
defer rows.Close()
for rows.Next() {
var ent DepotGetEntities2
err = rows.StructScan(&ent)
if err != nil {
fmt.Printf("error reading rows: %v\n", err)
return err
}
dget.ents = append(dget.ents, ent)
}
if sqh.IsLog() {
fmt.Println("DepotGetEntitiesTab.Exec() got:", dget.ents)
}
return nil
}
//============================================================================================================================
// GetEntities test artifacts end
//============================================================================================================================
// TestMain - the go test entry point
func TestMain(m *testing.M) {
// parse flags
dbFlag := flag.String("db", "postgres", "db to connect to")
logFlag := flag.Bool("l", false, "activate sqac detail logging to stdout")
dbLogFlag := flag.Bool("dbl", false, "activate DDL/DML logging to stdout)")
flag.Parse()
var cs string
switch *dbFlag {
case "postgres":
cs = "host=127.0.0.1 user=godev dbname=sqactst sslmode=disable password=gogogo123"
case "mysql":
cs = "godev:gogogo123@tcp(localhost:3306)/sqactst?charset=utf8&parseTime=True&loc=Local"
case "sqlite":
cs = "testdb.sqlite"
case "mssql":
cs = "sqlserver://SA:gogogo123@localhost:1433?database=sqlx"
case "db2":
cs = ""
case "hdb":
cs = "hdb://SYSTEM:WTBHana1!@192.168.112.35:39017"
//cs = "hdb://hxeadm:HXEHana1@192.168.112.35:39017"
//cs = "hdb://godev:gogogo123@your.hanadb.com:30015"
default:
cs = ""
}
Handle = sqac.Create(*dbFlag, *logFlag, *dbLogFlag, cs)
// run the tests
code := m.Run()
os.Exit(code)
}
// TestGetDBDriverName
//
// Check that a driver name is returned
func TestGetDBDriverName(t *testing.T) {
driverName := Handle.GetDBDriverName()
if driverName == "" {
t.Errorf("unable to determine db driver name")
}
if Handle.IsLog() {
fmt.Println("db driver name:", driverName)
}
}
// TestGetDBName
//
// Check that a db name is known
func TestGetDBName(t *testing.T) {
dbName := Handle.GetDBName()
if dbName == "" {
t.Errorf("unable to determine db name")
}
if Handle.IsLog() {
fmt.Println("db name:", dbName)
}
}
// TestExistsTableNegative
// Test for non-existent table 'Footle'
func TestExistsTableNegative(t *testing.T) {
type Footle struct {
KeyNum int `db:"key_num" sqac:"primary_key:inc"`
CreateDate time.Time `db:"create_date" sqac:"nullable:false;default:now();"`
Description string `db:"description" sqac:"nullable:false;default:"`
}
// determine the table name as per the table creation logic
tn := common.GetTableName(Footle{})
// expect that table depot does not exist
if Handle.ExistsTable(tn) {
t.Errorf("table %s was found when it was not expected", tn)
}
}
// TestCreateTableBasic
//
// Create table depot via CreateTables(i ...interface{})
// Verify table creation via ExistsTable(tn string)
// Perform negative validation be checking for non-existent
//
// table "abcdefg" via ExistsTable(tn string)
func TestCreateTableBasic(t *testing.T) {
type Depot struct {
DepotNum int `db:"depot_num" sqac:"primary_key:inc"`
CreateDate time.Time `db:"create_date" sqac:"nullable:false;default:now();"`
Region string `db:"region" sqac:"nullable:false;default:YYC"`
Province string `db:"province" sqac:"nullable:false;default:AB"`
Country string `db:"country" sqac:"nullable:false;default:CA"`
}
err := Handle.CreateTables(Depot{})
if err != nil {
t.Errorf("%s", err.Error())
}
// determine the table name as per the table creation logic
tn := common.GetTableName(Depot{})
// expect that table depot exists
if !Handle.ExistsTable(tn) {
t.Errorf("table %s was not created", tn)
}
}
// TestDropTablesBasic
//
// Drop table depot via DropTables(i ...interface{})
func TestDropTablesBasic(t *testing.T) {
type Depot struct {
DepotNum int `db:"depot_num" sqac:"primary_key:inc"`
CreateDate time.Time `db:"create_date" sqac:"nullable:false;default:now();"`
Region string `db:"region" sqac:"nullable:false;default:YYC"`
Province string `db:"province" sqac:"nullable:false;default:AB"`
Country string `db:"country" sqac:"nullable:false;default:CA"`
}
err := Handle.DropTables(Depot{})
if err != nil {
t.Errorf("%s", err.Error())
}
// determine the table name as per the table creation logic
tn := common.GetTableName(Depot{})
// expect that table depot has been dropped
if Handle.ExistsTable(tn) {
t.Errorf("table %s was not dropped", tn)
}
err = Handle.DropTables(Depot{})
if err != nil {
t.Errorf("%s", err.Error())
}
}
func TestCreateTableCompoundKey(t *testing.T) {
type Depot struct {
DepotNum int `db:"depot_num" sqac:"primary_key:inc"`
DepotBay int `db:"depot_bay" sqac:"primary_key:"`
CreateDate time.Time `db:"create_date" sqac:"default:now()"`
ExpiryDate time.Time `db:"expiry_date" sqac:"default:eot()"`
Region string `db:"region" sqac:"nullable:true;default:YYC"`
Province string `db:"province" sqac:"nullable:false;default:AB"`
Country string `db:"country" sqac:"nullable:false;default:CA"`
}
err := Handle.CreateTables(Depot{})
if err != nil {
t.Errorf("%s", err.Error())
}
// determine the table name as per the table creation logic
tn := common.GetTableName(Depot{})
// expect that table depot exists
if !Handle.ExistsTable(tn) {
t.Errorf("table %s was not created", tn)
}
// create a new record via the CRUD Create call
var depot = Depot{
DepotBay: 1,
Region: "YYC",
Province: "AB",
Country: "CA",
}
err = Handle.Create(&depot)
if err != nil {
t.Errorf(err.Error())
}
depot.DepotBay = 1
depot.Region = "YEG"
err = Handle.Create(&depot)
if err != nil {
t.Errorf(err.Error())
}
// time.Sleep(15 * time.Second)
if Handle.IsLog() {
fmt.Printf("INSERTING: %v\n", depot)
fmt.Printf("TEST GOT: %v\n", depot)
}
err = Handle.DropTables(Depot{})
if err != nil {
t.Errorf("TestCreateTableCompoundKey: %s", err.Error())
}
}
// TestCreateTableNonIncKey
//
// Create table depot via CreateTables(i ...interface{})
// Verify table creation via ExistsTable(tn string)
// Perform negative validation be checking for non-existent
//
// table "abcdefg" via ExistsTable(tn string)
func TestCreateTableNonIncKey(t *testing.T) {
type Depot struct {
DepotNum int `db:"depot_num" sqac:"primary_key:"` // non-incrementing key
CreateDate time.Time `db:"create_date" sqac:"nullable:false;default:now();"`
Region string `db:"region" sqac:"nullable:false;default:YYC"`
Province string `db:"province" sqac:"nullable:false;default:AB"`
Country string `db:"country" sqac:"nullable:false;default:CA"`
}
// ensure table does not exist
err := Handle.DropTables(Depot{})
if err != nil {
t.Errorf("%s", err.Error())
}
err = Handle.CreateTables(Depot{})
if err != nil {
t.Errorf("%s", err.Error())
}
// determine the table name as per the table creation logic
tn := common.GetTableName(Depot{})
// expect that table depot exists
if !Handle.ExistsTable(tn) {
t.Errorf("table %s was not created", tn)
}
err = Handle.DropTables(Depot{})
if err != nil {
t.Errorf("%s", err.Error())
}
}
// TestCreateTableNoKey
//
// Create table depot via CreateTables(i ...interface{})
// Verify table creation via ExistsTable(tn string)
// Perform negative validation be checking for non-existent
//
// table "abcdefg" via ExistsTable(tn string)
func TestCreateTableNoKey(t *testing.T) {
type Depot struct {
DepotNum int `db:"depot_num" sqac:"nullable:false"` // not a key
CreateDate time.Time `db:"create_date" sqac:"nullable:false;default:now();"`
Region string `db:"region" sqac:"nullable:false;default:YYC"`
Province string `db:"province" sqac:"nullable:false;default:AB"`
Country string `db:"country" sqac:"nullable:false;default:CA"`
}
// ensure table does not exist
err := Handle.DropTables(Depot{})
if err != nil {
t.Errorf("%s", err.Error())
}
err = Handle.CreateTables(Depot{})
if err != nil {
t.Errorf("%s", err.Error())
}
// determine the table name as per the table creation logic
tn := common.GetTableName(Depot{})
// expect that table depot exists
if !Handle.ExistsTable(tn) {
t.Errorf("table %s was not created", tn)
}
err = Handle.DropTables(Depot{})
if err != nil {
t.Errorf("%s", err.Error())
}
}
// TestCreateTableWithAlterSequence
//
// Create table depot via CreateTables(i ...interface{})
// Verify table creation via ExistsTable(tn string)
// Perform negative validation be checking for non-existent
//
// table "abcdefg" via ExistsTable(tn string)
func TestCreateTableWithAlterSequence(t *testing.T) {
type Depot struct {
DepotNum int `db:"depot_num" sqac:"primary_key:inc;start:90000000"`
CreateDate time.Time `db:"create_date" sqac:"nullable:false;default:now()"`
Region string `db:"region" sqac:"nullable:false;default:YYC"`
Province string `db:"province" sqac:"nullable:false;default:AB"`
Country string `db:"country" sqac:"nullable:false;default:CA"`
}
err := Handle.CreateTables(Depot{})
if err != nil {
t.Errorf("%s", err.Error())
}
// determine the table name as per the table creation logic
tn := common.GetTableName(Depot{})
// expect that table depot exists
if !Handle.ExistsTable(tn) {
t.Errorf("table %s was not created", tn)
}
// check the next value of the auto-increment, sequence or
// identity field depending on db-system.
var hdbName string
var seq int
if Handle.GetDBDriverName() != "hdb" {
seq, err = Handle.GetNextSequenceValue(tn)
} else {
hdbName = fmt.Sprintf("SEQ_%s_%s", strings.ToUpper(tn), "DEPOT_NUM")
seq, err = Handle.GetNextSequenceValue(hdbName)
}
if err != nil {
t.Errorf(err.Error())
}
if seq != 90000000 {
t.Errorf("expected value of 90000000, got %d", seq)
}
// Drop the depot table
err = Handle.DropTables(Depot{})
if err != nil {
t.Errorf("%s", err.Error())
}
}
// TestCreateTablesWithInclude
//
// Create table equipment via CreateTables(i ...interface{})
// and verify that flat structs can be included in the
// table creation.
func TestCreateTablesWithInclude(t *testing.T) {
// type Triplet struct {
// TripOne string `db:"trip_one" sqac:"nullable:false"`
// TripTwo int64 `db:"trip_two" sqac:"nullable:false;default:0"`
// Tripthree string `db:"trip_three" sqac:"nullable:false"`
// }
// type Equipment struct {
// EquipmentNum int64 `db:"equipment_num" sqac:"primary_key:inc;start:55550000"`
// ValidFrom time.Time `db:"valid_from" sqac:"primary_key;nullable:false;default:now()"`
// ValidTo time.Time `db:"valid_to" sqac:"primary_key;nullable:false;default:make_timestamptz(9999, 12, 31, 23, 59, 59.9)"`
// CreatedAt time.Time `db:"created_at" sqac:"nullable:false;default:now()"`
// InspectionAt time.Time `db:"inspection_at" sqac:"nullable:true"`
// MaterialNum int `db:"material_num" sqac:"index:idx_material_num_serial_num"`
// Description string `db:"description" sqac:"sqac:nullable:false"`
// SerialNum string `db:"serial_num" sqac:"index:idx_material_num_serial_num"`
// IntExample int `db:"int_example" sqac:"nullable:false;default:0"`
// Int64Example int64 `db:"int64_example" sqac:"nullable:false;default:0"`
// Int32Example int32 `db:"int32_example" sqac:"nullable:false;default:0"`
// Int16Example int16 `db:"int16_example" sqac:"nullable:false;default:0"`
// Int8Example int8 `db:"int8_example" sqac:"nullable:false;default:0"`
// UIntExample uint `db:"uint_example" sqac:"nullable:false;default:0"`
// UInt64Example uint64 `db:"uint64_example" sqac:"nullable:false;default:0"`
// UInt32Example uint32 `db:"uint32_example" sqac:"nullable:false;default:0"`
// UInt16Example uint16 `db:"uint16_example" sqac:"nullable:false;default:0"`
// UInt8Example uint8 `db:"uint8_example" sqac:"nullable:false;default:0"`
// Float32Example float32 `db:"float32_example" sqac:"nullable:false;default:0.0"`
// Float64Example float64 `db:"float64_example" sqac:"nullable:false;default:0.0"`
// BoolExample bool `db:"bool_example" sqac:"nullable:false;default:false"`
// RuneExample rune `db:"rune_example" sqac:"nullable:true"`
// ByteExample byte `db:"byte_example" sqac:"nullable:true"`
// Triplet
// }
err := Handle.CreateTables(Equipment{})
if err != nil {
t.Errorf("%s", err.Error())
}
// determine the table name as per the table creation logic
tn := common.GetTableName(Equipment{})
// expect that table depot exists
if !Handle.ExistsTable(tn) {
t.Errorf("table %s was not created", tn)
}
// drop the equipment table
err = Handle.DropTables(Equipment{})
if err != nil {
t.Errorf("%s", err.Error())
}
}
// TestCreateUniqueColumnConstraintFromModel
//
// Create table depot via CreateTables(i ...interface{})
// Create a single-field non-unique index based on model
// attributes.
func TestCreateUniqueColumnConstraintFromModel(t *testing.T) {
type DepotConstraint struct {
DepotNum int `db:"depot_num" sqac:"primary_key:inc;start:90000000"`
CreateDate time.Time `db:"create_date" sqac:"nullable:false;default:now();index:non-unique"`
Region string `db:"region" sqac:"nullable:false;default:YYC"`
Province string `db:"province" sqac:"nullable:false;default:AB"`
Country string `db:"country" sqac:"nullable:false;default:CA"`
LotID uint `db:"lot_id" sqac:"nullable:false;constraint:unique"`
}
err := Handle.DropTables(DepotConstraint{})
if err != nil {
t.Errorf("%s", err.Error())
}
err = Handle.CreateTables(DepotConstraint{})
if err != nil {
t.Errorf("%s", err.Error())
}
// determine the table name as per the table creation logic
tn := common.GetTableName(DepotConstraint{})
// expect that table depot exists
if !Handle.ExistsTable(tn) {
t.Errorf("table %s was not created", tn)
}
// if !Handle.ExistsIndex(tn, "idx_depot_create_date") {
// t.Errorf("expected unique index idx_depot_create_date - got: ")
// }
// drop the depotconstraint table
err = Handle.DropTables(DepotConstraint{})
if err != nil {
t.Errorf("%s", err.Error())
}
}
// TestExistsIndexNegative
//
// Check to see if index:
// idx_province_country exists on
// table depot.
func TestExistsIndexNegative(t *testing.T) {
type Depot struct {
DepotNum int `db:"depot_num" sqac:"primary_key:inc;start:90000000"`
CreateDate time.Time `db:"create_date" sqac:"nullable:false;default:now()"`
Region string `db:"region" sqac:"nullable:false;default:YYC"`
Province string `db:"province" sqac:"nullable:false;default:AB"`
Country string `db:"country" sqac:"nullable:false;default:CA"`
NewColumn1 string `db:"new_column1" sqac:"nullable:false"`
NewColumn2 int64 `db:"new_column2" sqac:"nullable:false;default:0"`
NewColumn3 float64 `db:"new_column3" sqac:"nullable:false;default:0.0"`
}
// drop table depot
// err := Handle.DropTables(Depot{})
// if err != nil {
// t.Errorf("%s", err.Error())
// }
// ensure that table depot exists
err := Handle.AlterTables(Depot{})
if err != nil {
t.Errorf("%s", err.Error())
}
// determine the table name as per the table creation logic
tn := common.GetTableName(Depot{})
// expect that table depot exists
if !Handle.ExistsTable(tn) {
t.Errorf("table %s does not exist", tn)
}
if Handle.ExistsIndex(tn, "idx_depot_province_country") {
t.Errorf("index %s was found on table %s, but was not expected", "idx_depot_province_country", tn)
}
// drop the depot table
err = Handle.DropTables(Depot{})
if err != nil {
t.Errorf("%s", err.Error())
}
}
// TestCreateSingleUniqueIndexFromModel
//
// Create table depot via CreateTables(i ...interface{})
// Create a single-field unique index based on model
// attributes.
func TestCreateSingleUniqueIndexFromModel(t *testing.T) {
type Depot struct {
DepotNum int `db:"depot_num" sqac:"primary_key:inc;start:90000000"`
CreateDate time.Time `db:"create_date" sqac:"nullable:false;default:now();index:unique"`
Region string `db:"region" sqac:"nullable:false;default:YYC"`
Province string `db:"province" sqac:"nullable:false;default:AB"`
Country string `db:"country" sqac:"nullable:false;default:CA"`
NewColumn1 string `db:"new_column1" sqac:"nullable:false"`
NewColumn2 int64 `db:"new_column2" sqac:"nullable:false;default:0"`
NewColumn3 float64 `db:"new_column3" sqac:"nullable:false;default:0.0"`
}
err := Handle.CreateTables(Depot{})
if err != nil {
t.Errorf("%s", err.Error())
}
// determine the table name as per the table creation logic
tn := common.GetTableName(Depot{})
// expect that table depot exists
if !Handle.ExistsTable(tn) {
t.Errorf("table %s was not created", tn)
}
if !Handle.ExistsIndex(tn, "idx_depot_create_date") {
t.Errorf("expected unique index idx_depot_create_date - got: ")
}
// drop the depot table
err = Handle.DropTables(Depot{})
if err != nil {
t.Errorf("%s", err.Error())
}
}
// TestCreateSingleNonUniqueIndexFromModel
//
// Create table depot via CreateTables(i ...interface{})
// Create a single-field non-unique index based on model
// attributes.
func TestCreateSingleNonUniqueIndexFromModel(t *testing.T) {
type Depot struct {
DepotNum int `db:"depot_num" sqac:"primary_key:inc;start:90000000"`
CreateDate time.Time `db:"create_date" sqac:"nullable:false;default:now();index:non-unique"`
Region string `db:"region" sqac:"nullable:false;default:YYC"`
Province string `db:"province" sqac:"nullable:false;default:AB"`
Country string `db:"country" sqac:"nullable:false;default:CA"`
NewColumn1 string `db:"new_column1" sqac:"nullable:false"`
NewColumn2 int64 `db:"new_column2" sqac:"nullable:false;default:0"`
NewColumn3 float64 `db:"new_column3" sqac:"nullable:false;default:0.0"`
}
err := Handle.CreateTables(Depot{})
if err != nil {
t.Errorf("%s", err.Error())
}
// determine the table name as per the table creation logic
tn := common.GetTableName(Depot{})
// expect that table depot exists
if !Handle.ExistsTable(tn) {
t.Errorf("table %s was not created", tn)
}
if !Handle.ExistsIndex(tn, "idx_depot_create_date") {
t.Errorf("expected unique index idx_depot_create_date - got: ")
}
// drop the depot table
err = Handle.DropTables(Depot{})
if err != nil {
t.Errorf("%s", err.Error())
}
}
// TestCreateSimpleCompositeIndex
//
// Create table depot via CreateTables(i ...interface{})
// Create an index not based on model attributes, but
// based on a constructed sqac.IndexInfo struct.
func TestCreateSimpleCompositeIndex(t *testing.T) {
type Depot struct {
DepotNum int `db:"depot_num" sqac:"primary_key:inc;start:90000000"`
CreateDate time.Time `db:"create_date" sqac:"nullable:false;default:now()"`
Region string `db:"region" sqac:"nullable:false;default:YYC"`
Province string `db:"province" sqac:"nullable:false;default:AB"`
Country string `db:"country" sqac:"nullable:false;default:CA"`
NewColumn1 string `db:"new_column1" sqac:"nullable:false"`
NewColumn2 int64 `db:"new_column2" sqac:"nullable:false;default:0"`
NewColumn3 float64 `db:"new_column3" sqac:"nullable:false;default:0.0"`
}
err := Handle.CreateTables(Depot{})
if err != nil {
t.Errorf("%s", err.Error())
}
// determine the table name as per the table creation logic
tn := common.GetTableName(Depot{})
// expect that table depot exists
if !Handle.ExistsTable(tn) {
t.Errorf("table %s was not created", tn)
}
var indexInfo sqac.IndexInfo
indexInfo.TableName = tn
indexInfo.Unique = false
indexInfo.IndexFields = []string{"province", "country"}
err = Handle.CreateIndex("idx_depot_province_country", indexInfo)
if err != nil {
t.Errorf("%s", err.Error())
}
}
// TestExistsIndexPositive
//
// Check to see if index:
// idx_province_country exists on
// table depot.
func TestExistsIndexPositive(t *testing.T) {
type Depot struct {
DepotNum int `db:"depot_num" sqac:"primary_key:inc;start:90000000"`
CreateDate time.Time `db:"create_date" sqac:"nullable:false;default:now();index:unique"`
Region string `db:"region" sqac:"nullable:false;default:YYC"`
Province string `db:"province" sqac:"nullable:false;default:AB"`
Country string `db:"country" sqac:"nullable:false;default:CA"`
NewColumn1 string `db:"new_column1" sqac:"nullable:false"`
NewColumn2 int64 `db:"new_column2" sqac:"nullable:false;default:0"`
NewColumn3 float64 `db:"new_column3" sqac:"nullable:false;default:0.0"`
}
// determine the table name as per the table creation logic
tn := common.GetTableName(Depot{})
// expect that table depot exists
if !Handle.ExistsTable(tn) {
t.Errorf("table %s does not exist", tn)
}
if !Handle.ExistsIndex(tn, "idx_depot_province_country") {
t.Errorf("index %s was not found on table %s", "idx_depot_province_country", tn)
}
}
// TestDropIndex
//
// Drop index "idx_province_country" on table depot via
// DropIndex(in string)
// Call ExistsIndex(tn string, in string) bool to
// verify that the index has been dropped.
func TestDropIndex(t *testing.T) {
type Depot struct {
DepotNum int `db:"depot_num" sqac:"primary_key:inc;start:90000000"`
CreateDate time.Time `db:"create_date" sqac:"nullable:false;default:now();index:unique"`
Region string `db:"region" sqac:"nullable:false;default:YYC"`
Province string `db:"province" sqac:"nullable:false;default:AB"`
Country string `db:"country" sqac:"nullable:false;default:CA"`
NewColumn1 string `db:"new_column1" sqac:"nullable:false"`
NewColumn2 int64 `db:"new_column2" sqac:"nullable:false;default:0"`
NewColumn3 float64 `db:"new_column3" sqac:"nullable:false;default:0.0"`
}
// ensure table depot exists
err := Handle.AlterTables(Depot{})
if err != nil {
t.Errorf("%s", err.Error())
}
// determine the table name as per the table creation logic
tn := common.GetTableName(Depot{})
if !Handle.ExistsIndex(tn, "idx_depot_province_country") {
t.Errorf("index %s was not found on table %s", "idx_depot_province_country", tn)
}
err = Handle.DropIndex(tn, "idx_depot_province_country")
if err != nil {
t.Errorf("%s", err.Error())
}
if Handle.ExistsIndex(tn, "idx_depot_province_country") {
t.Errorf("drop of index %s did not succeed on table %s", "idx_province_country", tn)
}
err = Handle.DropTables(Depot{})
if err != nil {
t.Errorf("%s", err.Error())
}
}
// TestCreateCompositeIndexFromModel
//
// Create table depot via CreateTables(i ...interface{})
// Create an index not based on model attributes, but
// based on a constructed sqac.IndexInfo struct.
func TestCreateCompositeIndexFromModel(t *testing.T) {
type Depot struct {
DepotNum int `db:"depot_num" sqac:"primary_key:inc;start:90000000"`
CreateDate time.Time `db:"create_date" sqac:"nullable:false;default:now()"`
Region string `db:"region" sqac:"nullable:false;default:YYC"`
Province string `db:"province" sqac:"nullable:false;default:AB"`
Country string `db:"country" sqac:"nullable:false;default:CA"`
NewColumn1 string `db:"new_column1" sqac:"nullable:false;index:idx_depot_new_column1_new_column2"`
NewColumn2 int64 `db:"new_column2" sqac:"nullable:false;default:0;index:idx_depot_new_column1_new_column2"`
NewColumn3 float64 `db:"new_column3" sqac:"nullable:false;default:0.0"`
}
err := Handle.CreateTables(Depot{})
if err != nil {
t.Errorf("%s", err.Error())
}
// determine the table name as per the table creation logic
tn := common.GetTableName(Depot{})
// expect that table depot exists
if !Handle.ExistsTable(tn) {
t.Errorf("table %s was not created", tn)
}
if !Handle.ExistsIndex(tn, "idx_depot_new_column1_new_column2") {
t.Errorf("index %s was not found on table %s", "idx_depot_new_column1_new_column2", tn)
}
err = Handle.DropTables(Depot{})
if err != nil {
t.Errorf("%s", err.Error())
}
}
// TestExistsColumn
//
// Verify that table depot exists on the database
// by calling AlterTables(Depot{})
// Verify that column 'region' exists in the table
// via ExistsColumn(tn string, cn string) bool.
// Verify that column 'footle' does not exist in the
// table via ExistsColumn(tn string, cn string) bool.
func TestExistsColumn(t *testing.T) {
type Depot struct {
DepotNum int `db:"depot_num" sqac:"primary_key:inc;start:90000000"`
CreateDate time.Time `db:"create_date" sqac:"nullable:false;default:now();index:unique"`
Region string `db:"region" sqac:"nullable:false;default:YYC;index:non-unique"`
Province string `db:"province" sqac:"nullable:false;default:AB"`
Country string `db:"country" sqac:"nullable:true;default:CA"`
}
// determine the table name as per the table creation logic
tn := common.GetTableName(Depot{})
// ensure table exists in db - create via alter
err := Handle.AlterTables(Depot{})
if err != nil {
t.Errorf("%s", err.Error())
}
// expect that table depot exists
if !Handle.ExistsTable(tn) {
t.Errorf("table %s does not exist", tn)
}
// check for column 'region'
if !Handle.ExistsColumn(tn, "region") {
t.Errorf("column %s should have been found in table %s", "region", tn)
}
// check for column 'footle'
if Handle.ExistsColumn(tn, "footle") {
t.Errorf("column %s should not have been found in table %s", "footle", tn)
}
}
// TestAlterTables
//
// Alter table depot via AlterTables(i ...interface{})
// Add three columns:
// - NewColumn1 string `db:"new_column1" sqac:"nullable:false"`
// - NewColumn2 int64 `db:"new_column2" sqac:"nullable:false;default:0"`
// - NewColumn3 float64 `db:"new_column3" sqac:"nullable:false;default:0.0"`
func TestAlterTables(t *testing.T) {
type Depot struct {
DepotNum int `db:"depot_num" sqac:"primary_key:inc;start:90000000"`
CreateDate time.Time `db:"create_date" sqac:"nullable:false;default:now();index:unique"`
Region string `db:"region" sqac:"nullable:false;default:YYC;index:non-unique"`
Province string `db:"province" sqac:"nullable:false;default:AB"`
Country string `db:"country" sqac:"nullable:false;default:CA"`
NewColumn1 string `db:"new_column1" sqac:"nullable:false;default:nc1_default;index:non-unique"`
NewColumn2 int64 `db:"new_column2" sqac:"nullable:false;default:0;index:idx_new_column2_new_column3"`
NewColumn3 float64 `db:"new_column3" sqac:"nullable:false;default:0.0;index:idx_new_column2_new_column3"`
}
err := Handle.AlterTables(Depot{})
if err != nil {
t.Errorf("%s", err.Error())
}
// determine the table name as per the table creation logic
tn := common.GetTableName(Depot{})
if !Handle.ExistsColumn("depot", "new_column1") {
t.Errorf("new_column1 was expected to exist but does not ")
}
if !Handle.ExistsColumn("depot", "new_column2") {
t.Errorf("new_column2 was expected to exist but does not ")
}
if !Handle.ExistsColumn("depot", "new_column3") {
t.Errorf("new_column3 was expected to exist but does not ")
}
r := Handle.ExistsIndex(tn, "idx_new_column2_new_column3")
if r == false {
t.Errorf("index idx_new_column2_new_column3 was not not found following alter table call on table %s", tn)
}
r = Handle.ExistsIndex(tn, "idx_depot_region")
if r == false {
t.Errorf("index idx_depot_region was not not found following alter table call on table %s", tn)
}
}
// TestDestructiveResetTables
//
// Verify that tables depot and equipment exist on the db
// by calling AlterTables(i ...interface{})
// Drop and recreate tables depot and equipment on the db
// by calling DestructiveResetTables(i ...interface{})
// Select * from db tables depot and equipment to verify
// that they exist and contain no records.
func TestDestructiveResetTables(t *testing.T) {
type Depot struct {
DepotNum int `db:"depot_num" sqac:"primary_key:inc;start:90000000"`
CreateDate time.Time `db:"create_date" sqac:"nullable:false;default:now();index:unique"`
Region string `db:"region" sqac:"nullable:false;default:YYC"`
Province string `db:"province" sqac:"nullable:false;default:AB"`
Country string `db:"country" sqac:"nullable:false;default:CA"`
Active bool `db:"active" sqac:"nullable:false;default:true"`
}
// determine the table names
tns := make([]string, 0)
tn := common.GetTableName(Depot{})
tns = append(tns, tn)
tn = common.GetTableName(Equipment{})
tns = append(tns, tn)
// ensure tables exist in db
err := Handle.AlterTables(Depot{}, Equipment{})
if err != nil {
t.Errorf("%s", err.Error())
}
// expect that tables depot and equipment exist
for _, n := range tns {
if !Handle.ExistsTable(n) {
t.Errorf("table %s does not exist", n)
}
}
// drop both tables via DestructiveReset
err = Handle.DestructiveResetTables(Depot{}, Equipment{})
if err != nil {
t.Errorf("%s", err.Error())
}
// expect that tables depot and equipment have been recreated
for _, n := range tns {
if !Handle.ExistsTable(n) {
t.Errorf("table %s does not exist", n)
}
}
d := []Depot{}
err = Handle.Select(&d, "SELECT * FROM depot;")
if err != nil {
t.Errorf("error reading from table depot - got %s\n", err.Error())
}
if len(d) > 0 {
t.Errorf("table depot contained records after attempted DestructiveReset - got %v\n", d)
}
e := []Equipment{}
err = Handle.Select(&e, "SELECT * FROM equipment;")
if err != nil {
t.Errorf("error reading from table equipment - got %s\n", err.Error())
}
if len(e) > 0 {
t.Errorf("table equipment contained records after attempted DestructiveReset - got %v\n", d)
}
err = Handle.DropTables(Depot{})
if err != nil {
t.Errorf("%s", err.Error())
}
}
// TestQueryOps
//
// This test is designed to illustrate the handling of database reads when dealing with db fields that
// contain null values. Create db table depot based on an updated Depot struct containing a number of
// nullable and non-defaulted fields.
// Insert a new record containing null-values into db table depot.
// Declare struct DepotN{} as a parallel structure to Depot{} making use of sql.Null<type> fields in