-
Notifications
You must be signed in to change notification settings - Fork 18
/
SoqlBuilderTest.cls
419 lines (373 loc) · 15.9 KB
/
SoqlBuilderTest.cls
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
@IsTest
private class SoqlBuilderTest {
private static testmethod void testLimit(){
SoqlUtils.assertEquals(
'SELECT id FROM account LIMIT 1000',
new SoqlBuilder().selectx('id').fromx('account').limitx(1000).toSoql());
}
private static testmethod void testNoObjectName(){
Boolean exceptionCaught = false;
try{
new SoqlBuilder().toSoql();
}catch(IllegalStateException e){
exceptionCaught = true;
}
System.assert(exceptionCaught == true,'IllegalStateException not thrown');
}
private static testmethod void testBadField1(){
Boolean exceptionCaught = false;
try{
String aNull = null;
new SoqlBuilder().selectx(aNull);
}catch(IllegalArgumentException e){
exceptionCaught = true;
}
System.assert(exceptionCaught == true,'IllegalArgumentException not thrown');
}
private static testmethod void testBadField2(){
Boolean exceptionCaught = false;
try{
List<Object> anObjectList = new List<Object>{new DecimalRange(0,1)};
new SoqlBuilder().selectx(anObjectList);
}catch(IllegalArgumentException e){
exceptionCaught = true;
}
System.assert(exceptionCaught == true,'IllegalArgumentException not thrown');
}
private static testmethod void testBasicSelect(){
SoqlUtils.assertEquals(
'SELECT id,name FROM account WHERE name like \'%acme%\'',
new SoqlBuilder()
.selectx(new List<String>{'id','name'})
.fromx('account')
.wherex(new FieldCondition('name',Operator.LIKEX,'%acme%'))
.toSoql());
}
private static testmethod void testOption_wildcardStringsInLikeOperators(){
SoqlUtils.assertEquals(
'SELECT id FROM account WHERE name like \'%acme%\'',
new SoqlBuilder()
.fromx('account')
.wherex(new FieldCondition('name',Operator.LIKEX,'acme'))
.toSoql(new SoqlOptions().wildcardStringsInLikeOperators()));
}
private static testmethod void testOption_wildcardStringsInLikeOperatorsAlreadyWildcarded(){
SoqlUtils.assertEquals(
'SELECT id FROM account WHERE name like \'%acme%\'',
new SoqlBuilder()
.fromx('account')
.wherex(new FieldCondition('name',Operator.LIKEX,'%acme%'))
.toSoql(new SoqlOptions().wildcardStringsInLikeOperators()));
}
private static testmethod void testOption_doNotWildcardStringsInLikeOperators(){
SoqlUtils.assertEquals(
'SELECT id FROM account WHERE name like \'acme\'',
new SoqlBuilder()
.fromx('account')
.wherex(new FieldCondition('name',Operator.LIKEX,'acme'))
.toSoql(new SoqlOptions().doNotWildcardStringsInLikeOperators()));
}
private static testmethod void testOption_escapeSingleQuotes(){
SoqlUtils.assertEquals(
'SELECT id FROM account WHERE name like \'Bill\\\'s Chop Shop\'',
new SoqlBuilder()
.fromx('account')
.wherex(new FieldCondition('name',Operator.LIKEX,'Bill\'s Chop Shop'))
.toSoql(new SoqlOptions().escapeSingleQuotes()));
}
private static testmethod void testOption_doNotEscapeSingleQuotes(){
SoqlUtils.assertEquals(
'SELECT id FROM account WHERE name like \'Bill\'s Chop Shop\'',
new SoqlBuilder()
.fromx('account')
.wherex(new FieldCondition('name',Operator.LIKEX,'Bill\'s Chop Shop'))
.toSoql(new SoqlOptions().doNotEscapeSingleQuotes()));
}
private static testmethod void testNestedConditions(){
SoqlUtils.assertEquals(
'SELECT id,name FROM account WHERE ((name like \'%acme%\' OR description = \'yo\') AND ispartner = true) ORDER BY name DESC LIMIT 500',
new SoqlBuilder()
.selectx(new List<String>{'id','name'})
.fromx('account')
.wherex(
new AndCondition()
.add(
new OrCondition()
.add(new FieldCondition('name',Operator.LIKEX,'%acme%'))
.add(new FieldCondition('description',Operator.EQUALS,'yo'))
)
.add(new FieldCondition('ispartner',Operator.EQUALS,true))
).orderByx(new OrderBy('name').descending())
.limitx(500)
.toSoql());
}
private static testmethod void testOrderBy1(){
SoqlUtils.assertEquals(
'SELECT id FROM account ORDER BY name',
new SoqlBuilder()
.fromx('account')
.orderByx(new List<OrderBy>{new OrderBy('name')})
.toSoql());
}
private static testmethod void testOrderBy2(){
SoqlUtils.assertEquals(
'SELECT id FROM account ORDER BY name ASC NULLS LAST, rating DESC, industry DESC NULLS FIRST',
new SoqlBuilder()
.fromx('account')
.orderByx(new List<OrderBy>{
new OrderBy('name').ascending().nullsLast()
,new OrderBy('rating').descending()
,new OrderBy('industry').descending().nullsFirst()})
.toSoql());
}
private static testmethod void testANullOrderBy(){
SoqlUtils.assertEquals(
'SELECT id FROM account ORDER BY name',
new SoqlBuilder()
.fromx('account')
.orderByx(new List<OrderBy>{
new OrderBy('name')
,null})
.toSoql());
}
private static testmethod void testGroupBy(){
SoqlUtils.assertEquals(
'SELECT id FROM account GROUP BY name',
new SoqlBuilder()
.fromx('account')
.groupByx('name')
.toSoql());
}
private static testmethod void testParentToChildQuery1(){
SoqlUtils.assertEquals(
'SELECT ID, Name, toLabel(Rating), (SELECT FirstName, LastName FROM Contacts) FROM Account',
new SoqlBuilder()
.selectx('ID')
.selectx('Name')
.selectx(new Field('Rating').toLabelx())
.selectx(
new SoqlBuilder()
.selectx('FirstName')
.selectx('LastName')
.fromx('Contacts'))
.fromx('Account')
.toSoql());
}
private static testmethod void testParentToChildQuery2(){
SoqlUtils.assertEquals(
'SELECT ID, Name, toLabel(Rating), (SELECT FirstName, LastName FROM Contacts) FROM Account',
new SoqlBuilder()
.selectx(new List<Object>
{
'ID'
,'Name'
,new Field('Rating').toLabelx()
,new SoqlBuilder()
.selectx('FirstName')
.selectx('LastName')
.fromx('Contacts')
}
)
.fromx('Account')
.toSoql());
}
private static testmethod void testSelectCount1(){
SoqlUtils.assertEquals(
'SELECT count() from Contact c, c.Account a WHERE a.name = \'MyriadPubs\'',
new SoqlBuilder()
.selectCount()
.fromx('Contact c, c.Account a')
.wherex(new FieldCondition('a.name','MyriadPubs'))
.toSoql());
}
private static testmethod void testSelectCount2(){
SoqlUtils.assertEquals(
'SELECT count() FROM Account WHERE Name LIKE \'a%\'',
new SoqlBuilder()
.selectCount()
.fromx('Account')
.wherex(new FieldCondition('Name',Operator.LIKEX,'a%'))
.toSoql());
}
private static testmethod void testSelectCountWithArg(){
SoqlUtils.assertEquals(
'SELECT count(Id) FROM Account WHERE Name LIKE \'a%\'',
new SoqlBuilder()
.selectCount('Id')
.fromx('Account')
.wherex(new FieldCondition('Name',Operator.LIKEX,'a%'))
.toSoql());
}
private static testmethod void testSelectAverage(){
SoqlUtils.assertEquals(
'SELECT AVG(Amount) FROM Account WHERE Name LIKE \'a%\'',
new SoqlBuilder()
.selectAveragex('Amount')
.fromx('Account')
.wherex(new FieldCondition('Name',Operator.LIKEX,'a%'))
.toSoql());
}
private static testmethod void testSelectMax(){
SoqlUtils.assertEquals(
'SELECT Max(Amount) FROM Account WHERE Name LIKE \'a%\'',
new SoqlBuilder()
.selectMaxx('Amount')
.fromx('Account')
.wherex(new FieldCondition('Name',Operator.LIKEX,'a%'))
.toSoql());
}
private static testmethod void testSelectMin(){
SoqlUtils.assertEquals(
'SELECT Min(Amount) FROM Account WHERE Name LIKE \'a%\'',
new SoqlBuilder()
.selectMinx('Amount')
.fromx('Account')
.wherex(new FieldCondition('Name',Operator.LIKEX,'a%'))
.toSoql());
}
private static testmethod void testSelectSumx(){
SoqlUtils.assertEquals(
'SELECT Sum(Amount) FROM Account WHERE Name LIKE \'a%\'',
new SoqlBuilder()
.selectSumx('Amount')
.fromx('Account')
.wherex(new FieldCondition('Name',Operator.LIKEX,'a%'))
.toSoql());
}
private static testmethod void testSelectNullCondition(){
SoqlUtils.assertEquals(
'SELECT Id FROM Case WHERE Contact.Lastname = null',
new SoqlBuilder()
.fromx('Case')
.wherex(new FieldCondition('Contact.Lastname',null))
.toSoql());
}
private static testmethod void testIncludes(){
SoqlUtils.assertEquals(
'SELECT Id, MSP1__c from CustObj__c WHERE MSP1__c INCLUDES (\'AAA;BBB\',\'CCC\')',
new SoqlBuilder()
.selectx(new List<String>{'id','MSP1__c'})
.fromx('CustObj__c')
.wherex(new SetCondition('MSP1__c').includes(new List<String>{'AAA;BBB','CCC'}))
.toSoql());
}
private static testmethod void testExcludes(){
SoqlUtils.assertEquals(
'SELECT Id, MSP1__c from CustObj__c WHERE MSP1__c EXCLUDES (\'AAA;BBB\',\'CCC\')',
new SoqlBuilder()
.selectx(new List<String>{'id','MSP1__c'})
.fromx('CustObj__c')
.wherex(new SetCondition('MSP1__c').excludes(new List<String>{'AAA;BBB','CCC'}))
.toSoql());
}
private static testmethod void testIn(){
SoqlUtils.assertEquals(
'SELECT id FROM ACCOUNT WHERE BillingState IN (\'California\',\'New York\')',
new SoqlBuilder()
.fromx('ACCOUNT')
.wherex(new SetCondition('BillingState').inx(new List<String>{'California','New York'}))
.toSoql());
}
private static testmethod void testNotIn(){
SoqlUtils.assertEquals(
'SELECT id FROM ACCOUNT WHERE BillingState NOT IN (\'California\',\'New York\')',
new SoqlBuilder()
.fromx('ACCOUNT')
.wherex(new SetCondition('BillingState').notIn(new List<String>{'California','New York'}))
.toSoql());
}
private static testmethod void testDateFormula_NEXT_N_FISCAL_QUARTERS(){
SoqlUtils.assertEquals(
'SELECT Id FROM Account WHERE CreatedDate < NEXT_N_FISCAL_QUARTERS:6',
new SoqlBuilder()
.fromx('Account')
.wherex(new FieldCondition('CreatedDate', Operator.LESS_THAN, new DateFormula().next(6,UnitOfTime.FiscalQuarter)))
.toSoql());
}
private static testmethod void testDateFormula_TOMORROW(){
SoqlUtils.assertEquals(
'SELECT Id FROM Opportunity WHERE CloseDate = TOMORROW',
new SoqlBuilder()
.fromx('Opportunity')
.wherex(new FieldCondition('CloseDate', new DateFormula().tomorrowx()))
.toSoql());
}
private static testmethod void test_EQUALS_1(){
SoqlUtils.assertEquals(
'SELECT name,employees FROM account WHERE employees = 10',
new SoqlBuilder()
.selectx('name')
.selectx(new Set<Object>{'employees'})
.fromx('account')
.wherex(new FieldCondition('employees', 10))
.toSoql());
}
private static testmethod void test_EQUALS_2(){
SoqlUtils.assertEquals(
'SELECT name,employees FROM account WHERE employees = 10',
new SoqlBuilder()
.selectx('name')
.selectx('employees')
.fromx('account')
.wherex(new FieldCondition('employees').equals(10))
.toSoql());
}
private static testmethod void test_EQUALS_3(){
SoqlUtils.assertEquals(
'SELECT name,employees FROM account WHERE employees = 10',
new SoqlBuilder()
.selectx(new Set<Object>{'name','employees'})
.fromx('account')
.wherex(new FieldCondition().field('employees').equals(10))
.toSoql());
}
private static testmethod void test_INX_1(){
SoqlUtils.assertEquals(
'SELECT id,industry FROM account WHERE industry in (\'Agriculture\',\'Apparel\')',
new SoqlBuilder()
.selectx(new List<Object>{'industry','id'})
.fromx('account')
.wherex(new SetCondition('industry', Operator.INX, new List<Object>{'Agriculture','Apparel'}))
.toSoql());
}
private static testmethod void test_INX_2(){
SoqlUtils.assertEquals(
'SELECT id,industry FROM account WHERE industry in (\'Agriculture\',\'Apparel\')',
new SoqlBuilder()
.selectx(new List<Object>{'industry','id'})
.fromx('account')
.wherex(new SetCondition('industry').inx(new List<Object>{'Agriculture','Apparel'}))
.toSoql());
}
private static testmethod void test_INX_3(){
SoqlUtils.assertEquals(
'SELECT id,industry FROM account WHERE industry in (\'Agriculture\',\'Apparel\')',
new SoqlBuilder()
.selectx(new List<Object>{'industry','id'})
.fromx('account')
.wherex(new SetCondition().field('industry').inx(new List<Object>{'Agriculture','Apparel'}))
.toSoql());
}
private static testmethod void testBasicSemiJoin(){
System.assertEquals(
'SELECT id FROM Account WHERE ID IN '
+'(SELECT AccountId FROM Opportunity WHERE StageName = \'Closed Lost\')',
new SoqlBuilder()
.fromx('Account')
.wherex(new SetCondition('ID').inx(
new SoqlBuilder()
.selectx('AccountId')
.fromx('Opportunity')
.wherex(new FieldCondition('StageName','Closed Lost'))))
.toSoql());
}
private static testmethod void testDuplicateFields(){
System.assertEquals(
'SELECT name FROM Account',
new SoqlBuilder()
.selectx('name')
.selectx(new Field('name'))
.fromx('Account')
.toSoql());
}
}