-
Notifications
You must be signed in to change notification settings - Fork 12
/
marshaller_test.go
2024 lines (1801 loc) · 67.3 KB
/
marshaller_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
/*
* Copyright (c) 2018, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
package soql_test
import (
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/forcedotcom/go-soql"
)
var _ = Describe("Marshaller", func() {
Describe("MarshalWhereClause", func() {
var (
clause string
expectedClause string
err error
)
Context("when non pointer value is passed as argument", func() {
var (
critetria TestQueryCriteria
)
JustBeforeEach(func() {
clause, err = MarshalWhereClause(critetria)
Expect(err).ToNot(HaveOccurred())
})
Context("when there are no fields populated", func() {
It("returns empty where clause", func() {
Expect(err).ToNot(HaveOccurred())
Expect(clause).To(BeEmpty())
})
})
Context("when only like clause pattern is populated", func() {
Context("when there is only one item in the like clause array", func() {
BeforeEach(func() {
critetria = TestQueryCriteria{
IncludeNamePattern: []string{"-db"},
}
expectedClause = "Host_Name__c LIKE '%-db%'"
})
It("returns where clause with only one condition", func() {
Expect(clause).To(Equal(expectedClause))
})
})
Context("when there is more than one item in the like clause array", func() {
BeforeEach(func() {
critetria = TestQueryCriteria{
IncludeNamePattern: []string{"-db", "-dbmgmt", "-dgdb"},
}
expectedClause = "(Host_Name__c LIKE '%-db%' OR Host_Name__c LIKE '%-dbmgmt%' OR Host_Name__c LIKE '%-dgdb%')"
})
It("returns where clause with OR condition", func() {
Expect(clause).To(Equal(expectedClause))
})
})
Context("when there is single quote in values", func() {
BeforeEach(func() {
critetria = TestQueryCriteria{
IncludeNamePattern: []string{"-db'", "-dbmgmt", "-dgdb"},
}
expectedClause = "(Host_Name__c LIKE '%-db\\'%' OR Host_Name__c LIKE '%-dbmgmt%' OR Host_Name__c LIKE '%-dgdb%')"
})
It("returns appropriate where clause by escaping single quote", func() {
Expect(clause).To(Equal(expectedClause))
})
})
Context("when there are special characters in values", func() {
BeforeEach(func() {
critetria = TestQueryCriteria{
IncludeNamePattern: []string{"-db'_%\\"},
AssetType: "-db'_%\\",
}
expectedClause = `Host_Name__c LIKE '%-db\'\_\%\\%' AND Tech_Asset__r.Asset_Type_Asset_Type__c = '-db\'_%\\'`
})
It("returns appropriate where clause by escaping special characters", func() {
Expect(clause).To(Equal(expectedClause))
})
})
})
Context("when only not like clause is populated", func() {
Context("when there is only one item in the not like clause array", func() {
BeforeEach(func() {
critetria = TestQueryCriteria{
ExcludeNamePattern: []string{"-db"},
}
expectedClause = "(NOT Host_Name__c LIKE '%-db%')"
})
It("returns where clause with only one condition", func() {
Expect(clause).To(Equal(expectedClause))
})
})
Context("when there is more than one item in the not like clause array", func() {
BeforeEach(func() {
critetria = TestQueryCriteria{
ExcludeNamePattern: []string{"-db", "-dbmgmt", "-dgdb"},
}
expectedClause = "((NOT Host_Name__c LIKE '%-db%') AND (NOT Host_Name__c LIKE '%-dbmgmt%') AND (NOT Host_Name__c LIKE '%-dgdb%'))"
})
It("returns where clause with OR condition", func() {
Expect(clause).To(Equal(expectedClause))
})
})
Context("when there is single quote in values", func() {
BeforeEach(func() {
critetria = TestQueryCriteria{
ExcludeNamePattern: []string{"-d'b"},
}
expectedClause = "(NOT Host_Name__c LIKE '%-d\\'b%')"
})
It("returns appropriate where clause by escaping single quote", func() {
Expect(clause).To(Equal(expectedClause))
})
})
})
Context("when only equalsClause is populated", func() {
BeforeEach(func() {
critetria = TestQueryCriteria{
AssetType: "SERVER",
}
expectedClause = "Tech_Asset__r.Asset_Type_Asset_Type__c = 'SERVER'"
})
It("returns appropriate where clause", func() {
Expect(clause).To(Equal(expectedClause))
})
Context("when value has single quote", func() {
BeforeEach(func() {
critetria = TestQueryCriteria{
AssetType: "SER'VER",
}
expectedClause = "Tech_Asset__r.Asset_Type_Asset_Type__c = 'SER\\'VER'"
})
It("returns appropriate where clause by escaping single quote", func() {
Expect(clause).To(Equal(expectedClause))
})
})
})
Context("when only notEqualsClause is populated", func() {
BeforeEach(func() {
critetria = TestQueryCriteria{
Status: "InActive",
}
expectedClause = "Status__c != 'InActive'"
})
It("returns appropriate where clause", func() {
Expect(clause).To(Equal(expectedClause))
})
Context("when value has single quote", func() {
BeforeEach(func() {
critetria = TestQueryCriteria{
Status: "In'Active",
}
expectedClause = "Status__c != 'In\\'Active'"
})
It("returns appropriate where clause by escaping single quote", func() {
Expect(clause).To(Equal(expectedClause))
})
})
})
Context("when only inClause is populated", func() {
Context("when there is only one item in the inClause array", func() {
BeforeEach(func() {
critetria = TestQueryCriteria{
Roles: []string{"db"},
}
expectedClause = "Role__r.Name IN ('db')"
})
It("returns where clause with only one item in IN clause", func() {
Expect(clause).To(Equal(expectedClause))
})
})
Context("when there is more than one item in the inClause array", func() {
BeforeEach(func() {
critetria = TestQueryCriteria{
Roles: []string{"db", "dbmgmt"},
}
expectedClause = "Role__r.Name IN ('db','dbmgmt')"
})
It("returns where clause with all the items in IN clause", func() {
Expect(clause).To(Equal(expectedClause))
})
})
Context("when value has single quote", func() {
BeforeEach(func() {
critetria = TestQueryCriteria{
Roles: []string{"db", "db'mgmt"},
}
expectedClause = "Role__r.Name IN ('db','db\\'mgmt')"
})
It("returns appropriate where clause by escaping single quote", func() {
Expect(clause).To(Equal(expectedClause))
})
})
})
Context("when only notInClause is populated", func() {
Context("when there is only one item in the inClause array", func() {
BeforeEach(func() {
critetria = TestQueryCriteria{
ExcludeIDs: []string{"123"},
}
expectedClause = "id NOT IN ('123')"
})
It("returns where clause with only one item in NOT IN clause", func() {
Expect(clause).To(Equal(expectedClause))
})
})
Context("when there is more than one item in the notInClause array", func() {
BeforeEach(func() {
critetria = TestQueryCriteria{
ExcludeIDs: []string{"123", "456"},
}
expectedClause = "id NOT IN ('123','456')"
})
It("returns where clause with all the items in NOT IN clause", func() {
Expect(clause).To(Equal(expectedClause))
})
})
Context("when value has single quote", func() {
BeforeEach(func() {
critetria = TestQueryCriteria{
ExcludeIDs: []string{"123", "4'56"},
}
expectedClause = "id NOT IN ('123','4\\'56')"
})
It("returns appropriate where clause by escaping single quote", func() {
Expect(clause).To(Equal(expectedClause))
})
})
})
Context("when only null clause is populated", func() {
Context("when null is allowed", func() {
BeforeEach(func() {
allowNull := true
critetria = TestQueryCriteria{
AllowNullLastDiscoveredDate: &allowNull,
}
expectedClause = "Last_Discovered_Date__c = null"
})
It("returns appropriate where clause", func() {
Expect(clause).To(Equal(expectedClause))
})
})
Context("when null is not allowed", func() {
BeforeEach(func() {
allowNull := false
critetria = TestQueryCriteria{
AllowNullLastDiscoveredDate: &allowNull,
}
expectedClause = "Last_Discovered_Date__c != null"
})
It("returns appropriate where clause", func() {
Expect(clause).To(Equal(expectedClause))
})
})
})
Context("when likeOperator and inClause are populated", func() {
BeforeEach(func() {
critetria = TestQueryCriteria{
IncludeNamePattern: []string{"-db", "-dbmgmt"},
Roles: []string{"db"},
}
expectedClause = "(Host_Name__c LIKE '%-db%' OR Host_Name__c LIKE '%-dbmgmt%') AND Role__r.Name IN ('db')"
})
It("returns properly formed clause for name and role joined by AND clause", func() {
Expect(clause).To(Equal(expectedClause))
})
})
Context("when likeOperator and equalsClause are populated", func() {
BeforeEach(func() {
critetria = TestQueryCriteria{
IncludeNamePattern: []string{"-db", "-dbmgmt"},
AssetType: "SERVER",
}
expectedClause = "(Host_Name__c LIKE '%-db%' OR Host_Name__c LIKE '%-dbmgmt%') AND Tech_Asset__r.Asset_Type_Asset_Type__c = 'SERVER'"
})
It("returns properly formed clause for likeOperator and inClause joined by AND clause", func() {
Expect(clause).To(Equal(expectedClause))
})
})
Context("when both likeOperator and notlikeOperator are populated", func() {
BeforeEach(func() {
critetria = TestQueryCriteria{
IncludeNamePattern: []string{"-db", "-dbmgmt"},
ExcludeNamePattern: []string{"-core", "-drp"},
}
expectedClause = "(Host_Name__c LIKE '%-db%' OR Host_Name__c LIKE '%-dbmgmt%') AND ((NOT Host_Name__c LIKE '%-core%') AND (NOT Host_Name__c LIKE '%-drp%'))"
})
It("returns properly formed clause for likeOperator and notlikeOperator joined by AND clause", func() {
Expect(clause).To(Equal(expectedClause))
})
})
Context("when all clauses are populated", func() {
BeforeEach(func() {
allowNull := false
critetria = TestQueryCriteria{
AssetType: "SERVER",
IncludeNamePattern: []string{"-db", "-dbmgmt"},
Roles: []string{"db", "dbmgmt"},
ExcludeNamePattern: []string{"-core", "-drp"},
AllowNullLastDiscoveredDate: &allowNull,
ExcludeIDs: []string{"123", "456"},
}
expectedClause = "(Host_Name__c LIKE '%-db%' OR Host_Name__c LIKE '%-dbmgmt%') AND Role__r.Name IN ('db','dbmgmt') AND ((NOT Host_Name__c LIKE '%-core%') AND (NOT Host_Name__c LIKE '%-drp%')) AND Tech_Asset__r.Asset_Type_Asset_Type__c = 'SERVER' AND Last_Discovered_Date__c != null AND id NOT IN ('123','456')"
})
It("returns properly formed clause joined by AND clause", func() {
Expect(clause).To(Equal(expectedClause))
})
})
})
Context("when pointer is passed as argument", func() {
var (
critetria *TestQueryCriteria
)
JustBeforeEach(func() {
clause, err = MarshalWhereClause(critetria)
})
Context("when nil is passed as argument", func() {
It("returns empty where clause", func() {
Expect(err).To(Equal(ErrNilValue))
Expect(clause).To(BeEmpty())
})
})
Context("when empty value is passed as argument", func() {
BeforeEach(func() {
critetria = &TestQueryCriteria{}
})
It("returns empty where clause", func() {
Expect(clause).To(BeEmpty())
})
})
Context("when all values are populated", func() {
BeforeEach(func() {
critetria = &TestQueryCriteria{
AssetType: "SERVER",
IncludeNamePattern: []string{"-db", "-dbmgmt"},
Roles: []string{"db", "dbmgmt"},
ExcludeNamePattern: []string{"-core", "-drp"},
}
expectedClause = "(Host_Name__c LIKE '%-db%' OR Host_Name__c LIKE '%-dbmgmt%') AND Role__r.Name IN ('db','dbmgmt') AND ((NOT Host_Name__c LIKE '%-core%') AND (NOT Host_Name__c LIKE '%-drp%')) AND Tech_Asset__r.Asset_Type_Asset_Type__c = 'SERVER'"
})
It("returns properly formed clause joined by AND clause", func() {
Expect(clause).To(Equal(expectedClause))
})
})
})
Context("When values for date literals are int", func() {
var criteria QueryCriteriaDateLiteralsOperatorsInt
BeforeEach(func() {
criteria = QueryCriteriaDateLiteralsOperatorsInt{
CreatedDate: 5,
ClosedDate: 10,
}
expectedClause = "CreatedDate > NEXT_N_DAYS:5 AND ClosedDate < NEXT_N_DAYS:10"
})
It("returns appropriate where clause", func() {
clause, err = MarshalWhereClause(criteria)
Expect(clause).To(Equal(expectedClause))
})
})
Context("When values for date literals are uint", func() {
var criteria QueryCriteriaDateLiteralsOperatorsUint
BeforeEach(func() {
criteria = QueryCriteriaDateLiteralsOperatorsUint{
CreatedDate: 5,
ClosedDate: 10,
}
expectedClause = "CreatedDate > NEXT_N_DAYS:5 AND ClosedDate < NEXT_N_DAYS:10"
})
It("returns appropriate where clause", func() {
clause, err = MarshalWhereClause(criteria)
Expect(clause).To(Equal(expectedClause))
})
})
Context("When values for date literals are wrong type", func() {
type QueryCriteriaDateLiteralsOperatorsWrong struct {
CreatedDate string `soql:"greaterNextNDaysOperator,fieldName=CreatedDate"`
}
var criteria QueryCriteriaDateLiteralsOperatorsWrong
BeforeEach(func() {
criteria = QueryCriteriaDateLiteralsOperatorsWrong{
CreatedDate: "5",
}
})
It("returns error", func() {
clause, err = MarshalWhereClause(criteria)
Expect(err).To(Equal(ErrInvalidTag))
})
})
Context("When values for date literals are pointers", func() {
var criteria QueryCriteriaDateLiteralsOperatorsPtr
Context("When there is only greaterNextNDaysOperator operator", func() {
BeforeEach(func() {
v := 5
criteria = QueryCriteriaDateLiteralsOperatorsPtr{
CreatedDate: &v,
}
expectedClause = "CreatedDate > NEXT_N_DAYS:5"
})
It("returns appropriate where clause", func() {
clause, err = MarshalWhereClause(criteria)
Expect(clause).To(Equal(expectedClause))
})
})
Context("When there is only greaterOrEqualNextNDaysOperator operator", func() {
BeforeEach(func() {
v := 5
criteria = QueryCriteriaDateLiteralsOperatorsPtr{
DeliveredDate: &v,
}
expectedClause = "DeliveredDate >= NEXT_N_DAYS:5"
})
It("returns appropriate where clause", func() {
clause, err = MarshalWhereClause(criteria)
Expect(clause).To(Equal(expectedClause))
})
})
Context("when there is only greaterLastNDaysOperator operator", func() {
BeforeEach(func() {
v0 := 5
criteria = QueryCriteriaDateLiteralsOperatorsPtr{
OtherDate: &v0,
}
expectedClause = "OtherDate = NEXT_N_DAYS:5"
})
It("returns appropriate where clause", func() {
clause, err = MarshalWhereClause(criteria)
Expect(clause).To(Equal(expectedClause))
})
})
Context("When there is only lessNextNDaysOperator operator", func() {
BeforeEach(func() {
v := 10
criteria = QueryCriteriaDateLiteralsOperatorsPtr{
ClosedDate: &v,
}
expectedClause = "ClosedDate < NEXT_N_DAYS:10"
})
It("returns appropriate where clause", func() {
clause, err = MarshalWhereClause(criteria)
Expect(clause).To(Equal(expectedClause))
})
})
Context("When there is only lessOrEqualNextNDaysOperator operator", func() {
BeforeEach(func() {
v := 10
criteria = QueryCriteriaDateLiteralsOperatorsPtr{
ScheduledDate: &v,
}
expectedClause = "ScheduledDate <= NEXT_N_DAYS:10"
})
It("returns appropriate where clause", func() {
clause, err = MarshalWhereClause(criteria)
Expect(clause).To(Equal(expectedClause))
})
})
Context("When there are all operators", func() {
BeforeEach(func() {
v0 := 5
v1 := 10
v2 := 15
v3 := 20
v4 := 25
criteria = QueryCriteriaDateLiteralsOperatorsPtr{
CreatedDate: &v0,
OtherDate: &v2,
ClosedDate: &v1,
ScheduledDate: &v3,
DeliveredDate: &v4,
}
expectedClause = "CreatedDate > NEXT_N_DAYS:5 AND OtherDate = NEXT_N_DAYS:15 AND ClosedDate < NEXT_N_DAYS:10 AND ScheduledDate <= NEXT_N_DAYS:20 AND DeliveredDate >= NEXT_N_DAYS:25"
})
It("returns appropriate where clause", func() {
clause, err = MarshalWhereClause(criteria)
Expect(clause).To(Equal(expectedClause))
})
})
})
Context("when last_n_days literals are present", func() {
var criteria QueryCriteriaDateLastNDaysLiteralsOperatorsPtr
Context("when there is only lessLastNDaysOperator operator", func() {
BeforeEach(func() {
v1 := 10
criteria = QueryCriteriaDateLastNDaysLiteralsOperatorsPtr{
ClosedDate: &v1,
}
expectedClause = "ClosedDate < LAST_N_DAYS:10"
})
It("returns appropriate where clause", func() {
clause, err = MarshalWhereClause(criteria)
Expect(clause).To(Equal(expectedClause))
})
Context("when there is only lessOrEqualLastNDaysOperator operator", func() {
BeforeEach(func() {
v1 := 10
criteria = QueryCriteriaDateLastNDaysLiteralsOperatorsPtr{
ScheduledDate: &v1,
}
expectedClause = "ScheduledDate <= LAST_N_DAYS:10"
})
It("returns appropriate where clause", func() {
clause, err = MarshalWhereClause(criteria)
Expect(clause).To(Equal(expectedClause))
})
})
Context("when there is only greaterLastNDaysOperator operator", func() {
BeforeEach(func() {
v0 := 5
criteria = QueryCriteriaDateLastNDaysLiteralsOperatorsPtr{
CreatedDate: &v0,
}
expectedClause = "CreatedDate > LAST_N_DAYS:5"
})
It("returns appropriate where clause", func() {
clause, err = MarshalWhereClause(criteria)
Expect(clause).To(Equal(expectedClause))
})
})
Context("when there is only greaterOrEqualLastNDaysOperator operator", func() {
BeforeEach(func() {
v0 := 5
criteria = QueryCriteriaDateLastNDaysLiteralsOperatorsPtr{
DeliveredDate: &v0,
}
expectedClause = "DeliveredDate >= LAST_N_DAYS:5"
})
It("returns appropriate where clause", func() {
clause, err = MarshalWhereClause(criteria)
Expect(clause).To(Equal(expectedClause))
})
})
Context("when there is only equalsLastNDaysOperator operator", func() {
BeforeEach(func() {
v0 := 5
criteria = QueryCriteriaDateLastNDaysLiteralsOperatorsPtr{
OtherDate: &v0,
}
expectedClause = "OtherDate = LAST_N_DAYS:5"
})
It("returns appropriate where clause", func() {
clause, err = MarshalWhereClause(criteria)
Expect(clause).To(Equal(expectedClause))
})
})
Context("when there are greaterLastNDaysOperator, lessLastNDaysOperator and equalsLastNDaysOperator operators", func() {
BeforeEach(func() {
v0 := 5
v1 := 10
v2 := 15
v3 := 20
v4 := 25
criteria = QueryCriteriaDateLastNDaysLiteralsOperatorsPtr{
CreatedDate: &v0,
OtherDate: &v2,
ClosedDate: &v1,
ScheduledDate: &v3,
DeliveredDate: &v4,
}
expectedClause = "CreatedDate > LAST_N_DAYS:5 AND OtherDate = LAST_N_DAYS:15 AND ClosedDate < LAST_N_DAYS:10 AND ScheduledDate <= LAST_N_DAYS:20 AND DeliveredDate >= LAST_N_DAYS:25"
})
It("returns appropriate where clause", func() {
clause, err = MarshalWhereClause(criteria)
Expect(clause).To(Equal(expectedClause))
})
})
})
})
Context("when all clauses are signed integer data types", func() {
var criteria QueryCriteriaWithIntegerTypes
BeforeEach(func() {
criteria = QueryCriteriaWithIntegerTypes{
NumOfCPUCores: 16,
PhysicalCPUCount: 4,
NumOfSuccessivePuppetRunFailures: -1,
NumOfCoolanLogFiles: 1024,
PvtTestFailCount: 9223372036854775807,
}
expectedClause = "Num_of_CPU_Cores__c = 16 AND Physical_CPU_Count__c = 4 AND Number_Of_Successive_Puppet_Run_Failures__c = -1 AND Num_Of_Coolan_Log_Files__c = 1024 AND Pvt_Test_Fail_Count__c = 9223372036854775807"
})
It("returns properly formed clause joined by AND clause", func() {
clause, err = MarshalWhereClause(criteria)
Expect(err).ToNot(HaveOccurred())
Expect(clause).To(Equal(expectedClause))
})
})
Context("when all clauses are unsigned integer data types", func() {
var criteria QueryCriteriaWithUnsignedIntegerTypes
BeforeEach(func() {
criteria = QueryCriteriaWithUnsignedIntegerTypes{
NumOfCPUCores: 16,
PhysicalCPUCount: 4,
NumOfSuccessivePuppetRunFailures: 0,
NumOfCoolanLogFiles: 1024,
PvtTestFailCount: 9223372036854775807,
}
expectedClause = "Num_of_CPU_Cores__c = 16 AND Physical_CPU_Count__c = 4 AND Number_Of_Successive_Puppet_Run_Failures__c = 0 AND Num_Of_Coolan_Log_Files__c = 1024 AND Pvt_Test_Fail_Count__c = 9223372036854775807"
})
It("returns properly formed clause joined by AND clause", func() {
clause, err = MarshalWhereClause(criteria)
Expect(err).ToNot(HaveOccurred())
Expect(clause).To(Equal(expectedClause))
})
})
Context("when all clauses are float data types", func() {
var criteria QueryCriteriaWithFloatTypes
BeforeEach(func() {
criteria = QueryCriteriaWithFloatTypes{
NumOfCPUCores: 16.00000000,
PhysicalCPUCount: -4.12345678,
}
expectedClause = "Num_of_CPU_Cores__c = 16 AND Physical_CPU_Count__c = -4.12345678"
})
It("returns properly formed clause joined by AND clause", func() {
clause, err = MarshalWhereClause(criteria)
Expect(err).ToNot(HaveOccurred())
Expect(clause).To(Equal(expectedClause))
})
})
Context("when all clauses are *float data types", func() {
var criteria QueryCriteriaWithFloatPtrTypes
BeforeEach(func() {
numCores := 16.0
criteria = QueryCriteriaWithFloatPtrTypes{
NumOfCPUCores: &numCores,
}
expectedClause = "Num_of_CPU_Cores__c = 16"
})
It("returns properly formed clause joined by skipping nil values", func() {
clause, err = MarshalWhereClause(criteria)
Expect(err).ToNot(HaveOccurred())
Expect(clause).To(Equal(expectedClause))
})
})
Context("when all clauses are boolean data types", func() {
var criteria QueryCriteriaWithBooleanType
BeforeEach(func() {
criteria = QueryCriteriaWithBooleanType{
NUMAEnabled: true,
DisableAlerts: false,
}
expectedClause = "NUMA_Enabled__c = true AND Disable_Alerts__c = false"
})
It("returns properly formed clause joined by AND clause", func() {
clause, err = MarshalWhereClause(criteria)
Expect(err).ToNot(HaveOccurred())
Expect(clause).To(Equal(expectedClause))
})
})
Context("when data type is boolean pointer", func() {
var criteria QueryCriteriaWithBooleanPtrType
BeforeEach(func() {
numEnabled := true
criteria = QueryCriteriaWithBooleanPtrType{
NUMAEnabled: &numEnabled,
}
expectedClause = "NUMA_Enabled__c = true"
})
It("returns properly formed clause by skipping nil values", func() {
clause, err = MarshalWhereClause(criteria)
Expect(err).ToNot(HaveOccurred())
Expect(clause).To(Equal(expectedClause))
})
})
Context("when some clauses have soql tags and don't", func() {
var criteria QueryCriteriaWithNoSoqlTag
BeforeEach(func() {
criteria = QueryCriteriaWithNoSoqlTag{
NUMAEnabled: true,
DisableAlerts: false,
}
expectedClause = "NUMA_Enabled__c = true"
})
It("returns properly formed clause without error§q", func() {
clause, err = MarshalWhereClause(criteria)
Expect(err).ToNot(HaveOccurred())
Expect(clause).To(Equal(expectedClause))
})
})
Context("when all clauses are date time data types", func() {
var criteria QueryCriteriaWithDateTimeType
var currentTime time.Time
BeforeEach(func() {
currentTime = time.Now()
criteria = QueryCriteriaWithDateTimeType{
CreatedDate: currentTime,
}
expectedClause = "CreatedDate = " + currentTime.Format(DateTimeFormat)
})
It("returns properly formed clause", func() {
clause, err = MarshalWhereClause(criteria)
Expect(err).ToNot(HaveOccurred())
Expect(clause).To(Equal(expectedClause))
})
})
Context("when all clauses are pointers to date time data types", func() {
var criteria QueryCriteriaWithPtrDateTimeType
var currentTime time.Time
BeforeEach(func() {
currentTime = time.Now()
criteria = QueryCriteriaWithPtrDateTimeType{
CreatedDate: ¤tTime,
ResolvedDate: nil,
}
expectedClause = "CreatedDate = " + currentTime.Format(DateTimeFormat)
})
It("returns properly formed clause", func() {
clause, err = MarshalWhereClause(criteria)
Expect(err).ToNot(HaveOccurred())
Expect(clause).To(Equal(expectedClause))
})
})
Context("when all clauses are mixed data types and operators", func() {
var criteria QueryCriteriaWithMixedDataTypesAndOperators
var currentTime time.Time
BeforeEach(func() {
currentTime = time.Now()
numHardDrives := 2
criteria = QueryCriteriaWithMixedDataTypesAndOperators{
BIOSType: "98.7.654a",
NumOfCPUCores: 32,
NUMAEnabled: true,
PvtTestFailCount: 256,
PhysicalCPUCount: 4,
CreatedDate: currentTime,
UpdatedDate: ¤tTime,
DisableAlerts: false,
AllocationLatency: 10.5,
MajorOSVersion: "20",
NumOfSuccessivePuppetRunFailures: 0,
LastRestart: currentTime,
ClosedDate: 5,
NumHardDrives: &numHardDrives,
}
expectedClause = "BIOS_Type__c = '98.7.654a' AND Num_of_CPU_Cores__c > 32 AND NUMA_Enabled__c = true AND Pvt_Test_Fail_Count__c <= 256 AND Physical_CPU_Count__c >= 4 AND CreatedDate = " + currentTime.Format(TestDateFormat) + " AND UpdatedDate = " + currentTime.Format(TestDateFormat) + " AND Disable_Alerts__c = false AND Allocation_Latency__c < 10.5 AND Major_OS_Version__c = '20' AND Number_Of_Successive_Puppet_Run_Failures__c = 0 AND Last_Restart__c > " + currentTime.Format(DateTimeFormat) + " AND NumHardDrives__c = 2 AND ClosedDate > NEXT_N_DAYS:5"
})
It("returns properly formed clause", func() {
clause, err = MarshalWhereClause(criteria)
Expect(err).ToNot(HaveOccurred())
Expect(clause).To(Equal(expectedClause))
})
})
Context("when clauses contains gt, gte, lt and lte operators", func() {
var criteria QueryCriteriaNumericComparisonOperators
BeforeEach(func() {
criteria = QueryCriteriaNumericComparisonOperators{
NumOfCPUCores: 16,
PhysicalCPUCount: 4,
NumOfSuccessivePuppetRunFailures: 0,
NumOfCoolanLogFiles: 1024,
}
expectedClause = "Num_of_CPU_Cores__c > 16 AND Physical_CPU_Count__c < 4 AND Number_Of_Successive_Puppet_Run_Failures__c >= 0 AND Num_Of_Coolan_Log_Files__c <= 1024"
})
It("returns properly formed clause", func() {
clause, err = MarshalWhereClause(criteria)
Expect(err).ToNot(HaveOccurred())
Expect(clause).To(Equal(expectedClause))
})
})
Context("when no fieldName parameter is specified in tag", func() {
var defaultFieldNameCriteria DefaultFieldNameQueryCriteria
BeforeEach(func() {
defaultFieldNameCriteria = DefaultFieldNameQueryCriteria{
IncludeNamePattern: []string{"-db", "-dbmgmt"},
Role: []string{"foo", "bar"},
}
expectedClause = "(Host_Name__c LIKE '%-db%' OR Host_Name__c LIKE '%-dbmgmt%') AND Role IN ('foo','bar')"
})
It("returns properly formed clause joined by AND clause", func() {
clause, err = MarshalWhereClause(defaultFieldNameCriteria)
Expect(err).ToNot(HaveOccurred())
Expect(clause).To(Equal(expectedClause))
})
})
Context("when tag is invalid", func() {
Context("when struct has invalid tag key", func() {
type InvalidCriteriaStruct struct {
SomePattern []string `soql:"likeOperator,fieldName=Some_Pattern__c"`
SomeOtherPattern string `soql:"invalidClause,fieldName=Some_Other_Field"`
}
It("returns ErrInvalidTag error", func() {
str, err := MarshalWhereClause(InvalidCriteriaStruct{})
Expect(err).To(Equal(ErrInvalidTag))
Expect(str).To(BeEmpty())
})
})
Context("when struct has missing fieldName", func() {
type MissingFieldName struct {
SomePattern []string `soql:"likeOperator,fieldName=Some_Pattern__c"`
SomeOtherPattern string `soql:"equalsOperator,fieldName="`
}
It("returns ErrInvalidTag error", func() {
str, err := MarshalWhereClause(MissingFieldName{
SomePattern: []string{"test"},
SomeOtherPattern: "foo",
})
Expect(err).To(Equal(ErrInvalidTag))
Expect(str).To(BeEmpty())
})
})
Context("when struct has invalid type for likeOperator", func() {
type QueryCriteriaWithInvalidLikeOperator struct {
IncludeNamePattern []bool `soql:"likeOperator,fieldName=Host_Name__c"`
}
It("returns error", func() {
_, err := MarshalWhereClause(QueryCriteriaWithInvalidLikeOperator{})
Expect(err).To(Equal(ErrInvalidTag))
})
})
Context("when struct has invalid type for likeOperator", func() {
type QueryCriteriaWithInvalidNotLikeOperator struct {
ExcludeNamePattern []bool `soql:"notLikeOperator,fieldName=Host_Name__c"`
}
It("returns error", func() {
_, err := MarshalWhereClause(QueryCriteriaWithInvalidNotLikeOperator{})
Expect(err).To(Equal(ErrInvalidTag))
})
})
Context("when struct has invalid type for inOperator", func() {
type QueryCriteriaWithInvalidInOperator struct {
Roles int `soql:"inOperator,fieldName=Role__c"`
}
It("returns error", func() {
_, err := MarshalWhereClause(QueryCriteriaWithInvalidInOperator{})
Expect(err).To(Equal(ErrInvalidTag))
})
})
Context("when struct has invalid type for comparison operators", func() {
type QueryCriteriaWithInvalidComparisonOperator struct {
Roles []int `soql:"lessThanOperator,fieldName=Role__c"`
}
It("returns error", func() {
_, err := MarshalWhereClause(QueryCriteriaWithInvalidComparisonOperator{})
Expect(err).To(Equal(ErrInvalidTag))
})
})
})
})
Describe("MarshalOrderByClause", func() {
Context("when valid Order slice passed as argument", func() {
Context("when an empty Order by slice is passed", func() {
It("returns empty order by clause", func() {
clause, err := MarshalOrderByClause([]Order{}, struct {
NumOfCPUCores int `soql:"selectColumn,fieldName=Num_of_CPU_Cores__c"`
}{})
Expect(err).ToNot(HaveOccurred())
Expect(clause).To(BeEmpty())
})
})
Context("when an Order slice with single order by column desc is passed", func() {
It("returns a column desc partial clause", func() {
desc := Order{Field: "NumOfCPUCores", IsDesc: true}
clause, err := MarshalOrderByClause([]Order{desc}, struct {
NumOfCPUCores int `soql:"selectColumn,fieldName=Num_of_CPU_Cores__c"`
}{})
Expect(err).ToNot(HaveOccurred())
Expect(clause).To(Equal("Num_of_CPU_Cores__c DESC"))
})
})
Context("when an Order slice with single order by column asc is passed", func() {
It("returns a column asc partial clause", func() {
asc := Order{Field: "NumOfCPUCores", IsDesc: false}
clause, err := MarshalOrderByClause([]Order{asc}, struct {
NumOfCPUCores int `soql:"selectColumn,fieldName=Num_of_CPU_Cores__c"`
}{})
Expect(err).ToNot(HaveOccurred())
Expect(clause).To(Equal("Num_of_CPU_Cores__c ASC"))
})
})
Context("when an Order slice with multiple order by column ASC is passed", func() {
It("returns multiple columns asc partial clause", func() {
col1 := Order{Field: "MajorOSVersion", IsDesc: false}
col2 := Order{Field: "NumOfCPUCores", IsDesc: false}
col3 := Order{Field: "PhysicalCPUCount", IsDesc: false}
clause, err := MarshalOrderByClause([]Order{col1, col2, col3}, struct {
MajorOSVersion string `soql:"selectColumn,fieldName=Major_OS_Version__c"`
NumOfCPUCores int `soql:"selectColumn,fieldName=Num_of_CPU_Cores__c"`
PhysicalCPUCount uint8 `soql:"selectColumn,fieldName=Physical_CPU_Count__c"`
LastRestart time.Time `soql:"selectColumn,fieldName=Last_Restart__c"`
}{})
Expect(err).ToNot(HaveOccurred())
Expect(clause).To(Equal("Major_OS_Version__c ASC,Num_of_CPU_Cores__c ASC,Physical_CPU_Count__c ASC"))
})
})
Context("when an Order slice with multiple order by column DESC is passed", func() {
It("returns multiple columns asc partial clause", func() {
col1 := Order{Field: "MajorOSVersion", IsDesc: true}
col2 := Order{Field: "NumOfCPUCores", IsDesc: true}