This repository has been archived by the owner on Nov 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_test.go
1127 lines (970 loc) · 45.4 KB
/
query_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 rushia
import (
"reflect"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
// assertEqual 會將期望的 SQL 指令與實際的 SQL 指令拆分,因為 Reiner 裡有 Map 會導致產生出來的結果每次都不如預期地按照順序排。
// 拆分後便會比對是否有相同的「字詞」,若短缺則是執行結果不符合預期即報錯。
func assertEqual(a *assert.Assertions, expected string, actual string) {
originalExpected := expected
originalActual := actual
expected = strings.Replace(expected, "(", "", -1)
expected = strings.Replace(expected, ")", "", -1)
expected = strings.Replace(expected, ",", "", -1)
expectedParts := strings.Split(expected, " ")
actual = strings.Replace(actual, "(", "", -1)
actual = strings.Replace(actual, ")", "", -1)
actual = strings.Replace(actual, ",", "", -1)
actualParts := strings.Split(actual, " ")
passed := []bool{}
for _, v := range expectedParts {
for _, vv := range actualParts {
if v == vv {
passed = append(passed, true)
break
}
}
}
expectedLen := len(strings.Split(originalExpected, " "))
actualLen := len(strings.Split(originalActual, " "))
if len(passed) != len(actualParts) {
a.Fail(`Not equal:`, "expected: \"%s\"\nreceived: \"%s\"", originalExpected, originalActual)
}
if expectedLen != actualLen {
a.Fail(`Not same length:`, "expected: \"%s\"\nreceived: \"%s\"", originalExpected, originalActual)
}
}
func assertParams(a *assert.Assertions, expected []interface{}, actual []interface{}) {
if len(expected) != len(actual) {
a.Fail("Not same params length", "expected: \"%d\" %+v\nreceived: \"%d\" %+v", len(expected), expected, len(actual), actual)
}
for _, v := range expected {
var yes bool
for _, j := range actual {
if v == j {
yes = true
}
}
if !yes {
a.Fail(`Not in params`)
}
}
}
func assertParamOrders(a *assert.Assertions, expected []interface{}, actual []interface{}) {
if len(expected) != len(actual) {
a.Fail("Not same params length", "expected: \"%d\" %+v\nreceived: \"%d\" %+v", len(expected), expected, len(actual), actual)
}
for k, v := range expected {
if actual[k] != v {
a.Fail(`Not same params order`)
}
}
}
func BenchmarkInsertStruct(b *testing.B) {
u := struct {
Username string
Password string
}{
Username: "YamiOdymel",
Password: "test",
}
for i := 0; i < b.N; i++ {
NewQuery("Users").Insert(u)
}
}
func BenchmarkInsert(b *testing.B) {
for i := 0; i < b.N; i++ {
NewQuery("Users").Insert(H{
"Username": "YamiOdymel",
"Password": "test",
})
}
}
//=======================================================
// Insert
//=======================================================
func TestInsertStruct(t *testing.T) {
u := struct {
Username string
Password string
}{
Username: "YamiOdymel",
Password: "test",
}
assert := assert.New(t)
query, params := Build(NewQuery("Users").Insert(u))
assertEqual(assert, "INSERT INTO `Users` (`username`, `password`) VALUES (?, ?)", query)
assertParams(assert, []interface{}{"YamiOdymel", "test"}, params)
}
func TestInsertStructPointer(t *testing.T) {
type user struct {
Username string
Password string
}
u := &user{
Username: "YamiOdymel",
Password: "test",
}
assert := assert.New(t)
query, params := Build(NewQuery("Users").Insert(u))
assertEqual(assert, "INSERT INTO `Users` (`username`, `password`) VALUES (?, ?)", query)
assertParams(assert, []interface{}{"YamiOdymel", "test"}, params)
}
func TestInsert(t *testing.T) {
assert := assert.New(t)
query, params := Build(NewQuery("Users").Insert(H{
"Username": "YamiOdymel",
"Password": "test",
}))
assertEqual(assert, "INSERT INTO `Users` (`Username`, `Password`) VALUES (?, ?)", query)
assertParams(assert, []interface{}{"YamiOdymel", "test"}, params)
}
func TestInsertMap(t *testing.T) {
assert := assert.New(t)
query, params := Build(NewQuery("Users").Insert(map[string]interface{}{
"Username": "YamiOdymel",
"Password": "test",
}))
assertEqual(assert, "INSERT INTO `Users` (`Username`, `Password`) VALUES (?, ?)", query)
assertParams(assert, []interface{}{"YamiOdymel", "test"}, params)
}
func TestInsertMulti(t *testing.T) {
assert := assert.New(t)
data := []H{
{
"Username": "YamiOdymel",
"Password": "test",
}, {
"Username": "Karisu",
"Password": "12345",
},
}
query, params := Build(NewQuery("Users").Insert(data))
assertEqual(assert, "INSERT INTO `Users` (`Username`, `Password`) VALUES (?, ?), (?, ?)", query)
assertParams(assert, []interface{}{"YamiOdymel", "test", "Karisu", "12345"}, params)
}
func TestInsertMultiMaps(t *testing.T) {
assert := assert.New(t)
data := []map[string]interface{}{
{
"Username": "YamiOdymel",
"Password": "test",
}, {
"Username": "Karisu",
"Password": "12345",
},
}
query, params := Build(NewQuery("Users").Insert(data))
assertEqual(assert, "INSERT INTO `Users` (`Username`, `Password`) VALUES (?, ?), (?, ?)", query)
assertParams(assert, []interface{}{"YamiOdymel", "test", "Karisu", "12345"}, params)
}
func TestInsertStructOmit(t *testing.T) {
u := struct {
Username string
Password string
}{
Username: "YamiOdymel",
Password: "test",
}
assert := assert.New(t)
query, params := Build(NewQuery("Users").Omit("username").Insert(u))
assertEqual(assert, "INSERT INTO `Users` (`password`) VALUES (?)", query)
assertParams(assert, []interface{}{"test"}, params)
}
func TestInsertStructOmitByTag(t *testing.T) {
u := struct {
Username string `rushia:"-"`
Password string
}{
Username: "YamiOdymel",
Password: "test",
}
assert := assert.New(t)
query, params := Build(NewQuery("Users").Insert(u))
assertEqual(assert, "INSERT INTO `Users` (`password`) VALUES (?)", query)
assertParams(assert, []interface{}{"test"}, params)
}
func TestInsertStructTagOmit(t *testing.T) {
assert := assert.New(t)
u := struct {
Username string `rushia:"user_name"`
Password string
}{
Username: "YamiOdymel",
Password: "test",
}
query, params := Build(NewQuery("Users").Omit("user_name").Insert(u))
assertEqual(assert, "INSERT INTO `Users` (`password`) VALUES (?)", query)
assertParams(assert, []interface{}{"test"}, params)
}
func TestInsertStructTagRename(t *testing.T) {
u := struct {
Username string `rushia:"user_name"`
Password string
Nickname string `rushia:"Nickname"`
}{
Username: "YamiOdymel",
Password: "test",
Nickname: "Xiaoan",
}
assert := assert.New(t)
query, params := Build(NewQuery("Users").Insert(u))
assertEqual(assert, "INSERT INTO `Users` (`user_name`, `password`, `Nickname`) VALUES (?, ?, ?)", query)
assertParams(assert, []interface{}{"YamiOdymel", "test", "Xiaoan"}, params)
}
func TestInsertOmit(t *testing.T) {
assert := assert.New(t)
query, params := Build(NewQuery("Users").Omit("Username").Insert(H{
"Username": "YamiOdymel",
"Password": "test",
}))
assertEqual(assert, "INSERT INTO `Users` (`Password`) VALUES (?)", query)
assertParams(assert, []interface{}{"test"}, params)
}
func TestInsertMultiOmit(t *testing.T) {
assert := assert.New(t)
data := []H{
{
"Username": "YamiOdymel",
"Password": "test",
}, {
"Username": "Karisu",
"Password": "12345",
},
}
query, params := Build(NewQuery("Users").Omit("Username").Insert(data))
assertEqual(assert, "INSERT INTO `Users` (`Password`) VALUES (?), (?)", query)
assertParams(assert, []interface{}{"test", "12345"}, params)
}
func TestInsertExpr(t *testing.T) {
assert := assert.New(t)
query, params := Build(NewQuery("Users").Insert(H{
"Username": "YamiOdymel",
"Password": NewExpr("SHA1(?)", "secretpassword+salt"),
"Expires": NewExpr("NOW() + INTERVAL 1 YEAR"),
"CreatedAt": NewExpr("NOW()"),
}))
assertEqual(assert, "INSERT INTO `Users` (`CreatedAt`, `Expires`, `Password`, `Username`) VALUES (NOW(), NOW() + INTERVAL 1 YEAR, SHA1(?), ?)", query)
assertParams(assert, []interface{}{"YamiOdymel", "secretpassword+salt"}, params)
}
func TestInsertSubQueryExpr(t *testing.T) {
assert := assert.New(t)
subQuery := NewQuery("Salaries").Where("Username = ?", "YamiOdymel").Select("Salary")
query, params := Build(NewQuery("Users").Insert(H{
"Username": "YamiOdymel",
"AvgSalary": NewExpr("SUM((?))", subQuery),
}))
assertEqual(assert, "INSERT INTO `Users` (`Username`, `AvgSalary`) VALUES (?, SUM((SELECT `Salary` FROM `Salaries` WHERE Username = ?)))", query)
assertParams(assert, []interface{}{"YamiOdymel", "YamiOdymel"}, params)
subQuery = NewQuery("Salaries").Where("Username = ?", "YamiOdymel").Select("Salary")
query, params = Build(NewQuery("Users").Insert(H{
"Username": "YamiOdymel",
"Salary": subQuery,
}))
assertEqual(assert, "INSERT INTO `Users` (`Username`, `Salary`) VALUES (?, (SELECT `Salary` FROM `Salaries` WHERE Username = ?))", query)
assertParams(assert, []interface{}{"YamiOdymel", "YamiOdymel"}, params)
}
func TestInsertSelect(t *testing.T) {
assert := assert.New(t)
from := NewQuery("AdditionalUsers").Where("Username LIKE ?", "ABC%").Select("ID", "Username", "Nickname")
query, params := Build(NewQuery("Users").InsertSelect(from, "ID", "Username", "Nickname"))
assertEqual(assert, "INSERT INTO `Users` (`ID`, `Username`, `Nickname`) SELECT `ID`, `Username`, `Nickname` FROM `AdditionalUsers` WHERE Username LIKE ?", query)
assertParams(assert, []interface{}{"ABC%"}, params)
}
func TestOnDuplicateInsert(t *testing.T) {
assert := assert.New(t)
query, params := Build(NewQuery("Users").OnDuplicate(H{
"UpdatedAt": NewExpr("VALUES(UpdatedAt)"), // Deprecated in MySQL 8.0.20
"ID": NewExpr("LAST_INSERT_ID(ID)"),
}).Insert(H{
"Username": "YamiOdymel",
"Password": "test",
"UpdatedAt": NewExpr("NOW()"),
}))
assertEqual(assert, "INSERT INTO `Users` (`Password`, `UpdatedAt`, `Username`) VALUES (?, NOW(), ?) ON DUPLICATE KEY UPDATE `ID` = LAST_INSERT_ID(ID), `UpdatedAt` = VALUES(UpdatedAt)", query)
assertParams(assert, []interface{}{"YamiOdymel", "test"}, params)
query, params = Build(NewQuery("Users").As("New").OnDuplicate(H{
"UpdatedAt": NewExpr("New.UpdatedAt"),
"ID": NewExpr("LAST_INSERT_ID(ID)"),
}).Insert(H{
"Username": "YamiOdymel",
"Password": "test",
"UpdatedAt": NewExpr("NOW()"),
}))
assertEqual(assert, "INSERT INTO `Users` (`Password`, `UpdatedAt`, `Username`) VALUES (?, NOW(), ?) AS New ON DUPLICATE KEY UPDATE `ID` = LAST_INSERT_ID(ID), `UpdatedAt` = New.UpdatedAt", query)
assertParams(assert, []interface{}{"YamiOdymel", "test"}, params)
}
//=======================================================
// Replace
//=======================================================
func TestReplace(t *testing.T) {
assert := assert.New(t)
query, params := Build(NewQuery("Users").Replace(H{
"Username": "YamiOdymel",
"Password": "test",
}))
assertEqual(assert, "REPLACE INTO `Users` (`Password`, `Username`) VALUES (?, ?)", query)
assertParams(assert, []interface{}{"YamiOdymel", "test"}, params)
}
//=======================================================
// Update
//=======================================================
func TestUpdateOmit(t *testing.T) {
assert := assert.New(t)
query, params := Build(NewQuery("Users").Where("Username = ?", "YamiOdymel").Omit("Username").Update(H{
"Username": "Karisu",
"Password": "123456",
}))
assertEqual(assert, "UPDATE `Users` SET `Password` = ? WHERE Username = ?", query)
assertParams(assert, []interface{}{"YamiOdymel", "123456"}, params)
}
func TestUpdateOmitStruct(t *testing.T) {
u := struct {
Username string
Password string
}{
Username: "YamiOdymel",
Password: "test",
}
assert := assert.New(t)
query, params := Build(NewQuery("Users").Where("Username = ?", "YamiOdymel").Omit("username").Update(u))
assertEqual(assert, "UPDATE `Users` SET `password` = ? WHERE Username = ?", query)
assertParams(assert, []interface{}{"test", "YamiOdymel"}, params)
}
func TestUpdate(t *testing.T) {
assert := assert.New(t)
query, params := Build(NewQuery("Users").Where("Username = ?", "YamiOdymel").Update(H{
"Username": "",
"Password": "123456",
}))
assertEqual(assert, "UPDATE `Users` SET `Password` = ?, `Username` = ? WHERE Username = ?", query)
assertParams(assert, []interface{}{"YamiOdymel", "", "123456"}, params)
}
func TestUpdateCase(t *testing.T) {
assert := assert.New(t)
query, params := Build(NewQuery("Users").Where("Username = ?", "YamiOdymel").Update(H{
"Username": "",
"Password": NewExpr(`CASE Username WHEN "YamiOdymel" THEN 123 WHEN "Foobar" THEN 456 ELSE 789 END`),
}))
assertEqual(assert, "UPDATE `Users` SET `Password` = CASE Username WHEN \"YamiOdymel\" THEN 123 WHEN \"Foobar\" THEN 456 ELSE 789 END, `Username` = ? WHERE Username = ?", query)
assertParams(assert, []interface{}{"YamiOdymel", ""}, params)
}
func TestUpdateStruct(t *testing.T) {
u := struct {
Username string
Password string
}{
Username: "YamiOdymel",
Password: "test",
}
assert := assert.New(t)
query, params := Build(NewQuery("Users").Where("Username = ?", "YamiOdymel").Update(u))
assertEqual(assert, "UPDATE `Users` SET `password` = ?, `username` = ? WHERE Username = ?", query)
assertParams(assert, []interface{}{"YamiOdymel", "YamiOdymel", "test"}, params)
}
func TestLimitUpdate(t *testing.T) {
assert := assert.New(t)
query, params := Build(NewQuery("Users").Limit(10).Update(H{
"Username": "Karisu",
"Password": "123456",
}))
assertEqual(assert, "UPDATE `Users` SET `Password` = ?, `Username` = ? LIMIT 10", query)
assertParams(assert, []interface{}{"Karisu", "123456"}, params)
}
//=======================================================
// Patch
//=======================================================
func TestPatch(t *testing.T) {
assert := assert.New(t)
query, params := Build(NewQuery("Users").Where("Username = ?", "YamiOdymel").Patch(H{
"Username": "",
"Age": 0,
"Height": 183,
"Password": "123456",
}))
assertEqual(assert, "UPDATE `Users` SET `Password` = ?, `Height` = ? WHERE Username = ?", query)
assertParams(assert, []interface{}{"YamiOdymel", "123456", 183}, params)
}
func TestPatchExcludeTypes(t *testing.T) {
assert := assert.New(t)
query, params := Build(NewQuery("Users").Where("Username = ?", "YamiOdymel").Exclude(reflect.String).Patch(H{
"Username": "",
"Age": 0,
"Height": 183,
"Password": "123456",
}))
assertEqual(assert, "UPDATE `Users` SET `Password` = ?, `Username` = ?, `Height` = ? WHERE Username = ?", query)
assertParams(assert, []interface{}{"YamiOdymel", "", 183, "YamiOdymel"}, params)
}
func TestPatchExcludeColumns(t *testing.T) {
assert := assert.New(t)
query, params := Build(NewQuery("Users").Where("Username = ?", "YamiOdymel").Exclude("Username", "Age").Patch(H{
"Username": "",
"Age": 0,
"Height": 0,
"Password": "123456",
}))
assertEqual(assert, "UPDATE `Users` SET `Password` = ?, `Username` = ?, `Age` = ? WHERE Username = ?", query)
assertParams(assert, []interface{}{"YamiOdymel", "123456", 0, ""}, params)
}
func TestPatchStruct(t *testing.T) {
u := struct {
Username string
Password string
}{
Username: "",
Password: "test",
}
assert := assert.New(t)
query, params := Build(NewQuery("Users").Where("Username = ?", "YamiOdymel").Patch(u))
assertEqual(assert, "UPDATE `Users` SET `password` = ? WHERE Username = ?", query)
assertParams(assert, []interface{}{"YamiOdymel", "test"}, params)
}
//=======================================================
// Exists
//=======================================================
func TestExists(t *testing.T) {
assert := assert.New(t)
query, params := Build(NewQuery("Users").Where("Username = ?", "YamiOdymel").Exists())
assertEqual(assert, "SELECT EXISTS(SELECT * FROM `Users` WHERE Username = ?)", query)
assertParams(assert, []interface{}{"YamiOdymel"}, params)
}
//=======================================================
// Select
//=======================================================
func TestSelect(t *testing.T) {
assert := assert.New(t)
query, _ := Build(NewQuery("Users").Select())
assertEqual(assert, "SELECT * FROM `Users`", query)
}
func TestOffsetSelect(t *testing.T) {
assert := assert.New(t)
query, _ := Build(NewQuery("Users").Offset(10, 20).Select())
assertEqual(assert, "SELECT * FROM `Users` LIMIT 10 OFFSET 20", query)
}
func TestLimitSelect(t *testing.T) {
assert := assert.New(t)
query, _ := Build(NewQuery("Users").Limit(10).Select())
assertEqual(assert, "SELECT * FROM `Users` LIMIT 10", query)
}
func TestClearLimit(t *testing.T) {
assert := assert.New(t)
query, _ := Build(NewQuery("Users").Limit(10).Select().ClearLimit())
assertEqual(assert, "SELECT * FROM `Users`", query)
query, _ = Build(NewQuery("Users").Offset(10, 20).Select().ClearLimit())
assertEqual(assert, "SELECT * FROM `Users`", query)
query, _ = Build(NewQuery("Users").Limit(10, 100).Select().ClearLimit())
assertEqual(assert, "SELECT * FROM `Users`", query)
}
func TestLimitMultiSelect(t *testing.T) {
assert := assert.New(t)
query, _ := Build(NewQuery("Users").Limit(10, 20).Select())
assertEqual(assert, "SELECT * FROM `Users` LIMIT 10, 20", query)
}
func TestPaginateSelect(t *testing.T) {
assert := assert.New(t)
query, _ := Build(NewQuery("Users").Paginate(1, 100).Select())
assertEqual(assert, "SELECT * FROM `Users` LIMIT 0, 100", query)
query, _ = Build(NewQuery("Users").Paginate(2, 100).Select())
assertEqual(assert, "SELECT * FROM `Users` LIMIT 100, 100", query)
query, _ = Build(NewQuery("Users").Paginate(3, 100).Select())
assertEqual(assert, "SELECT * FROM `Users` LIMIT 200, 100", query)
}
func TestGetColumns(t *testing.T) {
assert := assert.New(t)
query, _ := Build(NewQuery("Users").Select("Username", "Nickname"))
assertEqual(assert, "SELECT `Username`, `Nickname` FROM `Users`", query)
query, _ = Build(NewQuery("Users").Select("COUNT(*) AS Count"))
assertEqual(assert, "SELECT COUNT(*) AS Count FROM `Users`", query)
query, _ = Build(NewQuery("Users").Select("SUM(ID)", "COUNT(*) AS Count"))
assertEqual(assert, "SELECT SUM(ID), COUNT(*) AS Count FROM `Users`", query)
}
func TestSelectOne(t *testing.T) {
assert := assert.New(t)
query, params := Build(NewQuery("Users").Where("ID = ?", 1).Select())
assertEqual(assert, "SELECT * FROM `Users` WHERE ID = ?", query)
assertParams(assert, []interface{}{1}, params)
query, _ = Build(NewQuery("Users").Limit(1).Select())
assertEqual(assert, "SELECT * FROM `Users` LIMIT 1", query)
query, _ = Build(NewQuery("Users").SelectOne())
assertEqual(assert, "SELECT * FROM `Users` LIMIT 1", query)
query, _ = Build(NewQuery("Users").SelectOne("Username", "Nickname"))
assertEqual(assert, "SELECT `Username`, `Nickname` FROM `Users` LIMIT 1", query)
}
//=======================================================
// Raw Query
//=======================================================
func TestRawQuery(t *testing.T) {
assert := assert.New(t)
query, params := Build(NewRawQuery("SELECT * FROM Users WHERE ID >= ?", 10))
assertEqual(assert, "SELECT * FROM Users WHERE ID >= ?", query)
assertParams(assert, []interface{}{10}, params)
}
//=======================================================
// Where
//=======================================================
func TestWhere(t *testing.T) {
assert := assert.New(t)
query, params := Build(NewQuery("Users").Where("ID = ?", 1).Where("Username = ?", "admin").Select())
assertEqual(assert, "SELECT * FROM `Users` WHERE ID = ? AND Username = ?", query)
assertParams(assert, []interface{}{1, "admin"}, params)
query, params = Build(NewQuery("Users").Where("ID = ?", 1).OrWhere("Username = ?", "admin").Select())
assertEqual(assert, "SELECT * FROM `Users` WHERE ID = ? OR Username = ?", query)
assertParams(assert, []interface{}{1, "admin"}, params)
}
func TestWhereEscape(t *testing.T) {
assert := assert.New(t)
query, params := Build(NewQuery("Users").Where("?? = ?", "ID", 1).Where("?? = ?", "Username", "admin").Select())
assertEqual(assert, "SELECT * FROM `Users` WHERE `ID` = ? AND `Username` = ?", query)
assertParams(assert, []interface{}{1, "admin"}, params)
query, params = Build(NewQuery("Users").Where("?? = ?", "ID", 1).OrWhere("?? = ?", "Username", "admin").Select())
assertEqual(assert, "SELECT * FROM `Users` WHERE `ID` = ? OR `Username` = ?", query)
assertParams(assert, []interface{}{1, "admin"}, params)
}
func TestWhereQuery(t *testing.T) {
assert := assert.New(t)
query, params := Build(NewQuery("Users").Where("(ID = ?)", 1).Where("Username = ?", "admin").Select())
assertEqual(assert, "SELECT * FROM `Users` WHERE (ID = ?) AND Username = ?", query)
assertParams(assert, []interface{}{1, "admin"}, params)
query, params = Build(NewQuery("Users").Where("(ID = ? OR Password = SHA(?))", 1, "password").Where("Username = ?", "admin").Select())
assertEqual(assert, "SELECT * FROM `Users` WHERE (ID = ? OR Password = SHA(?)) AND Username = ?", query)
assertParams(assert, []interface{}{1, "password", "admin"}, params)
query, params = Build(NewQuery("Users").Where("(ID = ? OR Password = SHA(?))", 1, "password").OrWhere("Username = ?", "admin").Select())
assertEqual(assert, "SELECT * FROM `Users` WHERE (ID = ? OR Password = SHA(?)) OR Username = ?", query)
assertParams(assert, []interface{}{1, "password", "admin"}, params)
query, params = Build(NewQuery("Users").Where("(ID = ? OR Password = SHA(?) OR Username = ?)", 1, "password", "Hello").Select())
assertEqual(assert, "SELECT * FROM `Users` WHERE (ID = ? OR Password = SHA(?) OR Username = ?)", query)
assertParams(assert, []interface{}{1, "password", "Hello"}, params)
}
func TestWhereExpr(t *testing.T) {
assert := assert.New(t)
query, params := Build(NewQuery("Users").Where("(ID = ?)", 1).Where("Username = ?", "admin").Select())
assertEqual(assert, "SELECT * FROM `Users` WHERE (ID = ?) AND Username = ?", query)
assertParams(assert, []interface{}{1, "admin"}, params)
query, params = Build(NewQuery("Users").Where("(ID = ? OR Password = SHA(?))", 1, "password").Where("Username = ?", "admin").Select())
assertEqual(assert, "SELECT * FROM `Users` WHERE (ID = ? OR Password = SHA(?)) AND Username = ?", query)
assertParams(assert, []interface{}{1, "password", "admin"}, params)
query, params = Build(NewQuery("Users").Where("(ID = ? OR Password = SHA(?))", 1, "password").Where("Username = ?", "admin").Select())
assertEqual(assert, "SELECT * FROM `Users` WHERE (ID = ? OR Password = SHA(?)) AND Username = ?", query)
assertParams(assert, []interface{}{1, "password", "admin"}, params)
}
func TestWhereHaving(t *testing.T) {
assert := assert.New(t)
query, params := Build(NewQuery("Users").Where("ID = ?", 1).Having("Username = ?", "admin").Select())
assertEqual(assert, "SELECT * FROM `Users` WHERE ID = ? HAVING Username = ?", query)
assertParams(assert, []interface{}{1, "admin"}, params)
query, params = Build(NewQuery("Users").Where("ID = ?", 1).Having("Username = ?", "admin").OrHaving("Password = ?", "test").Select())
assertEqual(assert, "SELECT * FROM `Users` WHERE ID = ? HAVING Username = ? OR Password = ?", query)
assertParams(assert, []interface{}{1, "admin", "test"}, params)
}
func TestWhereColumns(t *testing.T) {
assert := assert.New(t)
query, _ := Build(NewQuery("Users").Where("LastLogin = CreatedAt").Select())
assertEqual(assert, "SELECT * FROM `Users` WHERE LastLogin = CreatedAt", query)
query, _ = Build(NewQuery("Users").Where("LastLogin = CreatedAt").Select())
assertEqual(assert, "SELECT * FROM `Users` WHERE LastLogin = CreatedAt", query)
}
func TestWhereOperator(t *testing.T) {
assert := assert.New(t)
query, params := Build(NewQuery("Users").Where("ID >= ?", 50).Select())
assertEqual(assert, "SELECT * FROM `Users` WHERE ID >= ?", query)
assertParams(assert, []interface{}{50}, params)
}
func TestWhereLike(t *testing.T) {
assert := assert.New(t)
query, params := Build(NewQuery("Users").Where("ID LIKE ?", 50).Select())
assertEqual(assert, "SELECT * FROM `Users` WHERE ID LIKE ?", query)
assertParams(assert, []interface{}{50}, params)
query, params = Build(NewQuery("Users").Where("ID NOT LIKE ?", 50).Select())
assertEqual(assert, "SELECT * FROM `Users` WHERE ID NOT LIKE ?", query)
assertParams(assert, []interface{}{50}, params)
}
func TestWhereBetween(t *testing.T) {
assert := assert.New(t)
query, params := Build(NewQuery("Users").Where("ID BETWEEN ? AND ?", 0, 20).Select())
assertEqual(assert, "SELECT * FROM `Users` WHERE ID BETWEEN ? AND ?", query)
assertParams(assert, []interface{}{0, 20}, params)
now := time.Now()
nowAdd := time.Now().Add(time.Second * 60)
query, params = Build(NewQuery("Users").Where("ID BETWEEN ? AND ?", now, nowAdd).Select())
assertEqual(assert, "SELECT * FROM `Users` WHERE ID BETWEEN ? AND ?", query)
assertParams(assert, []interface{}{now, nowAdd}, params)
query, params = Build(NewQuery("Users").Where("ID NOT BETWEEN ? AND ?", 0, 20).Select())
assertEqual(assert, "SELECT * FROM `Users` WHERE ID NOT BETWEEN ? AND ?", query)
assertParams(assert, []interface{}{0, 20}, params)
query, params = Build(NewQuery("Users").Where("ID NOT BETWEEN ? AND ?", now, nowAdd).Select())
assertEqual(assert, "SELECT * FROM `Users` WHERE ID NOT BETWEEN ? AND ?", query)
assertParams(assert, []interface{}{now, nowAdd}, params)
}
func TestWhereIn(t *testing.T) {
assert := assert.New(t)
query, params := Build(NewQuery("Users").Where("ID IN (?, ?, ?, ?, ?)", 1, 5, 27, -1, "d").Select())
assertEqual(assert, "SELECT * FROM `Users` WHERE ID IN (?, ?, ?, ?, ?)", query)
assertParams(assert, []interface{}{1, 5, 27, -1, "d"}, params)
query, params = Build(NewQuery("Users").Where("ID IN ?", []interface{}{1, 5, 27, -1, "d"}).Select())
assertEqual(assert, "SELECT * FROM `Users` WHERE ID IN (?, ?, ?, ?, ?)", query)
assertParams(assert, []interface{}{1, 5, 27, -1, "d"}, params)
query, params = Build(NewQuery("Users").Where("ID IN ?", []int{1, 5, 27, -1}).Select())
assertEqual(assert, "SELECT * FROM `Users` WHERE ID IN (?, ?, ?, ?)", query)
assertParams(assert, []interface{}{1, 5, 27, -1}, params)
query, params = Build(NewQuery("Users").Where("ID IN (?)", 1).Select())
assertEqual(assert, "SELECT * FROM `Users` WHERE ID IN (?)", query)
assertParams(assert, []interface{}{1}, params)
query, params = Build(NewQuery("Users").Where("ID NOT IN (?, ?, ?, ?, ?)", 1, 5, 27, -1, "d").Select())
assertEqual(assert, "SELECT * FROM `Users` WHERE ID NOT IN (?, ?, ?, ?, ?)", query)
assertParams(assert, []interface{}{1, 5, 27, -1, "d"}, params)
query, params = Build(NewQuery("Users").Where("ID NOT IN ?", []interface{}{1, 5, 27, -1, "d"}).Select())
assertEqual(assert, "SELECT * FROM `Users` WHERE ID NOT IN (?, ?, ?, ?, ?)", query)
assertParams(assert, []interface{}{1, 5, 27, -1, "d"}, params)
}
func TestOrWhere(t *testing.T) {
assert := assert.New(t)
query, params := Build(NewQuery("Users").Where("FirstName = ?", "John").OrWhere("FirstName = ?", "Peter").Select())
assertEqual(assert, "SELECT * FROM `Users` WHERE FirstName = ? OR FirstName = ?", query)
assertParams(assert, []interface{}{"John", "Peter"}, params)
query, _ = Build(NewQuery("Users").Where("A = B").OrWhere("(A = C OR A = D)").Select())
assertEqual(assert, "SELECT * FROM `Users` WHERE A = B OR (A = C OR A = D)", query)
}
func TestWhereNull(t *testing.T) {
assert := assert.New(t)
query, _ := Build(NewQuery("Users").Where("LastName IS NULL").Select())
assertEqual(assert, "SELECT * FROM `Users` WHERE LastName IS NULL", query)
query, _ = Build(NewQuery("Users").Where("LastName IS NOT NULL").Select())
assertEqual(assert, "SELECT * FROM `Users` WHERE LastName IS NOT NULL", query)
}
func TestWhereExists(t *testing.T) {
assert := assert.New(t)
subQuery := NewQuery("Products").Select()
query, _ := Build(NewQuery("Users").Where("EXISTS ?", subQuery).Select())
assertEqual(assert, "SELECT * FROM `Users` WHERE EXISTS (SELECT * FROM `Products`)", query)
query, _ = Build(NewQuery("Users").Where("NOT EXISTS ?", subQuery).Select())
assertEqual(assert, "SELECT * FROM `Users` WHERE NOT EXISTS (SELECT * FROM `Products`)", query)
}
func TestRawWhere(t *testing.T) {
assert := assert.New(t)
query, _ := Build(NewQuery("Users").Where("ID != CompanyID").Where("DATE(CreatedAt) = DATE(LastLogin)").Select())
assertEqual(assert, "SELECT * FROM `Users` WHERE ID != CompanyID AND DATE(CreatedAt) = DATE(LastLogin)", query)
query, params := Build(NewQuery("Users").Where("ID != CompanyID").Where("DATE(CreatedAt) = DATE(LastLogin)").Where("Username = ?", "YamiOdymel").Select())
assertEqual(assert, "SELECT * FROM `Users` WHERE ID != CompanyID AND DATE(CreatedAt) = DATE(LastLogin) AND Username = ?", query)
assertParams(assert, []interface{}{"YamiOdymel"}, params)
}
//=======================================================
// As
//=======================================================
func TestAs(t *testing.T) {
assert := assert.New(t)
query, _ := Build(NewQuery(NewAlias("Products", "p")).Select())
assertEqual(assert, "SELECT * FROM `Products` AS p", query)
}
//=======================================================
// Distinct
//=======================================================
func TestDistinct(t *testing.T) {
assert := assert.New(t)
query, _ := Build(NewQuery("Users").Distinct().Select("Username"))
assertEqual(assert, "SELECT DISTINCT `Username` FROM `Users`", query)
}
//=======================================================
// Union
//=======================================================
func TestUnion(t *testing.T) {
assert := assert.New(t)
tableQuery := NewQuery("Locations").Select()
query, _ := Build(NewQuery("Users").Union(tableQuery).Select())
assertEqual(assert, "SELECT * FROM `Users` UNION (SELECT * FROM `Locations`)", query)
query, _ = Build(NewQuery("Users").Union(tableQuery).OrderBy("Username DESC").Select())
assertEqual(assert, "SELECT * FROM `Users` UNION (SELECT * FROM `Locations`) ORDER BY Username DESC", query)
subQueryA := NewQuery("Friends").Where("UserID_1 = ?", 1).Select("UserID_2")
subQueryB := NewQuery("Friends").Where("UserID_2 = ?", 1).Select("UserID_1")
query, _ = Build(NewQuery("Users").Where("ID IN ?", NewQuery(subQueryA).Union(subQueryB)).Select())
assertEqual(assert, "SELECT * FROM `Users` WHERE ID IN ((SELECT `UserID_2` FROM `Friends` WHERE UserID_1 = ?) UNION (SELECT `UserID_1` FROM `Friends` WHERE UserID_2 = ?))", query)
query, params := Build(NewQuery(NewQuery("Users").Union(tableQuery).Select()).Where("Username = ?", "YamiOdymel").Select())
assertEqual(assert, "SELECT * FROM (SELECT * FROM `Users` UNION (SELECT * FROM `Locations` WHERE Username = ?))", query)
assertParams(assert, []interface{}{"YamiOdymel"}, params)
}
func TestUnionOnly(t *testing.T) {
assert := assert.New(t)
userQuery := NewQuery("Users").OrderBy("Username DESC").Select()
tableQuery := NewQuery("Locations").OrderBy("House DESC").Select()
query, _ := Build(NewQuery(userQuery).Union(tableQuery).OrderBy("Username DESC"))
assertEqual(assert, "(SELECT * FROM `Users` ORDER BY Username DESC) UNION (SELECT * FROM `Locations` ORDER BY House DESC) ORDER BY Username DESC", query)
}
func TestUnionAll(t *testing.T) {
assert := assert.New(t)
tableQuery := NewQuery("Locations").Select()
query, _ := Build(NewQuery("Users").UnionAll(tableQuery).Select())
assertEqual(assert, "SELECT * FROM `Users` UNION ALL SELECT * FROM `Locations`", query)
query, params := Build(NewQuery(
NewQuery("Users").UnionAll(tableQuery).Select(),
).As("Result").Where("Username = ?", "YamiOdymel").Select())
assertEqual(assert, "SELECT * FROM (SELECT * FROM `Users` UNION ALL SELECT * FROM `Locations`) AS Result WHERE Username = ?", query)
assertParams(assert, []interface{}{"YamiOdymel"}, params)
}
//=======================================================
// Delete
//=======================================================
func TestDelete(t *testing.T) {
assert := assert.New(t)
query, params := Build(NewQuery("Users").Where("ID = ?", 1).Delete())
assertEqual(assert, "DELETE FROM `Users` WHERE ID = ?", query)
assertParams(assert, []interface{}{1}, params)
}
//=======================================================
// OrderBy
//=======================================================
func TestOrderBy(t *testing.T) {
assert := assert.New(t)
query, _ := Build(NewQuery("Users").OrderBy("ID ASC").OrderBy("Login DESC").OrderBy("RAND()").Select())
assertEqual(assert, "SELECT * FROM `Users` ORDER BY ID ASC, Login DESC, RAND()", query)
}
func TestOrderByField(t *testing.T) {
assert := assert.New(t)
query, params := Build(NewQuery("Users").OrderByField("UserGroup", "SuperUser", "Admin", "Users").Select())
assertEqual(assert, "SELECT * FROM `Users` ORDER BY FIELD (UserGroup, ?, ?, ?)", query)
assertParams(assert, []interface{}{"SuperUser", "Admin", "Users"}, params)
}
//=======================================================
// GroupBy
//=======================================================
func TestGroupBy(t *testing.T) {
assert := assert.New(t)
query, _ := Build(NewQuery("Users").GroupBy("Name").Select())
assertEqual(assert, "SELECT * FROM `Users` GROUP BY `Name`", query)
query, _ = Build(NewQuery("Users").GroupBy("Name", "ID").Select())
assertEqual(assert, "SELECT * FROM `Users` GROUP BY `Name`, `ID`", query)
}
//=======================================================
// Join
//=======================================================
func TestJoin(t *testing.T) {
assert := assert.New(t)
query, params := Build(NewQuery("Products").
CrossJoin("Users", "Products.TenantID = Users.TenantID").
Where("Users.ID = ?", 6).
Select("Users.Name", "Products.ProductName"))
assertEqual(assert, "SELECT Users.Name, Products.ProductName FROM `Products` CROSS JOIN `Users` ON (Products.TenantID = Users.TenantID) WHERE Users.ID = ?", query)
assertParams(assert, []interface{}{6}, params)
query, params = Build(NewQuery("Products").
LeftJoin("Users", "Products.TenantID = Users.TenantID").
Where("Users.ID = ?", 6).
Select("Users.Name", "Products.ProductName"))
assertEqual(assert, "SELECT Users.Name, Products.ProductName FROM `Products` LEFT JOIN `Users` ON (Products.TenantID = Users.TenantID) WHERE Users.ID = ?", query)
assertParams(assert, []interface{}{6}, params)
query, params = Build(NewQuery("Products").
RightJoin("Users", "Products.TenantID = Users.TenantID").
Where("Users.ID = ?", 6).
Select("Users.Name", "Products.ProductName"))
assertEqual(assert, "SELECT Users.Name, Products.ProductName FROM `Products` RIGHT JOIN `Users` ON (Products.TenantID = Users.TenantID) WHERE Users.ID = ?", query)
assertParams(assert, []interface{}{6}, params)
query, params = Build(NewQuery("Products").
InnerJoin("Users", "Products.TenantID = Users.TenantID").
Where("Users.ID = ?", 6).
Select("Users.Name", "Products.ProductName"))
assertEqual(assert, "SELECT Users.Name, Products.ProductName FROM `Products` INNER JOIN `Users` ON (Products.TenantID = Users.TenantID) WHERE Users.ID = ?", query)
assertParams(assert, []interface{}{6}, params)
query, params = Build(NewQuery("Products").
NaturalJoin("Users", "Products.TenantID = Users.TenantID").
Where("Users.ID = ?", 6).
Select("Users.Name", "Products.ProductName"))
assertEqual(assert, "SELECT Users.Name, Products.ProductName FROM `Products` NATURAL JOIN `Users` ON (Products.TenantID = Users.TenantID) WHERE Users.ID = ?", query)
assertParams(assert, []interface{}{6}, params)
query, params = Build(NewQuery("Products").
LeftJoin("Users", "Products.TenantID = Users.TenantID").
RightJoin("Posts", "Products.TenantID = Posts.TenantID").
Where("Users.ID = ?", 6).
Select("Users.Name", "Products.ProductName"))
assertEqual(assert, "SELECT Users.Name, Products.ProductName FROM `Products` LEFT JOIN `Users` ON (Products.TenantID = Users.TenantID) RIGHT JOIN `Posts` ON (Products.TenantID = Posts.TenantID) WHERE Users.ID = ?", query)
assertParams(assert, []interface{}{6}, params)
}
func TestJoinAlias(t *testing.T) {
assert := assert.New(t)
query, params := Build(NewQuery(NewAlias("Products", "p")).
LeftJoin(NewAlias("Users", "u"), "p.TenantID = u.TenantID").
Where("u.ID = ?", 6).
Select("u.Name", "p.ProductName"))
assertEqual(assert, "SELECT u.Name, p.ProductName FROM `Products` AS p LEFT JOIN `Users` AS u ON (p.TenantID = u.TenantID) WHERE u.ID = ?", query)
assertParams(assert, []interface{}{6}, params)
}
func TestJoinWhere(t *testing.T) {
assert := assert.New(t)
query, params := Build(NewQuery("Products").
LeftJoin("Users", "Products.TenantID = Users.TenantID").
OrJoinWhere("Users.TenantID = ?", 5).
Select("Users.Name", "Products.ProductName"))
assertEqual(assert, "SELECT Users.Name, Products.ProductName FROM `Products` LEFT JOIN `Users` ON (Products.TenantID = Users.TenantID OR Users.TenantID = ?)", query)
assertParams(assert, []interface{}{5}, params)
query, params = Build(NewQuery("Products").
LeftJoin("Users", "Products.TenantID = Users.TenantID").
JoinWhere("Users.Username = ?", "Wow").
Select("Users.Name", "Products.ProductName"))
assertEqual(assert, "SELECT Users.Name, Products.ProductName FROM `Products` LEFT JOIN `Users` ON (Products.TenantID = Users.TenantID AND Users.Username = ?)", query)
assertParams(assert, []interface{}{"Wow"}, params)
query, params = Build(NewQuery("Products").
LeftJoin("Users", "Products.TenantID = Users.TenantID").
RightJoin("Posts", "Products.TenantID = Posts.TenantID").
JoinWhere("Posts.Username = ?", "Wow").
JoinWhere("Users.Username = ?", "Wow").
Select("Users.Name", "Products.ProductName"))
assertEqual(assert, "SELECT Users.Name, Products.ProductName FROM `Products` LEFT JOIN `Users` ON (Products.TenantID = Users.TenantID AND Users.Username = ?) RIGHT JOIN `Posts` ON (Products.TenantID = Posts.TenantID AND Posts.Username = ?)", query)
assertParams(assert, []interface{}{"Wow", "Wow"}, params)
}
//=======================================================
// Sub Query
//=======================================================
func TestSubQuerySelect(t *testing.T) {
assert := assert.New(t)
subQuery := NewQuery("Products").Where("Quantity > ?", 2).Select("UserID")
query, params := Build(NewQuery("Users").Where("ID IN ?", subQuery).Select())
assertEqual(assert, "SELECT * FROM `Users` WHERE ID IN (SELECT `UserID` FROM `Products` WHERE Quantity > ?)", query)
assertParams(assert, []interface{}{2}, params)
}
func TestSubQueryInsert(t *testing.T) {
assert := assert.New(t)
subQuery := NewQuery("Users").Where("ID = ?", 6).SelectOne("Name")
query, params := Build(NewQuery("Products").Insert(H{
"ProductName": "測試商品",
"UserID": subQuery,
"LastUpdated": NewExpr("NOW()"),
}))
assertEqual(assert, "INSERT INTO `Products` (`LastUpdated`, `ProductName`, `UserID`) VALUES (NOW(), ?, (SELECT `Name` FROM `Users` WHERE ID = ? LIMIT 1))", query)
assertParams(assert, []interface{}{"測試商品", 6}, params)
}
func TestSubQueryJoin(t *testing.T) {
assert := assert.New(t)
subQuery := NewQuery("Users").As("Users").Where("Active = ?", 1).Select()
query, params := Build(NewQuery("Products").
LeftJoin(subQuery, "Products.UserID = Users.ID").
Select("Users.Username", "Products.ProductName"))
assertEqual(assert, "SELECT Users.Username, Products.ProductName FROM `Products` LEFT JOIN (SELECT * FROM `Users` WHERE Active = ?) AS Users ON (Products.UserID = Users.ID)", query)
assertParams(assert, []interface{}{1}, params)
}
func TestSubQueryJoinWhere(t *testing.T) {
assert := assert.New(t)
subQuery := NewQuery("Users").As("Users").Where("Active = ?", 1).Select()
query, params := Build(NewQuery("Products").
LeftJoin(subQuery, "Products.UserID = Users.ID").
JoinWhere("Users.Username = ?", "Hello").
Select("Users.Username", "Products.ProductName"))
assertEqual(assert, "SELECT Users.Username, Products.ProductName FROM `Products` LEFT JOIN (SELECT * FROM `Users` WHERE Active = ?) AS Users ON (Products.UserID = Users.ID AND Users.Username = ?)", query)
assertParams(assert, []interface{}{1, "Hello"}, params)
}
func TestSubQueryExists(t *testing.T) {
assert := assert.New(t)
subQuery := NewQuery("Users").Where("Company = ?", "測試公司").Select("UserID")
query, params := Build(NewQuery("Products").Where("EXISTS ?", subQuery).Select())
assertEqual(assert, "SELECT * FROM `Products` WHERE EXISTS (SELECT `UserID` FROM `Users` WHERE Company = ?)", query)