-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcompletion.test.ts
More file actions
871 lines (800 loc) · 29.1 KB
/
Copy pathcompletion.test.ts
File metadata and controls
871 lines (800 loc) · 29.1 KB
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
/*
* Copyright (c) 2021, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { CompletionItem, CompletionItemKind, InsertTextFormat } from 'vscode-languageserver';
import { completionsFor, SoqlItemContext } from './completion';
import { soqlDateRangeLiterals, soqlParametricDateRangeLiterals } from './completion/soql-functions';
const SELECT_SNIPPET = {
kind: CompletionItemKind.Snippet,
label: 'SELECT ... FROM ...',
insertText: 'SELECT $2 FROM $1',
insertTextFormat: InsertTextFormat.Snippet
};
const INNER_SELECT_SNIPPET = {
kind: CompletionItemKind.Snippet,
label: '(SELECT ... FROM ...)',
insertText: '(SELECT $2 FROM $1)',
insertTextFormat: InsertTextFormat.Snippet
};
const typesForLTGTOperators = [
'anyType',
'complexvalue',
'currency',
'date',
'datetime',
'double',
'int',
'percent',
'string',
'textarea',
'time',
'url'
];
const expectedSoqlContextByKeyword: {
[key: string]: Partial<SoqlItemContext>;
} = {
'<': { onlyTypes: typesForLTGTOperators },
'<=': { onlyTypes: typesForLTGTOperators },
'>': { onlyTypes: typesForLTGTOperators },
'>=': { onlyTypes: typesForLTGTOperators },
'INCLUDES (': { onlyTypes: ['multipicklist'] },
'EXCLUDES (': { onlyTypes: ['multipicklist'] },
LIKE: { onlyTypes: ['string', 'textarea', 'time'] }
};
function newLiteralItem(
soqlItemContext: SoqlItemContext,
kind: CompletionItemKind,
label: string,
extraOptions: Partial<CompletionItem> = {}
): CompletionItem {
return {
label,
kind,
...extraOptions,
data: {
soqlContext: soqlItemContext
}
};
}
function expectedItemsForLiterals(soqlContext: SoqlItemContext, nillableOperator: boolean): CompletionItem[] {
const items: CompletionItem[] = [
newLiteralItem(soqlContext, CompletionItemKind.Constant, '__LITERAL_VALUES_FOR_FIELD'),
newLiteralItem({ ...soqlContext, ...{ onlyTypes: ['boolean'] } }, CompletionItemKind.Value, 'TRUE'),
newLiteralItem({ ...soqlContext, ...{ onlyTypes: ['boolean'] } }, CompletionItemKind.Value, 'FALSE'),
newLiteralItem({ ...soqlContext, ...{ onlyTypes: ['int'] } }, CompletionItemKind.Snippet, 'nnn', {
insertText: '${1:123}',
insertTextFormat: InsertTextFormat.Snippet
}),
newLiteralItem({ ...soqlContext, ...{ onlyTypes: ['double'] } }, CompletionItemKind.Snippet, 'nnn.nnn', {
insertText: '${1:123.456}',
insertTextFormat: InsertTextFormat.Snippet
}),
newLiteralItem({ ...soqlContext, ...{ onlyTypes: ['currency'] } }, CompletionItemKind.Snippet, 'ISOCODEnnn.nn', {
insertText: '${1|USD,EUR,JPY,CNY,CHF|}${2:999.99}',
insertTextFormat: InsertTextFormat.Snippet
}),
newLiteralItem({ ...soqlContext, ...{ onlyTypes: ['string'] } }, CompletionItemKind.Snippet, 'abc123', {
insertText: "'${1:abc123}'",
insertTextFormat: InsertTextFormat.Snippet
}),
newLiteralItem({ ...soqlContext, ...{ onlyTypes: ['date'] } }, CompletionItemKind.Snippet, 'YYYY-MM-DD', {
insertText: '${1:${CURRENT_YEAR}}-${2:${CURRENT_MONTH}}-${3:${CURRENT_DATE}}$0',
insertTextFormat: InsertTextFormat.Snippet,
preselect: true,
sortText: ' YYYY-MM-DD'
}),
newLiteralItem(
{ ...soqlContext, ...{ onlyTypes: ['datetime'] } },
CompletionItemKind.Snippet,
'YYYY-MM-DDThh:mm:ssZ',
{
insertText:
'${1:${CURRENT_YEAR}}-${2:${CURRENT_MONTH}}-${3:${CURRENT_DATE}}T${4:${CURRENT_HOUR}}:${5:${CURRENT_MINUTE}}:${6:${CURRENT_SECOND}}Z$0',
insertTextFormat: InsertTextFormat.Snippet,
preselect: true,
sortText: ' YYYY-MM-DDThh:mm:ssZ'
}
),
...soqlDateRangeLiterals.map(k =>
newLiteralItem({ ...soqlContext, ...{ onlyTypes: ['date', 'datetime'] } }, CompletionItemKind.Value, k)
),
...soqlParametricDateRangeLiterals.map(k =>
newLiteralItem({ ...soqlContext, ...{ onlyTypes: ['date', 'datetime'] } }, CompletionItemKind.Snippet, k, {
insertText: k.replace(':n', ':${1:nn}') + '$0',
insertTextFormat: InsertTextFormat.Snippet
})
)
];
if (nillableOperator) {
items.push(newLiteralItem({ ...soqlContext, ...{ onlyNillable: true } }, CompletionItemKind.Keyword, 'NULL'));
}
return items;
}
function newKeywordItem(word: string, extraOptions: Partial<CompletionItem> = {}): CompletionItem {
return Object.assign(
{
kind: CompletionItemKind.Keyword,
label: word
},
extraOptions
);
}
function newKeywordItems(...words: string[]): CompletionItem[] {
return words.map(s => ({
kind: CompletionItemKind.Keyword,
label: s
}));
}
function newKeywordItemsWithContext(sobjectName: string, fieldName: string, words: string[]): CompletionItem[] {
return words.map(s => ({
kind: CompletionItemKind.Keyword,
label: s,
data: {
soqlContext: {
sobjectName,
fieldName,
...expectedSoqlContextByKeyword[s]
}
}
}));
}
function newFunctionCallItem(name: string, soqlItemContext?: SoqlItemContext): CompletionItem {
return Object.assign(
{
kind: CompletionItemKind.Function,
label: name + '(...)',
insertText: name + '($1)',
insertTextFormat: InsertTextFormat.Snippet
},
soqlItemContext ? { data: { soqlContext: soqlItemContext } } : {}
);
}
const expectedSObjectCompletions: CompletionItem[] = [
{
kind: CompletionItemKind.Class,
label: '__SOBJECTS_PLACEHOLDER'
}
];
function relationshipsItem(sobjectName: string): CompletionItem {
return {
kind: CompletionItemKind.Class,
label: '__RELATIONSHIPS_PLACEHOLDER',
data: {
soqlContext: {
sobjectName
}
}
};
}
describe('Code Completion on invalid cursor position', () => {
it('Should return empty if cursor is on non-exitent line', () => {
expect(completionsFor('SELECT id FROM Foo', 2, 5)).toHaveLength(0);
});
});
describe('Code Completion on SELECT ...', () => {
validateCompletionsFor('|', [newKeywordItem('SELECT'), SELECT_SNIPPET]);
validateCompletionsFor('SELE|', [...newKeywordItems('SELECT'), SELECT_SNIPPET]);
validateCompletionsFor('| FROM', newKeywordItems('SELECT'));
validateCompletionsFor('SELECT|', []);
// "COUNT()" can only be used on its own, unlike "COUNT(fieldName)".
// So we expect it on completions only right after "SELECT"
validateCompletionsFor('SELECT |', [newKeywordItem('COUNT()'), ...sobjectsFieldsFor('Object')]);
validateCompletionsFor('SELECT\n|', [newKeywordItem('COUNT()'), ...sobjectsFieldsFor('Object')]);
validateCompletionsFor('SELECT\n |', [newKeywordItem('COUNT()'), ...sobjectsFieldsFor('Object')]);
validateCompletionsFor('SELECT\n\n |\n\n', [newKeywordItem('COUNT()'), ...sobjectsFieldsFor('Object')]);
validateCompletionsFor('SELECT id, |', sobjectsFieldsFor('Object'));
validateCompletionsFor('SELECT id, boo,|', sobjectsFieldsFor('Object'));
validateCompletionsFor('SELECT id|', [newKeywordItem('COUNT()'), ...sobjectsFieldsFor('Object')]);
validateCompletionsFor('SELECT id |', newKeywordItems('FROM'));
validateCompletionsFor('SELECT COUNT() |', newKeywordItems('FROM'));
validateCompletionsFor('SELECT COUNT(), |', []);
// Inside Function expression:
validateCompletionsFor('SELECT OwnerId, COUNT(|)', [
{
kind: CompletionItemKind.Field,
label: '__SOBJECT_FIELDS_PLACEHOLDER',
data: {
soqlContext: {
sobjectName: 'Object',
onlyAggregatable: true,
onlyTypes: [
'date',
'datetime',
'double',
'int',
'string',
'combobox',
'currency',
'DataCategoryGroupReference',
'email',
'id',
'masterrecord',
'percent',
'phone',
'picklist',
'reference',
'textarea',
'url'
]
}
}
}
]);
});
describe('Code Completion on select fields: SELECT ... FROM XYZ', () => {
// "COUNT()" can only be used on its own, unlike "COUNT(fieldName)".
// So we expect it on completions only right after "SELECT"
validateCompletionsFor('SELECT | FROM Object', [newKeywordItem('COUNT()'), ...sobjectsFieldsFor('Object')]);
validateCompletionsFor('SELECT | FROM Foo', [newKeywordItem('COUNT()'), ...sobjectsFieldsFor('Foo')]);
validateCompletionsFor('SELECT |FROM Object', [newKeywordItem('COUNT()'), ...sobjectsFieldsFor('Object')]);
validateCompletionsFor('SELECT |FROM Foo', [newKeywordItem('COUNT()'), ...sobjectsFieldsFor('Foo')]);
validateCompletionsFor('SELECT | FROM Foo, Bar', [newKeywordItem('COUNT()'), ...sobjectsFieldsFor('Foo')]);
validateCompletionsFor('SELECT id, | FROM Foo', sobjectsFieldsFor('Foo'));
validateCompletionsFor('SELECT id,| FROM Foo', sobjectsFieldsFor('Foo'));
validateCompletionsFor('SELECT |, id FROM Foo', [newKeywordItem('COUNT()'), ...sobjectsFieldsFor('Foo')]);
validateCompletionsFor('SELECT |, id, FROM Foo', [newKeywordItem('COUNT()'), ...sobjectsFieldsFor('Foo')]);
validateCompletionsFor('SELECT id,| FROM', sobjectsFieldsFor('Object'));
// with alias
validateCompletionsFor('SELECT id,| FROM Foo F', sobjectsFieldsFor('Foo'));
validateCompletionsFor('SELECT |, id FROM Foo F', [newKeywordItem('COUNT()'), ...sobjectsFieldsFor('Foo')]);
validateCompletionsFor('SELECT |, id, FROM Foo F', [newKeywordItem('COUNT()'), ...sobjectsFieldsFor('Foo')]);
});
describe('Code Completion on nested select fields: SELECT ... FROM XYZ', () => {
// "COUNT()" can only be used on its own, unlike "COUNT(fieldName)".
// So we expect it on completions only right after "SELECT"
validateCompletionsFor('SELECT | (SELECT bar FROM Bar) FROM Foo', [
newKeywordItem('COUNT()'),
...sobjectsFieldsFor('Foo')
]);
validateCompletionsFor('SELECT (SELECT bar FROM Bar),| FROM Foo', sobjectsFieldsFor('Foo'));
validateCompletionsFor('SELECT (SELECT bar FROM Bar), | FROM Foo', sobjectsFieldsFor('Foo'));
validateCompletionsFor('SELECT id, | (SELECT bar FROM Bar) FROM Foo', sobjectsFieldsFor('Foo'));
validateCompletionsFor('SELECT foo, (SELECT | FROM Bars) FROM Foo', [...relationshipFieldsFor('Foo', 'Bars')]);
// TODO: improve ANTLR error strategy for this case:
validateCompletionsFor('SELECT foo, (SELECT |, bar FROM Bars) FROM Foo', [...relationshipFieldsFor('Foo', 'Bars')], {
skip: true
});
validateCompletionsFor('SELECT foo, (SELECT bar, | FROM Bars) FROM Foo', relationshipFieldsFor('Foo', 'Bars'));
/*
NOTE: Only 1 level of nesting is allowed. Thus, these are not valid queries:
SELECT foo, (SELECT bar, (SELECT | FROM XYZ) FROM Bar) FROM Foo
SELECT foo, (SELECT |, (SELECT xyz FROM XYZ) FROM Bar) FROM Foo
SELECT | (SELECT bar, (SELECT xyz FROM XYZ) FROM Bar) FROM Foo
*/
validateCompletionsFor('SELECT (SELECT |) FROM Foo', relationshipFieldsFor('Foo', undefined));
// We used to have special code just to handle this particular case.
// Not worth it, that's why it's skipped now.
// We keep the test here because it'd be nice to solve it in a generic way:
validateCompletionsFor('SELECT (SELECT ), | FROM Foo', sobjectsFieldsFor('Foo'), { skip: true });
validateCompletionsFor('SELECT foo, ( | FROM Foo', newKeywordItems('SELECT'));
validateCompletionsFor('SELECT foo, ( |FROM Foo', newKeywordItems('SELECT'));
validateCompletionsFor('SELECT foo, (| FROM Foo', newKeywordItems('SELECT'));
validateCompletionsFor('SELECT foo, (| FROM Foo', newKeywordItems('SELECT'));
validateCompletionsFor('SELECT foo, (|) FROM Foo', newKeywordItems('SELECT').concat(SELECT_SNIPPET));
validateCompletionsFor('SELECT foo, (SELECT bar FROM Bar), (SELECT | FROM Xyzs) FROM Foo', [
...relationshipFieldsFor('Foo', 'Xyzs')
]);
validateCompletionsFor(
'SELECT foo, (SELECT bar FROM Bar), (SELECT xyz, | FROM Xyzs) FROM Foo',
relationshipFieldsFor('Foo', 'Xyzs')
);
validateCompletionsFor(
'SELECT foo, | (SELECT bar FROM Bar), (SELECT xyz FROM Xyz) FROM Foo',
sobjectsFieldsFor('Foo')
);
validateCompletionsFor(
'SELECT foo, (SELECT bar FROM Bar), | (SELECT xyz FROM Xyz) FROM Foo',
sobjectsFieldsFor('Foo')
);
validateCompletionsFor('SELECT foo, (SELECT | FROM Bars), (SELECT xyz FROM Xyz) FROM Foo', [
...relationshipFieldsFor('Foo', 'Bars')
]);
// With a semi-join (SELECT in WHERE clause):
validateCompletionsFor(
`SELECT Id, Name, |
(SELECT Id, Parent.Profile.Name
FROM SetupEntityAccessItems
WHERE Parent.ProfileId != null)
FROM ApexClass
WHERE Id IN (SELECT SetupEntityId
FROM SetupEntityAccess)`,
sobjectsFieldsFor('ApexClass')
);
});
describe('Code Completion on SELECT XYZ FROM...', () => {
validateCompletionsFor('SELECT id FROM |', expectedSObjectCompletions);
validateCompletionsFor('SELECT id\nFROM |', expectedSObjectCompletions);
// cursor touching FROM should not complete with Sobject name
validateCompletionsFor('SELECT id\nFROM|', []);
validateCompletionsFor('SELECT id FROM |WHERE', expectedSObjectCompletions);
validateCompletionsFor('SELECT id FROM | WHERE', expectedSObjectCompletions);
validateCompletionsFor('SELECT id FROM | WHERE', expectedSObjectCompletions);
validateCompletionsFor('SELECT id FROM | WHERE', expectedSObjectCompletions);
validateCompletionsFor('SELECT id \nFROM |\nWHERE', expectedSObjectCompletions);
validateCompletionsFor('SELECTHHH id FROMXXX |', []);
});
describe('Code Completion on nested SELECT xyz FROM ...: parent-child relationship', () => {
validateCompletionsFor('SELECT id, (SELECT id FROM |) FROM Foo', [relationshipsItem('Foo')]);
validateCompletionsFor('SELECT id, (SELECT id FROM Foo) FROM |', expectedSObjectCompletions);
validateCompletionsFor('SELECT id, (SELECT id FROM |), (SELECT id FROM Bar) FROM Foo', [relationshipsItem('Foo')]);
validateCompletionsFor('SELECT id, (SELECT id FROM Foo), (SELECT id FROM |) FROM Bar', [relationshipsItem('Bar')]);
validateCompletionsFor(
'SELECT id, (SELECT FROM |) FROM Bar', // No fields on inner SELECT
[relationshipsItem('Bar')]
);
validateCompletionsFor(
'SELECT id, (SELECT FROM |), (SELECT Id FROM Foo) FROM Bar', // No fields on SELECT
[relationshipsItem('Bar')]
);
});
describe('Code Completion on SELECT FROM (no columns on SELECT)', () => {
validateCompletionsFor('SELECT FROM |', expectedSObjectCompletions, {});
validateCompletionsFor('SELECT\nFROM |', expectedSObjectCompletions);
validateCompletionsFor('SELECT FROM | WHERE', expectedSObjectCompletions);
validateCompletionsFor('SELECT\nFROM |\nWHERE\nORDER BY', expectedSObjectCompletions);
describe('Cursor is still touching FROM: it should still complete with fieldnames, and not SObject names', () => {
validateCompletionsFor('SELECT FROM|', [newKeywordItem('COUNT()'), ...sobjectsFieldsFor('Object')]);
validateCompletionsFor('SELECT\nFROM|', [newKeywordItem('COUNT()'), ...sobjectsFieldsFor('Object')]);
validateCompletionsFor('SELECT\nFROM|\nWHERE', [newKeywordItem('COUNT()'), ...sobjectsFieldsFor('Object')]);
});
validateCompletionsFor('SELECTHHH FROMXXX |', []);
});
describe('Code Completion for ORDER BY', () => {
validateCompletionsFor('SELECT id FROM Account ORDER BY |', [
{
kind: CompletionItemKind.Field,
label: '__SOBJECT_FIELDS_PLACEHOLDER',
data: { soqlContext: { sobjectName: 'Account', onlySortable: true } }
}
]);
// Nested, parent-child relationships:
validateCompletionsFor('SELECT id, (SELECT Email FROM Contacts ORDER BY |) FROM Account', [
{
kind: CompletionItemKind.Field,
label: '__RELATIONSHIP_FIELDS_PLACEHOLDER',
data: { soqlContext: { sobjectName: 'Account', relationshipName: 'Contacts', onlySortable: true } }
}
]);
});
describe('Code Completion for GROUP BY', () => {
validateCompletionsFor('SELECT COUNT(Id) FROM Account GROUP BY |', [
{
kind: CompletionItemKind.Field,
label: '__SOBJECT_FIELDS_PLACEHOLDER',
data: { soqlContext: { sobjectName: 'Account', onlyGroupable: true } }
},
...newKeywordItems('ROLLUP', 'CUBE')
]);
validateCompletionsFor('SELECT id FROM Account GROUP BY id |', [
...newKeywordItems('FOR', 'OFFSET', 'HAVING', 'LIMIT', 'ORDER BY', 'UPDATE TRACKING', 'UPDATE VIEWSTAT')
]);
// When there are aggregated fields on SELECT, the GROUP BY clause
// must include all non-aggregated fields... thus we want completion
// for those preselected
validateCompletionsFor('SELECT id FROM Account GROUP BY |', [
{
kind: CompletionItemKind.Field,
label: '__SOBJECT_FIELDS_PLACEHOLDER',
data: {
soqlContext: {
sobjectName: 'Account',
onlyGroupable: true,
mostLikelyItems: ['id']
}
}
},
...newKeywordItems('ROLLUP', 'CUBE')
]);
validateCompletionsFor('SELECT id, MAX(id2), AVG(AnnualRevenue) FROM Account GROUP BY |', [
{
kind: CompletionItemKind.Field,
label: '__SOBJECT_FIELDS_PLACEHOLDER',
data: {
soqlContext: {
sobjectName: 'Account',
onlyGroupable: true,
mostLikelyItems: ['id']
}
}
},
...newKeywordItems('ROLLUP', 'CUBE')
]);
validateCompletionsFor('SELECT ID, Name, MAX(id3), AVG(AnnualRevenue) FROM Account GROUP BY id, |', [
{
kind: CompletionItemKind.Field,
label: '__SOBJECT_FIELDS_PLACEHOLDER',
data: {
soqlContext: {
sobjectName: 'Account',
onlyGroupable: true,
mostLikelyItems: ['Name']
}
}
}
// NOTE: ROLLUP and CUBE not expected unless cursor right after GROUP BY
]);
// Expect more than one. Also test with inner queries..
validateCompletionsFor(
'SELECT Id, Name, (SELECT Id, Id2, AboutMe FROM User), AVG(AnnualRevenue) FROM Account GROUP BY |',
[
{
kind: CompletionItemKind.Field,
label: '__SOBJECT_FIELDS_PLACEHOLDER',
data: {
soqlContext: {
sobjectName: 'Account',
onlyGroupable: true,
mostLikelyItems: ['Id', 'Name']
}
}
},
...newKeywordItems('ROLLUP', 'CUBE')
]
);
});
describe('Some keyword candidates after FROM clause', () => {
validateCompletionsFor('SELECT id FROM Account |', [
newKeywordItem('WHERE', { preselect: true }),
...newKeywordItems('FOR', 'OFFSET', 'LIMIT', 'ORDER BY', 'GROUP BY', 'WITH', 'UPDATE TRACKING', 'UPDATE VIEWSTAT')
]);
validateCompletionsFor('SELECT id FROM Account FOR |', newKeywordItems('VIEW', 'REFERENCE'));
validateCompletionsFor('SELECT id FROM Account WITH |', newKeywordItems('DATA CATEGORY'));
// NOTE: GROUP BY not supported on nested (parent-child relationship) SELECTs
validateCompletionsFor('SELECT Account.Name, (SELECT FirstName, LastName FROM Contacts |) FROM Account', [
newKeywordItem('WHERE', { preselect: true }),
...newKeywordItems('FOR', 'OFFSET', 'LIMIT', 'ORDER BY', 'WITH', 'UPDATE TRACKING', 'UPDATE VIEWSTAT')
]);
validateCompletionsFor('SELECT id FROM Account LIMIT |', []);
});
describe('WHERE clause', () => {
validateCompletionsFor('SELECT id FROM Account WHERE |', [
...newKeywordItems('NOT'),
{
kind: CompletionItemKind.Field,
label: '__SOBJECT_FIELDS_PLACEHOLDER',
data: { soqlContext: { sobjectName: 'Account' } }
}
]);
validateCompletionsFor('SELECT id FROM Account WHERE Name |', [
...newKeywordItems('IN (', 'NOT IN (', '=', '!=', '<>'),
...newKeywordItemsWithContext('Account', 'Name', ['INCLUDES (', 'EXCLUDES (', '<', '<=', '>', '>=', 'LIKE'])
]);
validateCompletionsFor('SELECT id FROM Account WHERE Type IN (|', [
...newKeywordItems('SELECT'),
SELECT_SNIPPET,
...expectedItemsForLiterals(
{
sobjectName: 'Account',
fieldName: 'Type'
},
true
)
]);
validateCompletionsFor(
"SELECT id FROM Account WHERE Type IN ('Customer', |)",
expectedItemsForLiterals(
{
sobjectName: 'Account',
fieldName: 'Type'
},
true
)
);
validateCompletionsFor("SELECT id FROM Account WHERE Type IN (|, 'Customer')", [
...newKeywordItems('SELECT'),
SELECT_SNIPPET,
...expectedItemsForLiterals(
{
sobjectName: 'Account',
fieldName: 'Type'
},
true
)
]);
// NOTE: Unlike IN(), INCLUDES()/EXCLUDES() never support NULL in the list
validateCompletionsFor(
'SELECT Channel FROM QuickText WHERE Channel INCLUDES (|',
expectedItemsForLiterals(
{
sobjectName: 'QuickText',
fieldName: 'Channel'
},
false
)
);
validateCompletionsFor(
"SELECT Channel FROM QuickText WHERE Channel EXCLUDES('Email', |",
expectedItemsForLiterals(
{
sobjectName: 'QuickText',
fieldName: 'Channel'
},
false
)
);
validateCompletionsFor(
'SELECT id FROM Account WHERE Type = |',
expectedItemsForLiterals(
{
sobjectName: 'Account',
fieldName: 'Type'
},
true
)
);
validateCompletionsFor(
"SELECT id FROM Account WHERE Type = 'Boo' OR Name = |",
expectedItemsForLiterals(
{
sobjectName: 'Account',
fieldName: 'Name'
},
true
)
);
validateCompletionsFor(
"SELECT id FROM Account WHERE Type = 'Boo' OR Name LIKE |",
expectedItemsForLiterals(
{
sobjectName: 'Account',
fieldName: 'Name'
},
false
)
);
validateCompletionsFor(
'SELECT id FROM Account WHERE Account.Type = |',
expectedItemsForLiterals(
{
sobjectName: 'Account',
fieldName: 'Type'
},
true
)
);
validateCompletionsFor(
'SELECT Name FROM Account WHERE LastActivityDate < |',
expectedItemsForLiterals(
{
sobjectName: 'Account',
fieldName: 'LastActivityDate'
},
false
)
);
validateCompletionsFor(
'SELECT Name FROM Account WHERE LastActivityDate > |',
expectedItemsForLiterals(
{
sobjectName: 'Account',
fieldName: 'LastActivityDate'
},
false
)
);
});
describe('SELECT Function expressions', () => {
validateCompletionsFor('SELECT DISTANCE(|) FROM Account', [
{
kind: CompletionItemKind.Field,
label: '__SOBJECT_FIELDS_PLACEHOLDER',
data: { soqlContext: { sobjectName: 'Account' } }
}
]);
validateCompletionsFor('SELECT AVG(|) FROM Account', [
{
kind: CompletionItemKind.Field,
label: '__SOBJECT_FIELDS_PLACEHOLDER',
data: {
soqlContext: {
sobjectName: 'Account',
onlyAggregatable: true,
onlyTypes: ['double', 'int', 'currency', 'percent']
}
}
}
]);
// COUNT is treated differently, always worth testing it separately
validateCompletionsFor('SELECT COUNT(|) FROM Account', [
{
kind: CompletionItemKind.Field,
label: '__SOBJECT_FIELDS_PLACEHOLDER',
data: {
soqlContext: {
sobjectName: 'Account',
onlyAggregatable: true,
onlyTypes: [
'date',
'datetime',
'double',
'int',
'string',
'combobox',
'currency',
'DataCategoryGroupReference',
'email',
'id',
'masterrecord',
'percent',
'phone',
'picklist',
'reference',
'textarea',
'url'
]
}
}
}
]);
validateCompletionsFor('SELECT MAX(|) FROM Account', [
{
kind: CompletionItemKind.Field,
label: '__SOBJECT_FIELDS_PLACEHOLDER',
data: {
soqlContext: {
sobjectName: 'Account',
onlyAggregatable: true,
onlyTypes: [
'date',
'datetime',
'double',
'int',
'string',
'time',
'combobox',
'currency',
'DataCategoryGroupReference',
'email',
'id',
'masterrecord',
'percent',
'phone',
'picklist',
'reference',
'textarea',
'url'
]
}
}
}
]);
validateCompletionsFor('SELECT AVG(| FROM Account', [
{
kind: CompletionItemKind.Field,
label: '__SOBJECT_FIELDS_PLACEHOLDER',
data: {
soqlContext: {
sobjectName: 'Account',
onlyAggregatable: true,
onlyTypes: ['double', 'int', 'currency', 'percent']
}
}
}
]);
validateCompletionsFor('SELECT AVG(|), Id FROM Account', [
{
kind: CompletionItemKind.Field,
label: '__SOBJECT_FIELDS_PLACEHOLDER',
data: {
soqlContext: {
sobjectName: 'Account',
onlyAggregatable: true,
onlyTypes: ['double', 'int', 'currency', 'percent']
}
}
}
]);
validateCompletionsFor('SELECT Id, AVG(|) FROM Account', [
{
kind: CompletionItemKind.Field,
label: '__SOBJECT_FIELDS_PLACEHOLDER',
data: {
soqlContext: {
sobjectName: 'Account',
onlyAggregatable: true,
onlyTypes: ['double', 'int', 'currency', 'percent']
}
}
}
]);
// NOTE: cursor is right BEFORE the function expression:
validateCompletionsFor('SELECT Id, | SUM(AnnualRevenue) FROM Account', [...sobjectsFieldsFor('Account')]);
});
describe('Code Completion on "semi-join" (SELECT)', () => {
validateCompletionsFor('SELECT Id FROM Account WHERE Id IN (SELECT AccountId FROM |)', expectedSObjectCompletions);
validateCompletionsFor('SELECT Id FROM Account WHERE Id IN (SELECT FROM |)', expectedSObjectCompletions);
// NOTE: The SELECT of a semi-join only accepts an "identifier" type column, no functions
validateCompletionsFor('SELECT Id FROM Account WHERE Id IN (SELECT | FROM Foo)', [
{
kind: CompletionItemKind.Field,
label: '__SOBJECT_FIELDS_PLACEHOLDER',
data: { soqlContext: { sobjectName: 'Foo', onlyTypes: ['id', 'reference'], dontShowRelationshipField: true } }
}
]);
// NOTE: The SELECT of a semi-join can only have one field, thus
// we expect no completions here:
validateCompletionsFor('SELECT Id FROM Account WHERE Id IN (SELECT Id, | FROM Foo)', []);
});
describe('Special cases around newlines', () => {
validateCompletionsFor('SELECT id FROM|\n\n\n', []);
validateCompletionsFor('SELECT id FROM |\n\n', expectedSObjectCompletions);
validateCompletionsFor('SELECT id FROM\n|', expectedSObjectCompletions);
validateCompletionsFor('SELECT id FROM\n\n|', expectedSObjectCompletions);
validateCompletionsFor('SELECT id FROM\n|\n', expectedSObjectCompletions);
validateCompletionsFor('SELECT id FROM\n\n|\n\n', expectedSObjectCompletions);
validateCompletionsFor('SELECT id FROM\n\n\n\n\n\n|\n\n', expectedSObjectCompletions);
validateCompletionsFor('SELECT id FROM\n\n|\n\nWHERE', expectedSObjectCompletions);
validateCompletionsFor('SELECT id FROM\n\n|WHERE', expectedSObjectCompletions);
});
describe('Support leading comment lines (starting with // )', () => {
validateCompletionsFor(
`// This a comment line
SELECT id FROM |`,
expectedSObjectCompletions
);
});
describe('Support leading comment lines (starting with // )', () => {
validateCompletionsFor(
`// This a comment line 1
// This a comment line 2
// This a comment line 3
SELECT id FROM |`,
expectedSObjectCompletions
);
});
function validateCompletionsFor(
text: string,
expectedItems: CompletionItem[],
options: { skip?: boolean; only?: boolean; cursorChar?: string } = {}
): void {
const itFn = options.skip ? xit : options.only ? it.only : it;
const cursorChar = options.cursorChar || '|';
itFn(text, () => {
if (text.indexOf(cursorChar) !== text.lastIndexOf(cursorChar)) {
throw new Error(`Test text must have 1 and only 1 cursor (char: ${cursorChar})`);
}
const [line, column] = getCursorPosition(text, cursorChar);
const completions = completionsFor(text.replace(cursorChar, ''), line, column);
// NOTE: we don't use Sets here because when there are failures, the error
// message is not useful
expectedItems.forEach(item => expect(completions).toContainEqual(item));
completions.forEach(item => expect(expectedItems).toContainEqual(item));
});
}
function getCursorPosition(text: string, cursorChar: string): [number, number] {
for (const [line, lineText] of text.split('\n').entries()) {
const column = lineText.indexOf(cursorChar);
if (column >= 0) return [line + 1, column + 1];
}
throw new Error(`Cursor ${cursorChar} not found in ${text} !`);
}
function sobjectsFieldsFor(sobjectName: string): CompletionItem[] {
return [
{
kind: CompletionItemKind.Field,
label: '__SOBJECT_FIELDS_PLACEHOLDER',
data: { soqlContext: { sobjectName } }
},
...newKeywordItems('TYPEOF'),
newFunctionCallItem('AVG'),
newFunctionCallItem('MIN'),
newFunctionCallItem('MAX'),
newFunctionCallItem('SUM'),
newFunctionCallItem('COUNT'),
newFunctionCallItem('COUNT_DISTINCT'),
INNER_SELECT_SNIPPET
];
}
function relationshipFieldsFor(sobjectName: string, relationshipName?: string): CompletionItem[] {
return [
{
kind: CompletionItemKind.Field,
label: '__RELATIONSHIP_FIELDS_PLACEHOLDER',
data: { soqlContext: { sobjectName, relationshipName } }
},
...newKeywordItems('TYPEOF')
];
}