-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathRollupStateTests.cls
370 lines (341 loc) · 16.5 KB
/
RollupStateTests.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
@IsTest
private class RollupStateTests {
@IsTest
static void commitsAndLoadsStateProperly() {
RollupState state = new RollupState();
String stubAccountId = RollupTestUtils.createId(Account.SObjectType);
RollupState.GenericInfo info = (RollupState.GenericInfo) state.getState(
stubAccountId,
new Rollup__mdt(RollupOperation__c = 'SUM'),
RollupState.GenericInfo.class
);
info.value = 5;
RollupState.AverageInfo averageInfo = (RollupState.AverageInfo) state.getState(
stubAccountId,
new Rollup__mdt(RollupOperation__c = 'CONCAT'),
RollupState.AverageInfo.class
);
averageInfo.increment(10);
RollupState.SObjectInfo sObjectInfo = (RollupState.SObjectInfo) state.getState(
stubAccountId,
new Rollup__mdt(RollupOperation__c = 'FIRST'),
RollupState.SObjectInfo.class
);
sObjectInfo.setItem(new Account(AnnualRevenue = 1000));
String secondStubId = RollupTestUtils.createId(Contact.SObjectType);
RollupState.MostInfo mostInfo = (RollupState.MostInfo) state.getState(
secondStubId,
new Rollup__mdt(RollupOperation__c = 'MOST'),
RollupState.MostInfo.class
);
mostInfo.setValues(5, 'some string');
// populate a null state value to be sure if the last iteration is "empty" the body is still properly committed
state.getState(secondStubId, new Rollup__mdt(RollupOperation__c = 'LAST'), RollupState.GenericInfo.class);
Id stubJobId = RollupTestUtils.createId(AsyncApexJob.SObjectType);
Set<String> relatedRecordKeys = new Set<String>{ '%' + stubAccountId + '%', '%' + secondStubId + '%' };
state.commitState(stubJobId);
state.loadState(stubJobId, new Set<String>{ stubAccountId, secondStubId });
info = (RollupState.GenericInfo) state.getState(stubAccountId, new Rollup__mdt(RollupOperation__c = 'SUM'), RollupState.GenericInfo.class);
info.value = 6;
state.commitState(stubJobId);
RollupState__c insertedState = [
SELECT Id, Body0__c
FROM RollupState__c
WHERE RelatedRecordKeys0__c LIKE :relatedRecordKeys
];
Assert.isNotNull(insertedState.Body0__c, 'Serialized representation of generic state should be present');
state.loadState(stubJobId, new Set<String>{ stubAccountId, secondStubId });
Set<Object> actual = ((RollupState.AverageInfo) state.getState(
stubAccountId,
new Rollup__mdt(RollupOperation__c = 'CONCAT'),
RollupState.AverageInfo.class
))
.distinctNumerators;
Assert.areEqual(averageInfo.distinctNumerators.size(), actual.size());
Assert.areEqual(averageInfo.distinctNumerators.contains(10.00), actual.contains(10.00));
RollupState.GenericInfo updatedInfo = ((RollupState.GenericInfo) state.getState(
stubAccountId,
new Rollup__mdt(RollupOperation__c = 'SUM'),
RollupState.GenericInfo.class
));
Assert.areEqual(info.value, updatedInfo.value);
}
@IsTest
static void handlesMultipleExistingStateValues() {
Id stubJobId = RollupTestUtils.createId(AsyncApexJob.SObjectType);
String stubAccountId = RollupTestUtils.createId(Account.SObjectType);
RollupState state = new RollupState();
RollupState.AverageInfo averageInfo = (RollupState.AverageInfo) state.getState(
stubAccountId,
new Rollup__mdt(RollupOperation__c = 'AVERAGE'),
RollupState.AverageInfo.class
);
averageInfo.increment(10);
RollupState.GenericInfo info = (RollupState.GenericInfo) state.getState(
stubAccountId,
new Rollup__mdt(RollupOperation__c = 'SUM'),
RollupState.GenericInfo.class
);
RollupState__c existingAverageState = new RollupState__c(
RelatedJobId__c = stubJobId,
RelatedRecordKeys0__c = stubAccountId,
Body0__c = JSON.serialize(averageInfo.getUntypedState())
);
insert new List<RollupState__c>{
existingAverageState,
new RollupState__c(RelatedJobId__c = stubJobId, RelatedRecordKeys0__c = stubAccountId, Body0__c = JSON.serialize(info.getUntypedState()))
};
state = new RollupState();
state.loadState(stubJobId, new Set<String>{ stubAccountId });
info = (RollupState.GenericInfo) state.getState(stubAccountId, new Rollup__mdt(RollupOperation__c = 'SUM'), RollupState.GenericInfo.class);
info.value = 5;
averageInfo = (RollupState.AverageInfo) state.getState(stubAccountId, new Rollup__mdt(RollupOperation__c = 'AVERAGE'), RollupState.AverageInfo.class);
averageInfo.increment(20);
Test.startTest();
state.commitState(stubJobId);
Assert.areEqual(1, Limits.getDmlRows());
Test.stopTest();
Boolean hasCorrectAverageInfo = false;
Boolean hasCorrectGenericInfo = false;
for (RollupState__c createdState : [SELECT Body0__c FROM RollupState__c]) {
hasCorrectAverageInfo = hasCorrectAverageInfo || createdState.Body0__c.contains(JSON.serialize(averageInfo.getUntypedState()));
hasCorrectGenericInfo = hasCorrectGenericInfo || createdState.Body0__c.contains(JSON.serialize(info.getUntypedState()));
}
Assert.isTrue(hasCorrectAverageInfo, 'new average state should have been updated');
Assert.isTrue(hasCorrectGenericInfo, 'Generic info should not be wiped out');
}
@IsTest
static void splitsReallyLongStatesForTheSameRecord() {
// the type names take up a bit more space during namespaced packaging
RollupState.maxBodyLength = RollupTestUtils.IS_NAMESPACED_PACKAGE_ORG ? 20000 : 18000;
Id stubJobId = RollupTestUtils.createId(AsyncApexJob.SObjectType);
String stubAccountId = RollupTestUtils.createId(Account.SObjectType);
Rollup__mdt template = new Rollup__mdt(DeveloperName = 'exampleUnique40CharacterLimit');
RollupState state = new RollupState();
List<Map<String, Object>> statesToSample = new List<Map<String, Object>>();
for (Integer index = 0; index < 400; index++) {
Rollup__mdt clonedMeta = template.clone();
clonedMeta.DeveloperName += '' + index;
RollupState.GenericInfo info = (RollupState.GenericInfo) state.getState(stubAccountId, clonedMeta, RollupState.GenericInfo.class);
info.value = index;
if (index < 20) {
statesToSample.add(info.getUntypedState());
}
}
state.commitState(stubJobId);
List<RollupState__c> committedStates = [
SELECT Body0__c, RelatedRecordKeys0__c
FROM RollupState__c
];
Assert.areEqual(3, committedStates.size());
Boolean body0Filled = false;
Boolean sampleStatesFilled = false;
for (Integer index = 0; index < committedStates.size(); index++) {
RollupState__c committedState = committedStates[index];
body0Filled = body0Filled || committedState.Body0__c != null;
Assert.areEqual(stubAccountId, committedState.RelatedRecordKeys0__c, 'State was missing key at index: ' + index);
if (sampleStatesFilled) {
Boolean isDuplicate = committedState.Body0__c.contains(JSON.serialize(statesToSample).removeStart('[').removeEnd(']'));
if (isDuplicate) {
throw new IllegalArgumentException('Body0__c should not match for both states');
}
} else {
sampleStatesFilled = committedState.Body0__c?.contains(JSON.serialize(statesToSample).removeStart('[').removeEnd(']')) == true;
}
}
Assert.isTrue(body0Filled);
Assert.isTrue(sampleStatesFilled, committedStates[0].Body0__c?.substring(0, 50));
/**
* ensure that we don't exceed the DataWeave heap:
* System.DataWeaveScriptException: turtles.api.SandboxedLimitException - Heap limit exceeded 6004410 > 6000000
*/
state.loadState(stubJobId, new Set<String>{ stubAccountId });
}
@IsTest
static void splitsRelatedRecordKeysIntoDifferentTextFields() {
Rollup__mdt template = new Rollup__mdt(DeveloperName = 'one rollup');
RollupState state = new RollupState();
Set<String> fullKeys = new Set<String>();
List<String> fieldKeys0 = new List<String>();
List<String> fieldKeys1 = new List<String>();
List<String> fieldKeys2 = new List<String>();
List<String> fieldKeys3 = new List<String>();
List<String> fieldKeys4 = new List<String>();
List<String> fieldKeys5 = new List<String>();
List<String> fieldKeys6 = new List<String>();
List<String> fieldKeys7 = new List<String>();
List<String> fieldKeys8 = new List<String>();
List<String> fieldKeys9 = new List<String>();
List<String> fieldKeys10 = new List<String>();
List<String> secondFieldKeys0 = new List<String>();
String firstAccountId = RollupTestUtils.createId(Account.SObjectType) + 'aaa';
// 255 / 18 characters ~= 13 records per field (accounting for commas)
// 12 because with 11 fields, we want to "overflow" to the second RelatedRecordKeys0__c
for (Integer index = 0; index < 13 * 12; index++) {
String stubAccountId = index == 0 ? firstAccountId : RollupTestUtils.createId(Account.SObjectType) + 'aaa';
RollupState.GenericInfo info = (RollupState.GenericInfo) state.getState(stubAccountId, template, RollupState.GenericInfo.class);
info.value = index;
List<String> keys;
if (index < 13) {
keys = fieldKeys0;
} else if (index < 13 * 2) {
keys = fieldKeys1;
} else if (index < 13 * 3) {
keys = fieldKeys2;
} else if (index < 13 * 4) {
keys = fieldKeys3;
} else if (index < 13 * 5) {
keys = fieldKeys4;
} else if (index < 13 * 6) {
keys = fieldKeys5;
} else if (index < 13 * 7) {
keys = fieldKeys6;
} else if (index < 13 * 8) {
keys = fieldKeys7;
} else if (index < 13 * 9) {
keys = fieldKeys8;
} else if (index < 13 * 10) {
keys = fieldKeys9;
} else if (index < 13 * 11) {
keys = fieldKeys10;
} else if (index < 13 * 12) {
keys = secondFieldKeys0;
}
keys?.add(stubAccountId);
fullKeys.add(stubAccountId);
}
Id stubJobId = RollupTestUtils.createId(AsyncApexJob.SObjectType);
state.commitState(stubJobId);
List<RollupState__c> committedStates = [
SELECT
RelatedRecordKeys0__c,
RelatedRecordKeys1__c,
RelatedRecordKeys2__c,
RelatedRecordKeys3__c,
RelatedRecordKeys4__c,
RelatedRecordKeys5__c,
RelatedRecordKeys6__c,
RelatedRecordKeys7__c,
RelatedRecordKeys8__c,
RelatedRecordKeys9__c,
RelatedRecordKeys10__c
FROM RollupState__c
WHERE RelatedJobId__c = :stubJobId
];
// ensure that loadState can be called with the full spread of keys
state.loadState(stubJobId, fullKeys);
Assert.areEqual(2, committedStates.size());
RollupState__c firstState = committedStates.get(0);
RollupState__c secondState = committedStates.get(1);
// swap out for the correct "first" state - since they're inserted at the same time
// there's no deterministic way to order them
if (firstState.RelatedRecordKeys0__c.startsWith(firstAccountId) == false) {
secondState = firstState;
firstState = committedStates.get(1);
}
Assert.isNotNull(firstState.RelatedRecordKeys0__c, 'RelatedRecordKeys0__c should have been filled out');
Assert.isNotNull(firstState.RelatedRecordKeys1__c, 'RelatedRecordKeys1__c should have been filled out');
Assert.isNotNull(firstState.RelatedRecordKeys2__c, 'RelatedRecordKeys2__c should have been filled out');
Assert.isNotNull(firstState.RelatedRecordKeys3__c, 'RelatedRecordKeys3__c should have been filled out');
Assert.isNotNull(firstState.RelatedRecordKeys4__c, 'RelatedRecordKeys4__c should have been filled out');
Assert.isNotNull(firstState.RelatedRecordKeys5__c, 'RelatedRecordKeys5__c should have been filled out');
Assert.isNotNull(firstState.RelatedRecordKeys6__c, 'RelatedRecordKeys6__c should have been filled out');
Assert.isNotNull(firstState.RelatedRecordKeys7__c, 'RelatedRecordKeys7__c should have been filled out');
Assert.isNotNull(firstState.RelatedRecordKeys8__c, 'RelatedRecordKeys8__c should have been filled out');
Assert.isNotNull(firstState.RelatedRecordKeys9__c, 'RelatedRecordKeys9__c should have been filled out');
Assert.isNotNull(firstState.RelatedRecordKeys10__c, 'RelatedRecordKeys10__c should have been filled out');
Assert.isNotNull(secondState.RelatedRecordKeys0__c, 'Second RelatedRecordKeys0__c should have been filled out');
Assert.areEqual(String.join(fieldKeys0, ','), firstState.RelatedRecordKeys0__c, 'fieldKeys0');
Assert.areEqual(String.join(fieldKeys1, ','), firstState.RelatedRecordKeys1__c, 'fieldKeys1');
Assert.areEqual(String.join(fieldKeys2, ','), firstState.RelatedRecordKeys2__c, 'fieldKeys2');
Assert.areEqual(String.join(fieldKeys3, ','), firstState.RelatedRecordKeys3__c, 'fieldKeys3');
Assert.areEqual(String.join(fieldKeys4, ','), firstState.RelatedRecordKeys4__c, 'fieldKeys4');
Assert.areEqual(String.join(fieldKeys5, ','), firstState.RelatedRecordKeys5__c, 'fieldKeys5');
Assert.areEqual(String.join(fieldKeys6, ','), firstState.RelatedRecordKeys6__c, 'fieldKeys6');
Assert.areEqual(String.join(fieldKeys7, ','), firstState.RelatedRecordKeys7__c, 'fieldKeys7');
Assert.areEqual(String.join(fieldKeys8, ','), firstState.RelatedRecordKeys8__c, 'fieldKeys8');
Assert.areEqual(String.join(fieldKeys9, ','), firstState.RelatedRecordKeys9__c, 'fieldKeys9');
Assert.areEqual(String.join(fieldKeys10, ','), firstState.RelatedRecordKeys10__c, 'fieldKeys10');
Assert.areEqual(String.join(secondFieldKeys0, ','), secondState.RelatedRecordKeys0__c, 'secondFieldKeys0');
}
@IsTest
static void clearsStateProperly() {
Id stubJobId = RollupTestUtils.createId(AsyncApexJob.SObjectType);
insert new RollupState__c(RelatedJobId__c = stubJobId);
Test.startTest();
new RollupState().cleanup(new Set<String>{ stubJobId });
Test.stopTest();
Assert.areEqual(0, [SELECT COUNT() FROM RollupState__c]);
}
@IsTest
static void onlyLoadsStateForRelatedRecords() {
Id stubJobId = RollupTestUtils.createId(AsyncApexJob.SObjectType);
String stubAccountId = RollupTestUtils.createId(Account.SObjectType);
insert new List<RollupState__c>{
new RollupState__c(RelatedJobId__c = stubJobId),
new RollupState__c(RelatedJobId__c = stubJobId, RelatedRecordKeys0__c = stubAccountId)
};
RollupState state = new RollupState();
state.loadState(stubJobId, new Set<String>{ stubAccountId });
state.loadState(stubJobId, new Set<String>{ stubAccountId });
Assert.areEqual(1, Limits.getQueryRows());
Assert.areEqual(1, Limits.getQueries());
}
@IsTest
static void alwaysTracksRelatedRecordKeys() {
String firstId = RollupTestUtils.createId(Account.SObjectType) + 'aaa';
RollupState.maxRelatedKeysLength = firstId.length();
Rollup__mdt template = new Rollup__mdt(DeveloperName = 'unique');
RollupState state = new RollupState();
List<String> range = new String[14];
String serializedLastState;
String thirdToLastKey;
String secondToLastKey;
String lastKey;
for (Integer index = 0; index <= range.size(); index++) {
String stubAccountId = index == 0 || index == 12 ? firstId : RollupTestUtils.createId(Account.SObjectType) + 'bbb';
RollupState.GenericInfo info = (RollupState.GenericInfo) state.getState(stubAccountId, template, RollupState.GenericInfo.class);
info.value = index;
switch on index {
when 11 {
thirdToLastKey = stubAccountId;
}
when 13 {
secondToLastKey = stubAccountId;
}
when 14 {
lastKey = stubAccountId;
serializedLastState = JSON.serialize(info.getUntypedState());
}
}
}
state.commitState(RollupTestUtils.createId(AsyncApexJob.SObjectType));
List<RollupState__c> committedStates = [
SELECT
Body0__c,
RelatedRecordKeys0__c,
RelatedRecordKeys1__c,
RelatedRecordKeys2__c,
RelatedRecordKeys3__c,
RelatedRecordKeys4__c,
RelatedRecordKeys5__c,
RelatedRecordKeys6__c,
RelatedRecordKeys7__c,
RelatedRecordKeys8__c,
RelatedRecordKeys9__c,
RelatedRecordKeys10__c
FROM RollupState__c
];
Assert.areEqual(2, committedStates.size());
for (RollupState__c committedState : committedStates) {
if (committedState.RelatedRecordKeys3__c == null) {
Assert.isTrue(committedState.Body0__c.contains(serializedLastState));
Assert.areEqual(thirdToLastKey, committedState.RelatedRecordKeys0__c);
Assert.areEqual(secondToLastKey, committedState.RelatedRecordKeys1__c);
Assert.areEqual(lastKey, committedState.RelatedRecordKeys2__c);
} else {
Assert.areEqual(firstId, committedState.RelatedRecordKeys0__c);
}
}
}
}