-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsqlize_test.go
1037 lines (940 loc) · 30.7 KB
/
sqlize_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 sqlize
import (
"encoding/json"
"errors"
"reflect"
"regexp"
"strings"
"testing"
"time"
"github.com/sunary/sqlize/utils"
)
type Base struct {
CreatedAt time.Time
UpdatedAt time.Time
}
type person struct {
ID int32 `sql:"primary_key;auto_increment"`
Name string `sql:"type:VARCHAR(64);unique;index:name,age"`
Alias string `sql:"-"`
Age int
IsFemale bool
CreatedAt time.Time `sql:"default:CURRENT_TIMESTAMP"`
}
type anotherPerson struct {
ID int32 `sql:"primary_key;auto_increment"`
Name string `sql:"type:VARCHAR(64);index:name,age;unique"`
Alias string `sql:"-"`
Age int
IsFemale bool
CreatedAt time.Time `sql:"default:CURRENT_TIMESTAMP"`
}
func (anotherPerson) TableName() string {
return "another_person"
}
type hotel struct {
B1 Base `sql:"embedded"`
B2 Base `sql:"embedded_prefix:base_"`
ID int32 `sql:"primary_key"`
Name string
GrandOpening *time.Time
}
type city struct {
ID int32 `sql:"primary_key;auto_increment"`
Name string `sql:"column:name"`
Region string `sql:"type:ENUM('northern','southern');default:'northern'"`
}
type movie struct {
ID int32 `sql:"primary_key;auto_increment"`
Title string `sql:"type:varchar(255)"`
Director string `sql:"column:director;type:varchar(255)"`
YearReleased string `sql:"column:year_released,previous:released_at"`
}
type order struct {
B1 Base `sql:"embedded"`
ClientID string `sql:"type:varchar(255);primary_key;index_columns:client_id,country"`
Country string `sql:"type:varchar(255)"`
Email string `sql:"type:varchar(255);unique"`
User *user `sql:"foreign_key:email;references:email"`
}
func (order) TableName() string {
return "orders"
}
type order_sqlite struct {
B1 Base `sql:"embedded"`
ClientID string `sql:"type:TEXT;primary_key;index_columns:client_id,country"`
Country string `sql:"type:TEXT"`
Email string `sql:"type:TEXT;unique"`
}
func (order_sqlite) TableName() string {
return "orders_sqlite"
}
type user struct {
Email string
}
var (
space = regexp.MustCompile(`\s+`)
createAnotherPersonStm = `CREATE TABLE another_person (
id int(11) AUTO_INCREMENT PRIMARY KEY,
name varchar(64),
age int(11),
is_female tinyint(1),
created_at datetime DEFAULT CURRENT_TIMESTAMP()
);`
alterAnotherPersonUpStm = `
CREATE UNIQUE INDEX idx_name_age ON another_person(name, age);`
alterAnotherPersonDownStm = `
DROP INDEX idx_name_age ON another_person;`
createPersonStm = `CREATE TABLE person (
id int(11) AUTO_INCREMENT PRIMARY KEY,
name varchar(64),
age int(11),
is_female tinyint(1),
created_at datetime DEFAULT CURRENT_TIMESTAMP()
);`
alterPersonUpStm = `
CREATE UNIQUE INDEX idx_name_age ON person(name, age);`
alterPersonDownStm = `
DROP INDEX idx_name_age ON person;`
createHotelStm = `
CREATE TABLE hotel (
id int(11) PRIMARY KEY,
name text,
star tinyint(4),
grand_opening datetime NULL,
created_at datetime,
updated_at datetime,
base_created_at datetime,
base_updated_at datetime
);`
alterHotelUpStm = `
ALTER TABLE hotel DROP COLUMN star;`
alterHotelDownStm = `
ALTER TABLE hotel ADD COLUMN star tinyint(4) AFTER name;`
createCityStm = `
CREATE TABLE city (
code varchar(3),
id int(11) AUTO_INCREMENT PRIMARY KEY,
region enum('northern','southern') DEFAULT 'northern'
);`
alterCityUpStm = `
ALTER TABLE city DROP COLUMN code;
ALTER TABLE city ADD COLUMN name text AFTER id;`
alterCityDownStm = `
ALTER TABLE city ADD COLUMN code varchar(3) FIRST;
ALTER TABLE city DROP COLUMN name;`
createMovieStm = `
CREATE TABLE movie (
id int(11) AUTO_INCREMENT PRIMARY KEY,
title varchar(255),
director varchar(255)
);`
alterMovieUpStm = `
ALTER TABLE movie RENAME COLUMN released_at TO year_released;`
alterMovieDownStm = `
ALTER TABLE movie RENAME COLUMN year_released TO released_at;`
expectCreateAnotherPersonUp = `
CREATE TABLE another_person (
id int(11) AUTO_INCREMENT PRIMARY KEY,
name varchar(64),
age int(11),
is_female tinyint(1),
created_at datetime DEFAULT CURRENT_TIMESTAMP()
);
CREATE UNIQUE INDEX idx_name_age ON another_person(name, age);`
expectCreateAnotherPersonDown = `
DROP TABLE IF EXISTS another_person;`
expectCreatePersonUp = `
CREATE TABLE person (
id int(11) AUTO_INCREMENT PRIMARY KEY,
name varchar(64),
age int(11),
is_female tinyint(1),
created_at datetime DEFAULT CURRENT_TIMESTAMP()
);
CREATE UNIQUE INDEX idx_name_age ON person(name, age);`
expectCreatePersonDown = `
DROP TABLE IF EXISTS person;`
expectCreateHotelUp = `
CREATE TABLE hotel (
id int(11) PRIMARY KEY,
name text,
grand_opening datetime NULL,
created_at datetime,
updated_at datetime,
base_created_at datetime,
base_updated_at datetime
);`
expectCreateHotelDown = `
DROP TABLE IF EXISTS hotel;`
expectCreateCityUp = `
CREATE TABLE city (
id int(11) AUTO_INCREMENT PRIMARY KEY,
name text,
region enum('northern','southern') DEFAULT 'northern'
);`
expectCreateCityHasCommentUp = `
CREATE TABLE city (
id int(11) AUTO_INCREMENT PRIMARY KEY,
name text COMMENT 'name',
region enum('northern','southern') DEFAULT 'northern' COMMENT 'enum values: northern, southern'
);`
expectCreateCityDown = `
DROP TABLE IF EXISTS city;`
expectCreateOrderUp = `
CREATE TABLE orders (
client_id varchar(255) COMMENT 'client id',
country varchar(255) COMMENT 'country',
email varchar(255) COMMENT 'email',
created_at datetime,
updated_at datetime
);
ALTER TABLE orders ADD PRIMARY KEY(client_id, country);
CREATE UNIQUE INDEX idx_email ON orders(email);
ALTER TABLE orders ADD CONSTRAINT fk_user_orders FOREIGN KEY (email) REFERENCES user(email);`
expectCreateOrderDown = `
DROP TABLE IF EXISTS orders;`
expectCreateOrderPostgresUp = `
CREATE TABLE orders (
client_id VARCHAR(255),
country VARCHAR(255),
email VARCHAR(255),
created_at TIMESTAMP,
updated_at TIMESTAMP
);
COMMENT ON COLUMN orders.client_id IS 'client id';
COMMENT ON COLUMN orders.country IS 'country';
COMMENT ON COLUMN orders.email IS 'email';
ALTER TABLE orders ADD PRIMARY KEY(client_id, country);
CREATE UNIQUE INDEX idx_email ON orders(email);
ALTER TABLE orders ADD CONSTRAINT fk_user_orders FOREIGN KEY (email) REFERENCES "user"(email);`
expectCreateOrderPostgresDown = `
DROP TABLE IF EXISTS orders;`
expectCreateOrderSqliteUp = `
CREATE TABLE orders_sqlite (
client_id TEXT,
country TEXT,
email TEXT,
created_at TEXT,
updated_at TEXT
);`
expectCreateOrderSqliteDown = `
DROP TABLE IF EXISTS orders_sqlite;`
expectPersonMermaidJsErd = `erDiagram
PERSON {
int(11) id PK
varchar(64) name
int(11) age
tinyint(1) is_female
datetime created_at
}`
expectHotelMermaidJsErd = `erDiagram
HOTEL {
int(11) id PK
text name
datetime grand_opening
datetime created_at
datetime updated_at
datetime base_created_at
datetime base_updated_at
}`
expectPersonCityMermaidJsErd = `erDiagram
PERSON {
int(11) id PK
varchar(64) name
int(11) age
tinyint(1) is_female
datetime created_at
}
CITY {
int(11) id PK
text name
enum region
}`
expectOrderUserMermaidJsErd = `erDiagram
ORDERS {
varchar(255) client_id
varchar(255) country
varchar(255) email FK
datetime created_at
datetime updated_at
}
USER {
text email
}
ORDERS }o--|| USER: email`
expectPersonMermaidJsLive = `https://mermaid.ink/img/ZXJEaWFncmFtCiBQRVJTT04gewogIGludCgxMSkgaWQgUEsgCiAgdmFyY2hhcig2NCkgbmFtZSAgCiAgaW50KDExKSBhZ2UgIAogIHRpbnlpbnQoMSkgaXNfZmVtYWxlICAKICBkYXRldGltZSBjcmVhdGVkX2F0ICAKIH0K`
expectHotelMermaidJsLive = `https://mermaid.ink/img/ZXJEaWFncmFtCiBIT1RFTCB7CiAgaW50KDExKSBpZCBQSyAKICB0ZXh0IG5hbWUgIAogIGRhdGV0aW1lIGdyYW5kX29wZW5pbmcgIAogIGRhdGV0aW1lIGNyZWF0ZWRfYXQgIAogIGRhdGV0aW1lIHVwZGF0ZWRfYXQgIAogIGRhdGV0aW1lIGJhc2VfY3JlYXRlZF9hdCAgCiAgZGF0ZXRpbWUgYmFzZV91cGRhdGVkX2F0ICAKIH0K`
expectPersonCityMermaidJsLive = `https://mermaid.ink/img/ZXJEaWFncmFtCiBQRVJTT04gewogIGludCgxMSkgaWQgUEsgCiAgdmFyY2hhcig2NCkgbmFtZSAgCiAgaW50KDExKSBhZ2UgIAogIHRpbnlpbnQoMSkgaXNfZmVtYWxlICAKICBkYXRldGltZSBjcmVhdGVkX2F0ICAKIH0KIENJVFkgewogIGludCgxMSkgaWQgUEsgCiAgdGV4dCBuYW1lICAKICBlbnVtIHJlZ2lvbiAgCiB9Cg==`
expectOrderUserMermaidJsLive = `https://mermaid.ink/img/ZXJEaWFncmFtCiBPUkRFUlMgewogIHZhcmNoYXIoMjU1KSBjbGllbnRfaWQgIAogIHZhcmNoYXIoMjU1KSBjb3VudHJ5ICAKICB2YXJjaGFyKDI1NSkgZW1haWwgRksgCiAgZGF0ZXRpbWUgY3JlYXRlZF9hdCAgCiAgZGF0ZXRpbWUgdXBkYXRlZF9hdCAgCiB9CiBVU0VSIHsKICB0ZXh0IGVtYWlsICAKIH0KIE9SREVSUyB9by0tfHwgVVNFUjogZW1haWw=`
expectPersonArvo = `
{"type":"record","name":"person","namespace":"person","fields":[{"name":"before","type":["null",{"type":"record","name":"Value","namespace":"","fields":[{"name":"id","type":"int"},{"name":"name","type":"string"},{"name":"age","type":"int"},{"name":"is_female","type":"bool"},{"name":"created_at","type":["null",{"connect.default":"1970-01-01T00:00:00Z","connect.name":"io.debezium.time.ZonedTimestamp","connect.version":1,"type":"string"}]}],"connect.name":""}]},{"name":"after","type":["null","Value"]},{"name":"op","type":"string"},{"name":"ts_ms","type":["null","long"]},{"name":"transaction","type":["null",{"type":"record","name":"ConnectDefault","namespace":"io.confluent.connect.avro","fields":[{"name":"id","type":"string"},{"name":"total_order","type":"long"},{"name":"data_collection_order","type":"long"}],"connect.name":""}]}],"connect.name":"person"}`
expectHotelArvo = `
{"type":"record","name":"hotel","namespace":"hotel","fields":[{"name":"before","type":["null",{"type":"record","name":"Value","namespace":"","fields":[{"name":"id","type":"int"},{"name":"name","type":"string"},{"name":"grand_opening","type":{"connect.default":"1970-01-01T00:00:00Z","connect.name":"io.debezium.time.ZonedTimestamp","connect.version":1,"type":"string"}},{"name":"created_at","type":{"connect.default":"1970-01-01T00:00:00Z","connect.name":"io.debezium.time.ZonedTimestamp","connect.version":1,"type":"string"}},{"name":"updated_at","type":{"connect.default":"1970-01-01T00:00:00Z","connect.name":"io.debezium.time.ZonedTimestamp","connect.version":1,"type":"string"}},{"name":"base_created_at","type":{"connect.default":"1970-01-01T00:00:00Z","connect.name":"io.debezium.time.ZonedTimestamp","connect.version":1,"type":"string"}},{"name":"base_updated_at","type":{"connect.default":"1970-01-01T00:00:00Z","connect.name":"io.debezium.time.ZonedTimestamp","connect.version":1,"type":"string"}}],"connect.name":""}]},{"name":"after","type":["null","Value"]},{"name":"op","type":"string"},{"name":"ts_ms","type":["null","long"]},{"name":"transaction","type":["null",{"type":"record","name":"ConnectDefault","namespace":"io.confluent.connect.avro","fields":[{"name":"id","type":"string"},{"name":"total_order","type":"long"},{"name":"data_collection_order","type":"long"}],"connect.name":""}]}],"connect.name":"hotel"}`
expectCityArvo = `
{"type":"record","name":"city","namespace":"city","fields":[{"name":"before","type":["null",{"type":"record","name":"Value","namespace":"","fields":[{"name":"id","type":"int"},{"name":"name","type":"string"},{"name":"region","type":["null",{"connect.default":"init","connect.name":"io.debezium.data.Enum","connect.parameters":{"allowed":"northern,southern"},"connect.version":1,"type":"string"}]}],"connect.name":""}]},{"name":"after","type":["null","Value"]},{"name":"op","type":"string"},{"name":"ts_ms","type":["null","long"]},{"name":"transaction","type":["null",{"type":"record","name":"ConnectDefault","namespace":"io.confluent.connect.avro","fields":[{"name":"id","type":"string"},{"name":"total_order","type":"long"},{"name":"data_collection_order","type":"long"}],"connect.name":""}]}],"connect.name":"city"}`
expectCreateMigrationTableUp = `CREATE TABLE IF NOT EXISTS schema_migrations (
version bigint(20) PRIMARY KEY,
dirty BOOLEAN
);`
expectCreateMigrationTableDown = `
DROP TABLE IF EXISTS schema_migrations;`
expectMigrationVersion1Up = `
DELETE FROM schema_migrations LIMIT 1;
INSERT INTO schema_migrations (version, dirty) VALUES (1, false);`
expectMigrationVersion1Down = `
DELETE FROM schema_migrations LIMIT 1;`
)
func TestSqlize_FromObjects(t *testing.T) {
now := time.Now()
type args struct {
objs []interface{}
migrationFolder string
}
fromObjectMysqlTestcase := []struct {
name string
generateComment bool
pluralTableName bool
args args
wantMigrationUp string
wantMigrationDown string
wantErr bool
}{
{
name: "from anotherPerson object",
pluralTableName: true,
args: args{
[]interface{}{anotherPerson{}},
"",
},
wantMigrationUp: expectCreateAnotherPersonUp,
wantMigrationDown: expectCreateAnotherPersonDown,
wantErr: false,
},
{
name: "from person object",
args: args{
[]interface{}{person{}},
"",
},
wantMigrationUp: expectCreatePersonUp,
wantMigrationDown: expectCreatePersonDown,
wantErr: false,
},
{
name: "from hotel object",
args: args{
[]interface{}{hotel{GrandOpening: &now}},
"",
},
wantMigrationUp: expectCreateHotelUp,
wantMigrationDown: expectCreateHotelDown,
wantErr: false,
},
{
name: "from city object",
generateComment: true,
args: args{
[]interface{}{city{}},
"/",
},
wantMigrationUp: expectCreateCityHasCommentUp,
wantMigrationDown: expectCreateCityDown,
wantErr: false,
},
{
name: "from order object",
generateComment: true,
args: args{
[]interface{}{order{}},
"/",
},
wantMigrationUp: expectCreateOrderUp,
wantMigrationDown: expectCreateOrderDown,
wantErr: false,
},
{
name: "from all object",
args: args{
[]interface{}{person{}, hotel{GrandOpening: &now}, city{}},
"/",
},
wantMigrationUp: joinSql(expectCreatePersonUp, expectCreateHotelUp, expectCreateCityUp),
wantMigrationDown: joinSql(expectCreatePersonDown, expectCreateHotelDown, expectCreateCityDown),
wantErr: false,
},
}
for i, tt := range fromObjectMysqlTestcase {
t.Run(tt.name, func(t *testing.T) {
opts := []SqlizeOption{
WithMigrationSuffix(".up.test", ".down.test"), WithMigrationFolder(tt.args.migrationFolder),
}
if tt.generateComment {
opts = append(opts, WithCommentGenerate())
}
if tt.pluralTableName {
opts = append(opts, WithPluralTableName())
}
if i%3 == 1 {
opts = append(opts, WithSqlserver()) //fallback mysql
}
s := NewSqlize(opts...)
if tt.args.migrationFolder == "" {
if err := s.FromMigrationFolder(); err == nil {
t.Errorf("FromMigrationFolder() mysql error = %v,\n wantErr = %v", err, utils.PathDoesNotExistErr)
}
} else if tt.args.migrationFolder == "/" {
if err := s.FromMigrationFolder(); err != nil {
t.Errorf("FromMigrationFolder() mysql error = %v,\n wantErr = %v", err, nil)
}
}
if err := s.FromObjects(tt.args.objs...); (err != nil) != tt.wantErr {
t.Errorf("FromObjects() mysql error = %v,\n wantErr = %v", err, tt.wantErr)
}
if strUp := s.StringUp(); normSql(strUp) != normSql(tt.wantMigrationUp) {
t.Errorf("StringUp() mysql got = \n%s,\nexpected = \n%s", strUp, tt.wantMigrationUp)
}
if strDown := s.StringDown(); normSql(strDown) != normSql(tt.wantMigrationDown) {
t.Errorf("StringDown() mysql got = \n%s,\nexpected = \n%s", strDown, tt.wantMigrationDown)
}
if tt.args.migrationFolder == "" {
if err := s.WriteFiles(tt.name); err != nil {
t.Errorf("WriteFiles() mysql error = \n%v,\nwantErr = \n%v", err, nil)
}
} else if tt.args.migrationFolder == "/" {
if err := s.WriteFiles(tt.name); err == nil {
t.Errorf("WriteFiles() mysql error = \n%v,\nwantErr = \n%v", err, errors.New("read-only file system"))
}
}
})
}
fromObjectPostgresTestcase := []struct {
name string
generateComment bool
pluralTableName bool
args args
wantMigrationUp string
wantMigrationDown string
wantErr bool
}{
{
name: "from order object",
generateComment: true,
args: args{
[]interface{}{order{}},
"/",
},
wantMigrationUp: expectCreateOrderPostgresUp,
wantMigrationDown: expectCreateOrderPostgresDown,
wantErr: false,
},
}
for _, tt := range fromObjectPostgresTestcase {
t.Run(tt.name, func(t *testing.T) {
s := NewSqlize(WithPostgresql(), WithCommentGenerate())
if err := s.FromObjects(tt.args.objs...); (err != nil) != tt.wantErr {
t.Errorf("FromObjects() postgres error = %v,\n wantErr = %v", err, tt.wantErr)
}
if strUp := s.StringUp(); normSql(strUp) != normSql(tt.wantMigrationUp) {
t.Errorf("StringUp() postgres got = \n%s,\nexpected = \n%s", strUp, tt.wantMigrationUp)
}
if strDown := s.StringDown(); normSql(strDown) != normSql(tt.wantMigrationDown) {
t.Errorf("StringDown() postgres got = \n%s,\nexpected = \n%s", strDown, tt.wantMigrationDown)
}
})
}
fromObjectSqliteTestcase := []struct {
name string
generateComment bool
pluralTableName bool
args args
wantMigrationUp string
wantMigrationDown string
wantErr bool
}{
{
name: "from order sqlite object",
generateComment: true,
args: args{
[]interface{}{order_sqlite{}},
"/",
},
wantMigrationUp: expectCreateOrderSqliteUp,
wantMigrationDown: expectCreateOrderSqliteDown,
wantErr: false,
},
}
for _, tt := range fromObjectSqliteTestcase {
t.Run(tt.name, func(t *testing.T) {
s := NewSqlize(WithSqlite())
if err := s.FromObjects(tt.args.objs...); (err != nil) != tt.wantErr {
t.Errorf("FromObjects() sqlite error = %v,\n wantErr = %v", err, tt.wantErr)
}
if strUp := s.StringUp(); normSql(strUp) != normSql(tt.wantMigrationUp) {
t.Errorf("StringUp() sqlite got = \n%s,\nexpected = \n%s", strUp, tt.wantMigrationUp)
}
if strDown := s.StringDown(); normSql(strDown) != normSql(tt.wantMigrationDown) {
t.Errorf("StringDown() sqlite got = \n%s,\nexpected = \n%s", strDown, tt.wantMigrationDown)
}
})
}
}
func TestSqlize_FromString(t *testing.T) {
type args struct {
sql string
}
fromStringMysqlTestcases := []struct {
name string
args args
wantMigrationUp string
wantMigrationDown string
wantErr bool
}{
{
name: "from person sql",
args: args{
joinSql(createPersonStm, alterPersonUpStm),
},
wantMigrationUp: expectCreatePersonUp,
wantMigrationDown: expectCreatePersonDown,
wantErr: false,
},
{
name: "from hotel sql",
args: args{
joinSql(createHotelStm, alterHotelUpStm),
},
wantMigrationUp: expectCreateHotelUp,
wantMigrationDown: expectCreateHotelDown,
wantErr: false,
},
{
name: "from city sql",
args: args{
joinSql(createCityStm, alterCityUpStm),
},
wantMigrationUp: expectCreateCityUp,
wantMigrationDown: expectCreateCityDown,
wantErr: false,
},
}
for _, tt := range fromStringMysqlTestcases {
t.Run(tt.name, func(t *testing.T) {
s := NewSqlize(WithMysql(), WithSqlUppercase())
if err := s.FromString(tt.args.sql); (err != nil) != tt.wantErr {
t.Errorf("FromString() mysql error = %v,\n wantErr = %v", err, tt.wantErr)
}
if strUp := s.StringUp(); normSql(strUp) != normSql(tt.wantMigrationUp) {
t.Errorf("StringUp() mysql got = \n%s,\nexpected = \n%s", strUp, tt.wantMigrationUp)
}
if strDown := s.StringDown(); normSql(strDown) != normSql(tt.wantMigrationDown) {
t.Errorf("StringDown() mysql got = \n%s,\nexpected = \n%s", strDown, tt.wantMigrationDown)
}
})
}
fromStringPostgresTestcases := []struct {
name string
args args
wantMigrationUp string
wantMigrationDown string
wantErr bool
}{}
for _, tt := range fromStringPostgresTestcases {
t.Run(tt.name, func(t *testing.T) {
s := NewSqlize(WithPostgresql())
if err := s.FromString(tt.args.sql); (err != nil) != tt.wantErr {
t.Errorf("FromString() postgres error = %v,\n wantErr = %v", err, tt.wantErr)
}
if strUp := s.StringUp(); normSql(strUp) != normSql(tt.wantMigrationUp) {
t.Errorf("StringUp() postgres got = \n%s,\nexpected = \n%s", strUp, tt.wantMigrationUp)
}
if strDown := s.StringDown(); normSql(strDown) != normSql(tt.wantMigrationDown) {
t.Errorf("StringDown() postgres got = \n%s,\nexpected = \n%s", strDown, tt.wantMigrationDown)
}
})
}
}
func TestSqlize_Diff(t *testing.T) {
now := time.Now()
type args struct {
newObj interface{}
oldSql string
}
diffMysqlTestcases := []struct {
name string
args args
wantMigrationUp string
wantMigrationDown string
}{
{
name: "diff person sql",
args: args{
person{},
createPersonStm,
},
wantMigrationUp: alterPersonUpStm,
wantMigrationDown: alterPersonDownStm,
},
{
name: "diff hotel sql",
args: args{
hotel{GrandOpening: &now},
createHotelStm,
},
wantMigrationUp: alterHotelUpStm,
wantMigrationDown: alterHotelDownStm,
},
{
name: "diff city sql",
args: args{
city{},
createCityStm,
},
wantMigrationUp: alterCityUpStm,
wantMigrationDown: alterCityDownStm,
},
{
name: "diff movie sql",
args: args{
movie{},
createMovieStm,
},
wantMigrationUp: alterMovieUpStm,
wantMigrationDown: alterMovieDownStm,
},
}
for _, tt := range diffMysqlTestcases {
t.Run(tt.name, func(t *testing.T) {
s := NewSqlize(WithSqlTag("sql"), WithSqlLowercase())
_ = s.FromObjects(tt.args.newObj)
o := NewSqlize()
_ = o.FromString(tt.args.oldSql)
s.Diff(*o)
if strUp := s.StringUp(); normSql(strUp) != normSql(tt.wantMigrationUp) {
t.Errorf("StringUp() mysql got = \n%s,\nexpected = \n%s", strUp, tt.wantMigrationUp)
}
if strDown := s.StringDown(); normSql(strDown) != normSql(tt.wantMigrationDown) {
t.Errorf("StringDown() mysql got = \n%s,\nexpected = \n%s", strDown, tt.wantMigrationDown)
}
})
}
diffPostgresTestcases := []struct {
name string
args args
wantMigrationUp string
wantMigrationDown string
}{}
for _, tt := range diffPostgresTestcases {
t.Run(tt.name, func(t *testing.T) {
s := NewSqlize(WithPostgresql())
_ = s.FromObjects(tt.args.newObj)
o := NewSqlize(WithPostgresql())
_ = o.FromString(tt.args.oldSql)
s.Diff(*o)
if strUp := s.StringUp(); normSql(strUp) != normSql(tt.wantMigrationUp) {
t.Errorf("StringUp() postgres got = \n%s,\nexpected = \n%s", strUp, tt.wantMigrationUp)
}
if strDown := s.StringDown(); normSql(strDown) != normSql(tt.wantMigrationDown) {
t.Errorf("StringDown() postgres got = \n%s,\nexpected = \n%s", strDown, tt.wantMigrationDown)
}
})
}
}
func TestSqlize_MigrationVersion(t *testing.T) {
now := time.Now()
type args struct {
models []interface{}
version int64
isDirty bool
}
migrationVersionMysqlTestcases := []struct {
name string
args args
wantMigrationUp string
wantMigrationDown string
}{
{
name: "person migration version",
args: args{
[]interface{}{person{}},
0,
false,
},
wantMigrationUp: expectCreatePersonUp + "\n" + expectCreateMigrationTableUp,
wantMigrationDown: expectCreatePersonDown + "\n" + expectCreateMigrationTableDown,
},
{
name: "hotel migration version",
args: args{
[]interface{}{hotel{GrandOpening: &now}},
1,
false,
},
wantMigrationUp: expectCreateHotelUp + "\n" + expectMigrationVersion1Up,
wantMigrationDown: expectCreateHotelDown + "\n" + expectMigrationVersion1Down,
},
{
name: "city migration version",
args: args{
[]interface{}{city{}},
1,
false,
},
wantMigrationUp: expectCreateCityUp + "\n" + expectMigrationVersion1Up,
wantMigrationDown: expectCreateCityDown + "\n" + expectMigrationVersion1Down,
},
}
for _, tt := range migrationVersionMysqlTestcases {
t.Run(tt.name, func(t *testing.T) {
opts := []SqlizeOption{
WithMigrationSuffix(".up.test", ".down.test"),
WithMigrationFolder(""),
WithMigrationTable(utils.DefaultMigrationTable),
}
s := NewSqlize(opts...)
s.FromObjects(tt.args.models...)
if got := s.StringUpWithVersion(tt.args.version, tt.args.isDirty); normSql(got) != normSql(tt.wantMigrationUp) {
t.Errorf("StringUpWithVersion() mysql got = \n%s,\nexpected = \n%s", got, tt.wantMigrationUp)
}
if got := s.StringDownWithVersion(tt.args.version); normSql(got) != normSql(tt.wantMigrationDown) {
t.Errorf("StringDownWithVersion() mysql got = \n%s,\nexpected = \n%s", got, tt.wantMigrationDown)
}
if err := s.WriteFilesWithVersion(tt.name, tt.args.version, tt.args.isDirty); err != nil {
t.Errorf("WriteFilesWithVersion() mysql error = \n%v,\nwantErr = \n%v", err, nil)
}
if err := s.WriteFilesVersion(tt.name, tt.args.version, tt.args.isDirty); err != nil {
t.Errorf("WriteFilesVersion() mysql error = \n%v,\nwantErr = \n%v", err, nil)
}
})
}
migrationVersionPostgresTestcases := []struct {
name string
args args
wantMigrationUp string
wantMigrationDown string
}{}
for _, tt := range migrationVersionPostgresTestcases {
t.Run(tt.name, func(t *testing.T) {
opts := []SqlizeOption{
WithMigrationSuffix(".up.test", ".down.test"),
WithMigrationFolder(""),
WithMigrationTable(utils.DefaultMigrationTable),
WithPostgresql(),
WithIgnoreFieldOrder(),
}
s := NewSqlize(opts...)
s.FromObjects(tt.args.models...)
if got := s.StringUpWithVersion(tt.args.version, tt.args.isDirty); normSql(got) != normSql(tt.wantMigrationUp) {
t.Errorf("StringUpWithVersion() postgres got = \n%s,\nexpected = \n%s", got, tt.wantMigrationUp)
}
if got := s.StringDownWithVersion(tt.args.version); normSql(got) != normSql(tt.wantMigrationDown) {
t.Errorf("StringDownWithVersion() postgres got = \n%s,\nexpected = \n%s", got, tt.wantMigrationDown)
}
if err := s.WriteFilesWithVersion(tt.name, tt.args.version, tt.args.isDirty); err != nil {
t.Errorf("WriteFilesWithVersion() postgres error = \n%v,\nwantErr = \n%v", err, nil)
}
if err := s.WriteFilesVersion(tt.name, tt.args.version, tt.args.isDirty); err != nil {
t.Errorf("WriteFilesVersion() postgres error = \n%v,\nwantErr = \n%v", err, nil)
}
})
}
}
func TestSqlize_HashValue(t *testing.T) {
now := time.Now()
type args struct {
models []interface{}
}
hashValueMysqlTestcases := []struct {
name string
args args
want int64
}{
{
name: "person hash value",
args: args{
[]interface{}{person{}},
},
want: -5168892191412708041,
},
{
name: "hotel hash value",
args: args{
[]interface{}{hotel{GrandOpening: &now}},
},
want: -3590096811374758567,
},
{
name: "city hash value",
args: args{
[]interface{}{city{}},
},
want: -2026584327433441245,
}, {
name: "movie hash value",
args: args{
[]interface{}{movie{}},
},
want: -5515853333036032887,
},
}
for _, tt := range hashValueMysqlTestcases {
t.Run(tt.name, func(t *testing.T) {
opts := []SqlizeOption{}
s := NewSqlize(opts...)
s.FromObjects(tt.args.models...)
if got := s.HashValue(); got != tt.want {
t.Errorf("HashValue() mysql got = \n%d,\nexpected = \n%d", got, tt.want)
}
})
}
hashValuePostgresTestcases := []struct {
name string
args args
want int64
}{}
for _, tt := range hashValuePostgresTestcases {
t.Run(tt.name, func(t *testing.T) {
opts := []SqlizeOption{WithPostgresql()}
s := NewSqlize(opts...)
s.FromObjects(tt.args.models...)
if got := s.HashValue(); got != tt.want {
t.Errorf("HashValue() postgres got = \n%d,\nexpected = \n%d", got, tt.want)
}
})
}
}
func TestSqlize_Mermaidjs(t *testing.T) {
now := time.Now()
type args struct {
models []interface{}
needTables []string
}
MermaidJsTestcases := []struct {
name string
args args
wantErd string
wantLive string
}{
{
name: "person mermaidjs",
args: args{
[]interface{}{person{}},
[]string{"person"},
},
wantErd: expectPersonMermaidJsErd,
wantLive: expectPersonMermaidJsLive,
},
{
name: "hotel mermaidjs",
args: args{
[]interface{}{hotel{GrandOpening: &now}},
[]string{"hotel"},
},
wantErd: expectHotelMermaidJsErd,
wantLive: expectHotelMermaidJsLive,
},
{
name: "person city mermaidjs",
args: args{
[]interface{}{person{}, city{}},
[]string{"person", "city"},
},
wantErd: expectPersonCityMermaidJsErd,
wantLive: expectPersonCityMermaidJsLive,
},
{
name: "order user mermaidjs",
args: args{
[]interface{}{order{}, user{}},
[]string{"orders", "user"},
},
wantErd: expectOrderUserMermaidJsErd,
wantLive: expectOrderUserMermaidJsLive,
},
}
for _, tt := range MermaidJsTestcases {
t.Run(tt.name, func(t *testing.T) {
opts := []SqlizeOption{}
s := NewSqlize(opts...)
s.FromObjects(tt.args.models...)
if got := s.MermaidJsErd(tt.args.needTables...); normStr(got) != normStr(tt.wantErd) {
t.Errorf("MermaidJsErd() got = \n%v,\nexpected = \n%v", got, tt.wantErd)
}
if got := s.MermaidJsLive(tt.args.needTables...); got != tt.wantLive {
t.Errorf("MermaidJsLive() got = \n%v,\nexpected = \n%v", got, tt.wantLive)
}
})
}
}
func TestSqlize_ArvoSchema(t *testing.T) {
now := time.Now()
type args struct {
models []interface{}
needTables []string
}
arvoSchemaMysqlTestcases := []struct {
name string
args args
want []string
}{
{
name: "person arvo",
args: args{
[]interface{}{person{}},
[]string{"person"},
},
want: []string{expectPersonArvo},
},
{
name: "hotel arvo",
args: args{
[]interface{}{hotel{GrandOpening: &now}},
[]string{"hotel"},
},
want: []string{expectHotelArvo},
},
{
name: "city arvo",
args: args{
[]interface{}{city{}},
[]string{"city"},
},
want: []string{expectCityArvo},
},
}
for _, tt := range arvoSchemaMysqlTestcases {
t.Run(tt.name, func(t *testing.T) {
opts := []SqlizeOption{}
s := NewSqlize(opts...)
s.FromObjects(tt.args.models...)