-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder_test.go
1440 lines (1331 loc) · 46.7 KB
/
builder_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 gobuilder
import (
"reflect"
"strings"
"testing"
)
var (
query string
params []any
queryExpected string
paramsExpected []any
gb = NewGoBuilder(Postgres)
)
func TestSql_Select(t *testing.T) {
queryExpected = "SELECT * FROM users"
paramsExpected = []any{}
query, params = gb.Table("users").Select().Prepare()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
queryExpected = "SELECT * FROM users"
query = gb.Table("users").Select().Sql()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
}
func TestSql_Select_With_Columns(t *testing.T) {
queryExpected = "SELECT firstname, lastname FROM users"
paramsExpected = []any{}
query, params = gb.Table("users").Select("firstname", "lastname").Prepare()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
queryExpected = "SELECT firstname, lastname FROM users"
query = gb.Table("users").Select("firstname", "lastname").Sql()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
}
func TestSql_Distinct(t *testing.T) {
queryExpected = "SELECT DISTINCT name, age FROM users"
paramsExpected := []any{}
query, params = gb.Table("users").SelectDistinct("name", "age").Prepare()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
queryExpected = "SELECT DISTINCT name, age FROM users"
query = gb.Table("users").SelectDistinct("name", "age").Sql()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
}
func TestSql_Create(t *testing.T) {
queryExpected = "INSERT INTO users (firstname, lastname) VALUES ($1, $2)"
paramsExpected := []any{"Mesut", "GENEZ"}
args := map[string]any{"firstname": "Mesut", "lastname": "GENEZ"}
query, params = gb.Table("users").Create(args).Prepare()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
queryExpected = "INSERT INTO users (firstname, lastname) VALUES ('Mesut', 'GENEZ')"
query = gb.Table("users").Create(args).Sql()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
}
func TestSql_Update(t *testing.T) {
// sometimes it may not pass the test because the map is unordered
args := map[string]any{"firstname": "Mesut", "lastname": "GENEZ"}
queryExpected = "UPDATE users SET firstname = $1, lastname = $2"
paramsExpected := []any{"Mesut", "GENEZ"}
query, params = gb.Table("users").Update(args).Prepare()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
queryExpected = "UPDATE users SET firstname = 'Mesut', lastname = 'GENEZ'"
query = gb.Table("users").Update(args).Sql()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
}
func TestSql_Delete(t *testing.T) {
queryExpected = "DELETE FROM users"
paramsExpected := []any{}
query, params = gb.Table("users").Delete().Prepare()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
queryExpected = "DELETE FROM users"
query = gb.Table("users").Delete().Sql()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
}
func TestSql_Where(t *testing.T) {
queryExpected = "WHERE firstname = $1"
paramsExpected := []any{"Mesut"}
query, params = gb.Where("firstname", "=", "Mesut").Prepare()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
queryExpected = "WHERE firstname = 'Mesut'"
query = gb.Where("firstname", "=", "Mesut").Sql()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
}
func TestSql_WhereWithInt(t *testing.T) {
queryExpected = "WHERE id = $1"
paramsExpected := []any{55}
query, params = gb.Where("id", "=", 55).Prepare()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
queryExpected = "WHERE id = 55"
query = gb.Where("id", "=", 55).Sql()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
}
func TestSql_WhereWithFloat(t *testing.T) {
queryExpected = "WHERE amount = $1"
paramsExpected := []any{55.5}
query, params = gb.Where("amount", "=", 55.5).Prepare()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
queryExpected = "WHERE amount = 55.5"
query = gb.Where("amount", "=", 55.5).Sql()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
}
func TestSql_Where_With_And(t *testing.T) {
queryExpected = "WHERE firstname = $1 AND lastname = $2"
paramsExpected := []any{"Mesut", "GENEZ"}
query, params = gb.Where("firstname", "=", "Mesut").Where("lastname", "=", "GENEZ").Prepare()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
queryExpected = "WHERE firstname = 'Mesut' AND lastname = 'GENEZ'"
query = gb.Where("firstname", "=", "Mesut").Where("lastname", "=", "GENEZ").Sql()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
}
func TestSql_OrWhere(t *testing.T) {
queryExpected = "WHERE firstname = $1"
paramsExpected := []any{"Mesut"}
query, params = gb.OrWhere("firstname", "=", "Mesut").Prepare()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
queryExpected = "WHERE firstname = 'Mesut'"
query = gb.OrWhere("firstname", "=", "Mesut").Sql()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
}
func TestSql_OrWhere_With_And(t *testing.T) {
queryExpected = "WHERE firstname = $1 OR lastname = $2"
paramsExpected := []any{"Mesut", "GENEZ"}
query, params = gb.OrWhere("firstname", "=", "Mesut").OrWhere("lastname", "=", "GENEZ").Prepare()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
queryExpected = "WHERE firstname = 'Mesut' OR lastname = 'GENEZ'"
query = gb.OrWhere("firstname", "=", "Mesut").OrWhere("lastname", "=", "GENEZ").Sql()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
}
func TestSql_In(t *testing.T) {
queryExpected = "WHERE firstname IN ($1, $2)"
paramsExpected := []any{"Mesut", 33}
query, params = gb.In("firstname", "Mesut", 33).Prepare()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
queryExpected = "WHERE firstname IN ('Mesut', 33)"
query = gb.In("firstname", "Mesut", 33).Sql()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
}
func TestSql_InWithInt(t *testing.T) {
queryExpected = "WHERE id IN ($1, $2, $3)"
paramsExpected := []any{12, 34, 55}
query, params = gb.In("id", 12, 34, 55).Prepare()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
queryExpected = "WHERE id IN (12, 34, 55)"
query = gb.In("id", 12, 34, 55).Sql()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
}
func TestSql_InAnd(t *testing.T) {
queryExpected = "WHERE firstname IN ($1) AND lastname IN ($2)"
paramsExpected := []any{"Mesut", "GENEZ"}
query, params = gb.In("firstname", "Mesut").In("lastname", "GENEZ").Prepare()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
queryExpected = "WHERE firstname IN ('Mesut') AND lastname IN ('GENEZ')"
query = gb.In("firstname", "Mesut").In("lastname", "GENEZ").Sql()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
}
func TestSql_OrIn(t *testing.T) {
queryExpected = "WHERE firstname IN ($1)"
paramsExpected := []any{"Mesut"}
query, params = gb.OrIn("firstname", "Mesut").Prepare()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
queryExpected = "WHERE firstname IN ('Mesut')"
query = gb.OrIn("firstname", "Mesut").Sql()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
}
func TestSql_OrInAnd(t *testing.T) {
queryExpected = "WHERE firstname IN ($1, $2) OR lastname IN ($3)"
paramsExpected := []any{"Mesut", "GENEZ", "GENEZ"}
query, params = gb.OrIn("firstname", "Mesut", "GENEZ").OrIn("lastname", "GENEZ").Prepare()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
queryExpected = "WHERE firstname IN ('Mesut', 'GENEZ') OR lastname IN ('GENEZ')"
query = gb.OrIn("firstname", "Mesut", "GENEZ").OrIn("lastname", "GENEZ").Sql()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
}
func TestSql_Between(t *testing.T) {
queryExpected = "WHERE firstname BETWEEN $1 AND $2"
paramsExpected := []any{"Mesut", "GENEZ"}
query, params = gb.Between("firstname", "Mesut", "GENEZ").Prepare()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
queryExpected = "WHERE firstname BETWEEN 'Mesut' AND 'GENEZ'"
query = gb.Between("firstname", "Mesut", "GENEZ").Sql()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
}
func TestSql_BetweenWithInt(t *testing.T) {
queryExpected = "WHERE id BETWEEN $1 AND $2"
paramsExpected := []any{12, 55}
query, params = gb.Between("id", 12, 55).Prepare()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
queryExpected = "WHERE id BETWEEN 12 AND 55"
query = gb.Between("id", 12, 55).Sql()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
}
func TestSql_BetweenWithWhere(t *testing.T) {
queryExpected = "WHERE firstname BETWEEN $1 AND $2 AND lastname BETWEEN $3 AND $4"
paramsExpected := []any{"Mesut", "GENEZ", "Mesut", "GENEZ"}
query, params = gb.Between("firstname", "Mesut", "GENEZ").Between("lastname", "Mesut", "GENEZ").Prepare()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
queryExpected = "WHERE firstname BETWEEN 'Mesut' AND 'GENEZ' AND lastname BETWEEN 'Mesut' AND 'GENEZ'"
query = gb.Between("firstname", "Mesut", "GENEZ").Between("lastname", "Mesut", "GENEZ").Sql()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
}
func TestSql_OrBetween(t *testing.T) {
queryExpected = "WHERE firstname BETWEEN $1 AND $2"
paramsExpected := []any{"Mesut", "GENEZ"}
query, params = gb.OrBetween("firstname", "Mesut", "GENEZ").Prepare()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
queryExpected = "WHERE firstname BETWEEN 'Mesut' AND 'GENEZ'"
query = gb.OrBetween("firstname", "Mesut", "GENEZ").Sql()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
}
func TestSql_OrBetweenWithWhere(t *testing.T) {
queryExpected = "WHERE firstname BETWEEN $1 AND $2 OR lastname BETWEEN $3 AND $4"
paramsExpected := []any{"Mesut", "GENEZ", "Mesut", "GENEZ"}
query, params = gb.OrBetween("firstname", "Mesut", "GENEZ").OrBetween("lastname", "Mesut", "GENEZ").Prepare()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
queryExpected = "WHERE firstname BETWEEN 'Mesut' AND 'GENEZ' OR lastname BETWEEN 'Mesut' AND 'GENEZ'"
query = gb.OrBetween("firstname", "Mesut", "GENEZ").OrBetween("lastname", "Mesut", "GENEZ").Sql()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
}
func TestSql_IsNull(t *testing.T) {
queryExpected = "SELECT * FROM users WHERE age = $1 AND name IS NULL"
paramsExpected := []any{30}
query, params = gb.Table("users").Select().Where("age", "=", 30).IsNull("name").Prepare()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
queryExpected = "SELECT * FROM users WHERE age = 30 AND name IS NULL"
query = gb.Table("users").Select().Where("age", "=", 30).IsNull("name").Sql()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
}
func TestSql_IsNotNull(t *testing.T) {
queryExpected = "SELECT * FROM users WHERE age = $1 AND name IS NOT NULL"
paramsExpected := []any{30}
query, params = gb.Table("users").Select().Where("age", "=", 30).IsNotNull("name").Prepare()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
queryExpected = "SELECT * FROM users WHERE age = 30 AND name IS NOT NULL"
query = gb.Table("users").Select().Where("age", "=", 30).IsNotNull("name").Sql()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
}
func TestSql_Having(t *testing.T) {
queryExpected = "SELECT * FROM orders GROUP BY user_id HAVING COUNT(*) > 1"
paramsExpected := []any{}
query, params = gb.Table("orders").Select().GroupBy("user_id").Having("COUNT(*) > 1").Prepare()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
query = gb.Table("orders").Select().GroupBy("user_id").Having("COUNT(*) > 1").Sql()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
}
func TestSql_Join(t *testing.T) {
queryExpected = "INNER JOIN users ON users.id = roles.user_id"
paramsExpected := []any{}
query, params = gb.Join("users", "users.id", "=", "roles.user_id").Prepare()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
query = gb.Join("users", "users.id", "=", "roles.user_id").Sql()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
}
func TestSql_Limit(t *testing.T) {
queryExpected = "OFFSET 1 LIMIT 5"
paramsExpected := []any{}
query, params = gb.Limit(1, 5).Prepare()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
query = gb.Limit(1, 5).Sql()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
}
func TestSql_Group(t *testing.T) {
queryExpected = "GROUP BY firstname"
paramsExpected := []any{}
query, params = gb.GroupBy("firstname").Prepare()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
}
func TestSql_GroupMultiple(t *testing.T) {
queryExpected = "GROUP BY firstname, lastname, email"
paramsExpected := []any{}
query, params = gb.GroupBy("firstname", "lastname", "email").Prepare()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
}
func TestSql_OrderBy(t *testing.T) {
queryExpected = "ORDER BY firstname ASC"
paramsExpected := []any{}
query, params = gb.OrderBy("firstname").Prepare()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
}
func TestSql_OrderByMultiple(t *testing.T) {
queryExpected = "ORDER BY firstname, lastname ASC"
paramsExpected := []any{}
query, params = gb.OrderBy("firstname", "lastname").Prepare()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
}
func TestSql_OrderByDesc(t *testing.T) {
queryExpected = "ORDER BY firstname DESC"
paramsExpected := []any{}
query, params = gb.OrderByDesc("firstname").Prepare()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
}
func TestSql_OrderByDescMultiple(t *testing.T) {
queryExpected = "ORDER BY firstname, lastname DESC"
paramsExpected := []any{}
query, params = gb.OrderByDesc("firstname", "lastname").Prepare()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
}
func TestSql_Union(t *testing.T) {
// Test basic UNION
query1 := gb.Table("users").
Select("name", "email").
Where("type", "=", "customer")
query2 := NewGoBuilder(Postgres).
Table("companies").
Select("company_name", "company_email").
Where("status", "=", "active")
query, params = query1.Union(query2).Prepare()
queryExpected := "SELECT name, email FROM users WHERE type = $1 UNION SELECT company_name, company_email FROM companies WHERE status = $2"
paramsExpected := []any{"customer", "active"}
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
// Test UNION ALL
query1 = gb.Table("sales").
Select("date", "amount").
Where("year", "=", 2023)
query2 = NewGoBuilder(Postgres).
Table("purchases").
Select("date", "amount").
Where("year", "=", 2023)
query, params = query1.UnionAll(query2).Prepare()
queryExpected = "SELECT date, amount FROM sales WHERE year = $1 UNION ALL SELECT date, amount FROM purchases WHERE year = $2"
paramsExpected = []any{2023, 2023}
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
// Test UNION with ORDER BY
query1 = gb.Table("employees").
Select("id", "salary").
Where("department", "=", "IT")
query2 = NewGoBuilder(Postgres).
Table("contractors").
Select("id", "rate as salary").
Where("status", "=", "active")
query, params = query1.Union(query2).OrderBy("salary").Prepare()
queryExpected = "SELECT id, salary FROM employees WHERE department = $1 UNION SELECT id, rate as salary FROM contractors WHERE status = $2 ORDER BY salary ASC"
paramsExpected = []any{"IT", "active"}
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
// Test multiple UNIONs
query1 = gb.Table("full_time").
Select("id", "name", "'full_time' as type").
Where("active", "=", true)
query2 = NewGoBuilder(Postgres).
Table("part_time").
Select("id", "name", "'part_time' as type").
Where("active", "=", true)
query3 := NewGoBuilder(Postgres).
Table("contractors").
Select("id", "name", "'contractor' as type").
Where("active", "=", true)
query, params = query1.Union(query2).Union(query3).OrderBy("type").Prepare()
queryExpected = "SELECT id, name, 'full_time' as type FROM full_time WHERE active = $1 UNION SELECT id, name, 'part_time' as type FROM part_time WHERE active = $2 UNION SELECT id, name, 'contractor' as type FROM contractors WHERE active = $3 ORDER BY type ASC"
paramsExpected = []any{true, true, true}
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
}
func TestSql_SubQuery(t *testing.T) {
paramsExpected := []any{"SELECT id FROM users WHERE age > $1"}
mainBuilder := NewGoBuilder(Postgres)
subBuilder := NewGoBuilder(Postgres)
query, params = subBuilder.Table("users").Select("id").Where("age", ">", 30).Prepare()
query, params = mainBuilder.Table("orders").Select("order_id", "user_id").In("user_id", query).Prepare()
queryExpected = "SELECT order_id, user_id FROM orders WHERE user_id IN ($1)"
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
}
func TestSql_ErrorHandling(t *testing.T) {
gb := NewGoBuilder(Postgres)
// Table olmadan sorgu oluşturma denemesi
gb.Select("id").Where("age", ">", 30)
if gb.Error() == nil {
t.Error("Table olmadan sorgu oluşturulduğunda hata vermeli")
}
}
func TestSql_JsonOperations(t *testing.T) {
testCases := []struct {
name string
builder func() (string, []any)
expected string
params []any
}{
{
name: "Simple JSON Path",
builder: func() (string, []any) {
return gb.Table("users").Select().Where("data->>'name'", "=", "John").Prepare()
},
expected: "SELECT * FROM users WHERE data->>'name' = $1",
params: []any{"John"},
},
{
name: "PostgreSQL JSON Contains",
builder: func() (string, []any) {
gb := NewGoBuilder(Postgres)
return gb.Table("users").
Select("name").
WhereJsonContains("preferences", `{"theme": "dark"}`).
Prepare()
},
expected: "SELECT name FROM users WHERE preferences @> $1",
params: []any{`{"theme": "dark"}`},
},
{
name: "MySQL JSON Contains",
builder: func() (string, []any) {
gb := NewGoBuilder(MySQL)
return gb.Table("users").
Select("name").
WhereJsonContains("preferences", `{"theme": "dark"}`).
Prepare()
},
expected: "SELECT name FROM users WHERE JSON_CONTAINS(preferences, ?)",
params: []any{`{"theme": "dark"}`},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
query, params := tc.builder()
if query != tc.expected {
t.Errorf("expected query %v, got %v", tc.expected, query)
}
if !reflect.DeepEqual(params, tc.params) {
t.Errorf("expected params %v, got %v", tc.params, params)
}
})
}
}
func TestSql_AggregateFunctions(t *testing.T) {
testCases := []struct {
name string
builder func() (string, []any)
expected string
params []any
}{
{
name: "Count",
builder: func() (string, []any) {
return gb.Table("users").Select("COUNT(*) as total").Prepare()
},
expected: "SELECT COUNT(*) as total FROM users",
params: []any{},
},
{
name: "Sum",
builder: func() (string, []any) {
return gb.Table("orders").Select("SUM(amount) as total_amount").Prepare()
},
expected: "SELECT SUM(amount) as total_amount FROM orders",
params: []any{},
},
{
name: "Multiple Aggregates",
builder: func() (string, []any) {
return gb.Table("orders").
Select(
"customer_id",
"COUNT(*) as total_orders",
"SUM(amount) as total_amount",
"AVG(amount) as avg_amount",
"MIN(amount) as min_amount",
"MAX(amount) as max_amount",
).
GroupBy("customer_id").
Having("COUNT(*) > ?", 5).
OrderBy("total_amount").
Prepare()
},
expected: "SELECT customer_id, COUNT(*) as total_orders, SUM(amount) as total_amount, AVG(amount) as avg_amount, MIN(amount) as min_amount, MAX(amount) as max_amount FROM orders GROUP BY customer_id HAVING COUNT(*) > $1 ORDER BY total_amount ASC",
params: []any{5},
},
{
name: "Aggregate with CASE",
builder: func() (string, []any) {
return gb.Table("orders").
Select(
"status",
"COUNT(*) as total",
"SUM(CASE WHEN amount > 1000 THEN 1 ELSE 0 END) as high_value_orders",
).
GroupBy("status").
Prepare()
},
expected: "SELECT status, COUNT(*) as total, SUM(CASE WHEN amount > 1000 THEN 1 ELSE 0 END) as high_value_orders FROM orders GROUP BY status",
params: []any{},
},
{
name: "Aggregate with Having and Complex Conditions",
builder: func() (string, []any) {
return gb.Table("sales").
Select(
"department",
"COUNT(*) as total_sales",
"AVG(amount) as avg_sale",
).
GroupBy("department").
Having("COUNT(*) > ? AND AVG(amount) > ?", 10, 1000).
OrderByDesc("avg_sale").
Prepare()
},
expected: "SELECT department, COUNT(*) as total_sales, AVG(amount) as avg_sale FROM sales GROUP BY department HAVING COUNT(*) > $1 AND AVG(amount) > $2 ORDER BY avg_sale DESC",
params: []any{10, 1000},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
query, params := tc.builder()
if query != tc.expected {
t.Errorf("expected query %v, got %v", tc.expected, query)
}
if !reflect.DeepEqual(params, tc.params) {
t.Errorf("expected params %v, got %v", tc.params, params)
}
})
}
}
func TestSql_WithCTE(t *testing.T) {
subQuery := NewGoBuilder(Postgres).Table("orders").Select("user_id", "COUNT(*) as order_count").GroupBy("user_id")
queryExpected := "WITH user_orders AS (SELECT user_id, COUNT(*) as order_count FROM orders GROUP BY user_id) SELECT * FROM user_orders WHERE order_count > $1"
paramsExpected := []any{5}
query, params := gb.With("user_orders", subQuery).Table("user_orders").Select().Where("order_count", ">", 5).Prepare()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
}
func TestSql_BatchInsert(t *testing.T) {
records := []map[string]any{
{"name": "John", "age": 30},
{"name": "Jane", "age": 25},
}
queryExpected := "INSERT INTO users (age, name) VALUES ($1, $2), ($3, $4)"
paramsExpected := []any{30, "John", 25, "Jane"}
query, params := gb.Table("users").CreateBatch(records).Prepare()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
}
func TestSql_LockMechanism(t *testing.T) {
queryExpected := "SELECT * FROM users FOR UPDATE"
paramsExpected := []any{}
query, params := gb.Table("users").Select().Lock("FOR UPDATE").Prepare()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
}
func TestSql_ConditionalClauses(t *testing.T) {
age := 30
name := "John"
queryExpected := "SELECT * FROM users WHERE age > $1 AND name = $2"
paramsExpected := []any{30, "John"}
query, params := gb.Table("users").Select().WhenThen(age > 0, func(b *GoBuilder) *GoBuilder { return b.Where("age", ">", age) }, nil).WhenThen(name != "", func(b *GoBuilder) *GoBuilder { return b.Where("name", "=", name) }, nil).Prepare()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
}
func TestSql_DatabaseSpecificFeatures(t *testing.T) {
testCases := []struct {
name string
dialect SQLDialect
builder func(gb *GoBuilder) (string, []any)
expected string
params []any
}{
{
name: "PostgreSQL - RETURNING Clause",
dialect: Postgres,
builder: func(gb *GoBuilder) (string, []any) {
return gb.Table("users").Create(map[string]any{"name": "John"}, "id").Prepare()
},
expected: "INSERT INTO users (name) VALUES ($1) RETURNING id",
params: []any{"John"},
},
{
name: "MySQL - ON DUPLICATE KEY UPDATE",
dialect: MySQL,
builder: func(gb *GoBuilder) (string, []any) {
return gb.Table("users").Create(map[string]any{"id": 1, "name": "John"}).OnDuplicateKeyUpdate(map[string]any{"name": "John"}).Prepare()
},
expected: "INSERT INTO users (id, name) VALUES (?, ?) ON DUPLICATE KEY UPDATE name = ?",
params: []any{1, "John", "John"},
},
{
name: "SQLServer - TOP Clause",
dialect: SQLServer,
builder: func(gb *GoBuilder) (string, []any) {
return gb.Table("users").Select().Top(10).Prepare()
},
expected: "SELECT TOP 10 * FROM users",
params: []any{},
},
{
name: "Oracle - ROWNUM",
dialect: Oracle,
builder: func(gb *GoBuilder) (string, []any) {
return gb.Table("users").Select().Where("ROWNUM", "<=", 10).Prepare()
},
expected: "SELECT * FROM users WHERE ROWNUM <= :1",
params: []any{10},
},
{
name: "SQLite - PRAGMA",
dialect: SQLite,
builder: func(gb *GoBuilder) (string, []any) {
return gb.Pragma("foreign_keys", "ON").Prepare()
},
expected: "PRAGMA foreign_keys = ON",
params: []any{},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
gb := NewGoBuilder(tc.dialect)
query, params := tc.builder(gb)
if query != tc.expected {
t.Errorf("expected query %v, got %v", tc.expected, query)
}
if !reflect.DeepEqual(params, tc.params) {
t.Errorf("expected params %v, got %v", tc.params, params)
}
})
}
}
func TestSql_ComplexJoins(t *testing.T) {
testCases := []struct {
name string
builder func() (string, []any)
expected string
params []any
}{
{
name: "Multiple Inner Joins",
builder: func() (string, []any) {
return gb.Table("orders").
Select("orders.id", "customers.name", "products.title").
Join("customers", "customers.id", "=", "orders.customer_id").
Join("products", "products.id", "=", "orders.product_id").
Where("orders.status", "=", "pending").
Prepare()
},
expected: "SELECT orders.id, customers.name, products.title FROM orders INNER JOIN customers ON customers.id = orders.customer_id INNER JOIN products ON products.id = orders.product_id WHERE orders.status = $1",
params: []any{"pending"},
},
{
name: "Mixed Join Types",
builder: func() (string, []any) {
return gb.Table("users").
Select("users.name", "orders.total", "addresses.city").
LeftJoin("orders", "orders.user_id", "=", "users.id").
RightJoin("addresses", "addresses.user_id", "=", "users.id").
Where("users.status", "=", "active").
Prepare()
},
expected: "SELECT users.name, orders.total, addresses.city FROM users LEFT JOIN orders ON orders.user_id = users.id RIGHT JOIN addresses ON addresses.user_id = users.id WHERE users.status = $1",
params: []any{"active"},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
query, params := tc.builder()
if query != tc.expected {
t.Errorf("expected query %v, got %v", tc.expected, query)
}
if !reflect.DeepEqual(params, tc.params) {
t.Errorf("expected params %v, got %v", tc.params, params)
}
})
}
}
func TestSql_ComplexSubqueries(t *testing.T) {
testCases := []struct {
name string
builder func() (string, []any)
expected string
params []any
}{