-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathRollupQueryBuilderTests.cls
177 lines (142 loc) · 8.21 KB
/
RollupQueryBuilderTests.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
@SuppressWarnings('PMD.UnusedLocalVariable')
@IsTest
private class RollupQueryBuilderTests {
@SuppressWarnings('PMD.FieldNamingConventions')
private static final Set<String> objIds = new Set<String>();
@IsTest
static void shouldQueryAllTasks() {
String queryString = RollupQueryBuilder.Current.getQuery(Task.SObjectType, new List<String>{ 'Id' }, 'WhatId', '=');
// validate the query
List<SObject> records = Database.query(queryString);
System.assertEquals(true, queryString.contains('AND IsDeleted = false ALL ROWS'));
System.assertEquals(Task.SObjectType, records.getSObjectType());
}
@IsTest
static void shouldQueryAllEvents() {
String queryString = RollupQueryBuilder.Current.getQuery(Event.SObjectType, new List<String>{ 'Id' }, 'WhatId', '=');
Set<String> objIds = new Set<String>();
List<SObject> records = Database.query(queryString);
System.assertEquals(true, queryString.contains('AND IsDeleted = false ALL ROWS'));
System.assertEquals(Event.SObjectType, records.getSObjectType());
}
@IsTest
static void shouldProperlyQueryIfMultipleCasedVersionsOfSameFieldPassedIn() {
String queryString = RollupQueryBuilder.Current.getQuery(
Opportunity.SObjectType,
new List<String>{ 'Id', 'ID', 'id', 'iD', 'AccountId', 'AccountID', 'accountId', 'accountID' },
'AccountId',
'='
);
List<SObject> records = Database.query(queryString);
System.assertEquals(true, queryString.contains('Id'));
System.assertEquals(true, queryString.contains('AccountId'));
System.assertEquals(Opportunity.SObjectType, records.getSObjectType());
}
@IsTest
static void shouldNotBlowUpIfPassedInFieldsAreNullOrBlank() {
String queryString = RollupQueryBuilder.Current.getQuery(Opportunity.SObjectType, new List<String>{ '', null, ' ', 'Id' }, 'AccountId', '=');
List<SObject> records = Database.query(queryString);
System.assertEquals(true, queryString.contains('Id'));
System.assertEquals(Opportunity.SObjectType, records.getSObjectType());
}
@IsTest
static void shouldHandleNestedPolymporphicWhereClauses() {
// we expect that What.Type will end up being stripped out of this
// where clause; this is only true for polymorphic where clauses
String queryString = RollupQueryBuilder.Current.getQuery(
Event.SObjectType,
new List<String>{ 'Subject', 'WhatId' },
Event.WhatId.toString(),
'!=',
'(((What.Type = \'Account\') AND What.Owner.Id = :recordIds))'
);
System.assertEquals(true, queryString.contains('TYPEOF'), 'Polymorphic where clause should have been transformed: ' + queryString);
System.assertEquals(false, queryString.contains('What.Owner.Id'));
System.assertEquals(false, queryString.contains('()'), 'Should clean up removed nested conditionals: ' + queryString);
System.assertEquals(false, queryString.contains('AND AND '), queryString);
System.assertEquals(false, queryString.contains('What.Type'), 'What.Type should have been stripped out: ' + queryString);
Set<String> recordIds = new Set<String>();
Set<String> objIds = recordIds;
// confirm query works
List<SObject> records = Database.query(queryString);
System.assertEquals(Event.SObjectType, records.getSObjectType());
}
@IsTest
static void handlesNestedPolymorphicWhereClausesWithOr() {
// we expect that What.Type will end up being stripped out of this
// where clause; this is only true for polymorphic where clauses
String queryString = RollupQueryBuilder.Current.getQuery(
Event.SObjectType,
new List<String>{ 'Subject', 'WhatId' },
Event.WhatId.toString(),
'!=',
'(((What.Type = \'Account\') OR What.Owner.Id = :recordIds))'
);
Set<String> recordIds = new Set<String>();
Set<String> objIds = recordIds;
// confirm query works
List<SObject> records = Database.query(queryString);
System.assertEquals(Event.SObjectType, records.getSObjectType());
}
@IsTest
static void shouldWrapTopLevelOrClauses() {
String queryString = RollupQueryBuilder.Current.getQuery(
Opportunity.SObjectType,
new List<String>{ 'Amount' },
Opportunity.AccountId.toString(),
'=',
'Amount > 0 OR CloseDate = YESTERDAY'
);
System.assert(queryString.contains('(Amount > 0 OR CloseDate = YESTERDAY)'), 'top-level OR clause should be wrapped');
}
@IsTest
static void correctlyPutsAllRowsAtEnd() {
String queryString =
RollupQueryBuilder.Current.getQuery(Event.SObjectType, new List<String>{ 'Subject', 'WhatId' }, Event.WhatId.toString(), '!=') + '\nLIMIT 1';
System.assertEquals(true, queryString.contains(RollupQueryBuilder.ALL_ROWS), 'Needs to have all rows in order to be valid');
queryString = RollupQueryBuilder.Current.getAllRowSafeQuery(Event.SObjectType, queryString);
System.assertEquals(true, queryString.endsWith(RollupQueryBuilder.ALL_ROWS));
// validate query can be run
Database.query(queryString);
}
@IsTest
static void multiCurrencyOrgsAddIsoCodeWhenIsoCodeExists() {
if (RollupCurrencyInfo.isMultiCurrency() == false) {
return;
}
String queryString = RollupQueryBuilder.Current.getQuery(Account.SObjectType, new List<String>(), 'Id', '=');
System.assertEquals(true, queryString.contains(RollupCurrencyInfo.CURRENCY_ISO_CODE_FIELD_NAME));
}
@IsTest
static void doesNotAddIsoCodeForMultiCurrencyWhenFieldDoesNotExist() {
if (RollupCurrencyInfo.isMultiCurrency() == false) {
return;
}
String queryString = RollupQueryBuilder.Current.getQuery(ContactPointAddress.SObjectType, new List<String>(), 'Id', '=');
System.assertEquals(false, queryString.contains(RollupCurrencyInfo.CURRENCY_ISO_CODE_FIELD_NAME));
}
@IsTest
static void correctlyFormatsAggregateQuery() {
String countQuery = RollupQueryBuilder.Current.getQuery(Opportunity.SObjectType, new List<String>{ 'count()' }, 'Id', '=');
Integer countAmount = Database.countQuery(countQuery);
System.assertEquals(0, countAmount, 'Should make it here because query was formatted correctly');
}
@IsTest
static void doesNotBlowUpOnDeeplyNestedConditionals() {
String bigWhere = '(((Type = \'Event\' AND Status != \'Completed\' AND IsArchived = true) AND WhoId = :recordIds) OR ((Type = \'Letter-Note\' AND Status != \'Completed\' AND IsArchived = true) AND WhoId = :recordIds) OR ((Type = \'Meeting\' AND Status != \'Completed\' AND IsArchived = true) AND WhoId = :recordIds) OR ((Type = \'Email\' AND Status != \'Completed\' AND IsArchived = true) AND WhoId = :recordIds) OR ((Type = \'Call\' AND Status != \'Completed\' AND IsArchived = true) AND WhoId = :recordIds) OR ((Type = \'Text\' AND Status = \'Completed\' AND IsArchived = true) AND WhoId = :recordIds) OR ((Type = \'Voicemail\' AND Status = \'Completed\' AND IsArchived = true) AND WhoId = :recordIds) OR ((Type = \'Letter-Note\' AND Status = \'Completed\' AND IsArchived = true) AND WhoId = :recordIds) OR ((Type = \'Event\' AND Status = \'Completed\' AND IsArchived = true) AND WhoId = :recordIds) OR ((Type = \'Meeting\' AND Status = \'Completed\' AND IsArchived = true) AND WhoId = :recordIds) OR ((Type = \'Email\' AND Status = \'Completed\' AND IsArchived = true) AND WhoId = :recordIds) OR ((Type = \'Call\' AND Status = \'Completed\' AND IsArchived = true) AND WhoId = :recordIds))';
String queryString = RollupQueryBuilder.Current.getQuery(Task.SObjectType, new List<String>{ 'Id' }, 'Id', '=', bigWhere);
Set<Id> recordIds = new Set<Id>();
List<Task> tasks = Database.query(queryString);
System.assertEquals(true, tasks.isEmpty(), 'Should have made it here and query should have been run');
}
@IsTest
static void properlyFormatsOrWithAndClause() {
String whereClause = '(StageName = \'one\' AND CloseDate = TODAY) OR (StageName = \'four\' AND CloseDate = TODAY)';
String actualQuery = RollupQueryBuilder.Current.getQuery(Opportunity.SObjectType, new List<String>{ 'COUNT()' }, 'Id', '!=', whereClause);
System.assertEquals(0, Database.countQuery(actualQuery), 'Validation that query is syntactically valid');
System.assertEquals(
'SELECT count()\nFROM Opportunity\nWHERE Id != :objIds\nAND ((StageName = \'one\' AND CloseDate = TODAY) OR (StageName = \'four\' AND CloseDate = TODAY))',
actualQuery
);
}
}