-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathRollupFullRecalcTests.cls
2602 lines (2320 loc) · 109 KB
/
RollupFullRecalcTests.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
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
@IsTest
private class RollupFullRecalcTests {
@TestSetup
static void setup() {
Account acc = new Account(Name = RollupFullRecalcTests.class.getName());
insert acc;
upsert new RollupSettings__c(IsEnabled__c = true);
}
@IsTest
static void correctlyOrdersGrandparentRollupQueryString() {
Rollup.defaultControl = new RollupControl__mdt(IsRollupLoggingEnabled__c = true, MaxLookupRowsBeforeBatching__c = 0);
insert new Contact(LastName = 'grandparent rollup query string');
Rollup.performBulkFullRecalc(
new List<Rollup__mdt>{
new Rollup__mdt(
CalcItem__c = 'Contact',
GrandparentRelationshipFieldPath__c = 'ReportsTo.Account.Name',
LookupFieldOnCalcItem__c = 'ReportsToId',
LookupFieldOnLookupObject__c = 'Id',
LookupObject__c = 'Account',
RollupFieldOnCalcItem__c = 'FirstName',
RollupFieldOnLookupObject__c = 'Name',
RollupOperation__c = 'CONCAT'
)
},
Rollup.InvocationPoint.FROM_FULL_RECALC_LWC.name()
);
System.assertEquals(1, Rollup.CACHED_FULL_RECALCS.size());
List<String> queryParts = Rollup.CACHED_FULL_RECALCS.get(0).start(null).getQuery().split('\n');
System.assertEquals('ORDER BY ReportsTo.Account.Id,ReportsToId,Id', queryParts.get(queryParts.size() - 1));
}
@IsTest
static void shouldSortParentResetProcessorsToBeginning() {
RollupAsyncProcessor actualProcessor = RollupAsyncProcessor.getProcessor(
new Rollup.FilterResults(),
Contact.Id,
Contact.AccountId,
Account.Id,
Account.Name,
Account.SObjectType,
Contact.SObjectType,
Rollup.Op.SUM,
Rollup.InvocationPoint.FROM_APEX,
null,
null
);
RollupParentResetProcessor resetProcessor = new RollupParentResetProcessor(
new List<Rollup__mdt>(),
null,
'',
new Set<String>(),
Rollup.InvocationPoint.FROM_APEX
);
List<RollupAsyncProcessor> processors = new List<RollupAsyncProcessor>{
resetProcessor,
actualProcessor,
actualProcessor,
resetProcessor,
actualProcessor,
resetProcessor
};
processors.sort();
System.assertEquals(true, processors[0] instanceof RollupParentResetProcessor, processors[0]);
System.assertEquals(true, processors[1] instanceof RollupParentResetProcessor, processors[0]);
System.assertEquals(true, processors[2] instanceof RollupParentResetProcessor, processors[0]);
}
@IsTest
static void shouldPerformBulkFullRecalcWithHierarchy() {
// also validates that RollupRelationshipFieldFinder aborts early if it's called
// for a top-level parent
Rollup.defaultControl = new RollupControl__mdt(ShouldAbortRun__c = true, IsRollupLoggingEnabled__c = true);
Account acc = [SELECT Id FROM Account];
Account childAccount = new Account(Name = 'Hierarchy child', ParentId = acc.Id, AnnualRevenue = 5);
insert childAccount;
Rollup.shouldRefreshCachedControl = true;
Rollup.onlyUseMockMetadata = true;
List<Rollup__mdt> metadata = new List<Rollup__mdt>{
new Rollup__mdt(
CalcItem__c = 'Account',
LookupObject__c = 'Account',
RollupToUltimateParent__c = true,
LookupFieldOnCalcItem__c = 'ParentId',
UltimateParentLookup__c = 'ParentId',
RollupOperation__c = 'SUM',
RollupFieldOnLookupObject__c = 'AnnualRevenue',
RollupFieldOnCalcItem__c = 'AnnualRevenue',
LookupFieldOnLookupObject__c = 'Id'
)
};
Test.startTest();
Rollup.performBulkFullRecalc(metadata, Rollup.InvocationPoint.FROM_FULL_RECALC_LWC.name());
Test.stopTest();
acc = [SELECT Id, AnnualRevenue FROM Account WHERE Id = :acc.Id];
System.assertEquals(childAccount.AnnualRevenue, acc.AnnualRevenue);
}
@IsTest
static void shouldPerformFullRecalcWithGrandparentHierarchyApex() {
Rollup.onlyUseMockMetadata = true; // necessary for namespaced package generation
Account acc = [SELECT Id FROM Account];
Account childAccount = new Account(Name = 'Hierarchy child', ParentId = acc.Id);
insert childAccount;
ParentApplication__c parentApp = new ParentApplication__c(Account__c = childAccount.Id, Name = 'Link to child account');
ParentApplication__c secondParentApp = new ParentApplication__c(Account__c = acc.Id, Name = 'Linked directly to parent account');
insert new List<ParentApplication__c>{ parentApp, secondParentApp };
// ensure that both a top-level child item linked only to the ultimate parent and one linked to the hierarchy child is included
List<Application__c> apps = new List<Application__c>{
new Application__c(Engagement_Score__c = 1000, ParentApplication__c = parentApp.Id, Name = 'Linked to Child Parent App'),
new Application__c(Engagement_Score__c = 1000, ParentApplication__c = secondParentApp.Id, Name = 'Linked to Direct Parent App')
};
insert apps;
List<Rollup__mdt> metadata = new List<Rollup__mdt>{
new Rollup__mdt(
CalcItem__c = Application__c.SObjectType.toString(),
LookupObject__c = 'Account',
RollupToUltimateParent__c = true,
LookupFieldOnCalcItem__c = Application__c.ParentApplication__c.toString(),
UltimateParentLookup__c = 'ParentId',
RollupOperation__c = 'SUM',
RollupFieldOnLookupObject__c = 'AnnualRevenue',
RollupFieldOnCalcItem__c = Application__c.Engagement_Score__c.toString(),
LookupFieldOnLookupObject__c = 'Id',
GrandparentRelationshipFieldPath__c = RollupTestUtils.getRelationshipPath(
new List<Schema.SObjectField>{ Application__c.ParentApplication__c, ParentApplication__c.Account__c, Account.AnnualRevenue }
)
)
};
Test.startTest();
Rollup.performBulkFullRecalc(metadata, Rollup.InvocationPoint.FROM_FULL_RECALC_LWC.name());
Test.stopTest();
Account updatedAcc = [SELECT Id, AnnualRevenue FROM Account WHERE Id = :acc.Id];
System.assertEquals(2000, updatedAcc.AnnualRevenue, 'Apex grandparent rollup should fully recalc');
}
@IsTest
static void supportsOneToManyFullRecalcInMiddleOfChain() {
Rollup.onlyUseMockMetadata = true; // necessary for namespaced package generation
Account acc = [SELECT Id FROM Account];
Individual indy = new Individual(LastName = 'Indy');
insert indy;
Contact con = new Contact(LastName = 'One To Many Child', AccountId = acc.Id, IndividualId = indy.Id);
insert con;
ParentApplication__c parentApp = new ParentApplication__c(Account__c = acc.Id, Name = 'One');
ParentApplication__c secondParentApp = new ParentApplication__c(Account__c = acc.Id, Name = 'Two');
insert new List<ParentApplication__c>{ parentApp, secondParentApp };
List<Application__c> apps = new List<Application__c>{
new Application__c(Engagement_Score__c = 1000, ParentApplication__c = parentApp.Id, Name = 'App One'),
new Application__c(Engagement_Score__c = 1000, ParentApplication__c = secondParentApp.Id, Name = 'App Two')
};
insert apps;
List<Rollup__mdt> metadata = new List<Rollup__mdt>{
new Rollup__mdt(
CalcItem__c = Application__c.SObjectType.toString(),
LookupObject__c = 'Individual',
LookupFieldOnCalcItem__c = Application__c.ParentApplication__c.toString(),
RollupOperation__c = 'SUM',
RollupFieldOnLookupObject__c = 'ConsumerCreditScore',
RollupFieldOnCalcItem__c = Application__c.Engagement_Score__c.toString(),
LookupFieldOnLookupObject__c = 'Id',
GrandparentRelationshipFieldPath__c = String.valueOf(Application__c.ParentApplication__c).replace('__r', '__c') +
'.' +
String.valueOf(ParentApplication__c.Account__c).replace('__c', '__r') +
'.Contacts.Individual.ConsumerCreditScore',
OneToManyGrandparentFields__c = 'Contact.AccountId'
)
};
Test.startTest();
Rollup.performBulkFullRecalc(metadata, Rollup.InvocationPoint.FROM_FULL_RECALC_LWC.name());
Test.stopTest();
Individual updatedIndy = [SELECT Id, ConsumerCreditScore FROM Individual WHERE Id = :indy.Id];
System.assertEquals(2000, updatedIndy.ConsumerCreditScore, 'Apex grandparent rollup should fully recalc with one to many field path');
}
@IsTest
static void supportsOneToManyAtBeginningOfChain() {
Rollup.onlyUseMockMetadata = true;
Account acc = [SELECT Id, AnnualRevenue FROM Account];
acc.AnnualRevenue = 67;
update acc;
Individual indy = new Individual(LastName = 'Indy');
insert indy;
Contact con = new Contact(LastName = 'One To Many Child', AccountId = acc.Id, IndividualId = indy.Id);
insert con;
List<Rollup__mdt> metadata = new List<Rollup__mdt>{
new Rollup__mdt(
CalcItem__c = 'Account',
LookupObject__c = 'Individual',
LookupFieldOnCalcItem__c = 'Id',
RollupOperation__c = 'SUM',
RollupFieldOnLookupObject__c = 'ConsumerCreditScore',
RollupFieldOnCalcItem__c = 'AnnualRevenue',
LookupFieldOnLookupObject__c = 'Id',
GrandparentRelationshipFieldPath__c = 'Contacts.Individual.ConsumerCreditScore',
OneToManyGrandparentFields__c = 'Contact.AccountId'
)
};
Test.startTest();
Rollup.performBulkFullRecalc(metadata, Rollup.InvocationPoint.FROM_FULL_RECALC_LWC.name());
Test.stopTest();
Individual updatedIndy = [SELECT Id, ConsumerCreditScore FROM Individual WHERE Id = :indy.Id];
System.assertEquals(acc.AnnualRevenue, updatedIndy.ConsumerCreditScore, 'Apex grandparent rollup should fully recalc with one to many field path');
}
@IsTest
static void doesNotUpdateParentRecordsWhereRollupValueHasNotChangedInFullRecalc() {
RollupTestUtils.DMLMock mock = new RollupTestUtils.DMLMock();
Rollup.DML = mock;
Account acc = [SELECT Id FROM Account];
Account secondParent = new Account(Name = 'Second', NumberOfEmployees = 1);
insert secondParent;
List<ContactPointAddress> cpas = new List<ContactPointAddress>{
new ContactPointAddress(PreferenceRank = 500, ParentId = acc.Id, Name = 'One'),
// ensure that the count of children records associated with the second parent DOES match
// the second parent's existing NumberOfEmployees; here we are looking to ensure only the parent records needing an update are saved
new ContactPointAddress(ParentId = secondParent.Id, Name = 'Two')
};
insert cpas;
List<Rollup__mdt> metadata = new List<Rollup__mdt>{
new Rollup__mdt(
CalcItem__c = 'ContactPointAddress',
LookupObject__c = 'Account',
LookupFieldOnCalcItem__c = 'ParentId',
RollupOperation__c = 'SUM',
RollupFieldOnLookupObject__c = 'AnnualRevenue',
RollupFieldOnCalcItem__c = 'PreferenceRank',
LookupFieldOnLookupObject__c = 'Id',
CalcItemWhereClause__c = 'Name != \'Two\''
),
new Rollup__mdt(
CalcItem__c = 'ContactPointAddress',
LookupObject__c = 'Account',
LookupFieldOnCalcItem__c = 'ParentId',
RollupOperation__c = 'COUNT',
RollupFieldOnLookupObject__c = 'NumberOfEmployees',
RollupFieldOnCalcItem__c = 'Id',
LookupFieldOnLookupObject__c = 'Id',
CalcItemWhereClause__c = 'Name != \'One\''
)
};
Test.startTest();
Rollup.performBulkFullRecalc(metadata, Rollup.InvocationPoint.FROM_FULL_RECALC_LWC.name());
Test.stopTest();
Map<Id, SObject> updatedRecordMap = new Map<Id, SObject>(mock.Records);
System.assertEquals(1, updatedRecordMap.size());
System.assertEquals(true, updatedRecordMap.containsKey(acc.Id));
}
@IsTest
static void updatesParentRecordsWhenAtLeastOneCmdtRecordDoesNotHaveWhereClause() {
RollupTestUtils.DMLMock mock = new RollupTestUtils.DMLMock();
Rollup.DML = mock;
Account acc = [SELECT Id FROM Account];
Account secondParent = new Account(Name = 'Two', Description = 'Two');
insert secondParent;
List<ContactPointAddress> cpas = new List<ContactPointAddress>{
new ContactPointAddress(PreferenceRank = 500, ParentId = acc.Id, Name = 'One'),
new ContactPointAddress(ParentId = secondParent.Id, Name = secondParent.Name)
};
insert cpas;
List<Rollup__mdt> metadata = new List<Rollup__mdt>{
new Rollup__mdt(
CalcItem__c = 'ContactPointAddress',
LookupObject__c = 'Account',
LookupFieldOnCalcItem__c = 'ParentId',
RollupOperation__c = 'SUM',
RollupFieldOnLookupObject__c = 'AnnualRevenue',
RollupFieldOnCalcItem__c = 'PreferenceRank',
LookupFieldOnLookupObject__c = 'Id',
CalcItemWhereClause__c = 'Name != \'Two\''
),
new Rollup__mdt(
CalcItem__c = 'ContactPointAddress',
LookupObject__c = 'Account',
LookupFieldOnCalcItem__c = 'ParentId',
RollupOperation__c = 'COUNT',
RollupFieldOnLookupObject__c = 'NumberOfEmployees',
RollupFieldOnCalcItem__c = 'Id',
LookupFieldOnLookupObject__c = 'Id',
CalcItemWhereClause__c = 'Name != \'Two\''
),
new Rollup__mdt(
CalcItem__c = 'ContactPointAddress',
LookupObject__c = 'Account',
LookupFieldOnCalcItem__c = 'ParentId',
RollupOperation__c = 'CONCAT',
RollupFieldOnLookupObject__c = 'Description',
RollupFieldOnCalcItem__c = 'Name',
LookupFieldOnLookupObject__c = 'Id'
)
};
Test.startTest();
Rollup.performBulkFullRecalc(metadata, Rollup.InvocationPoint.FROM_FULL_RECALC_LWC.name());
Test.stopTest();
Map<Id, SObject> updatedRecordMap = new Map<Id, SObject>(mock.Records);
System.assertEquals(true, updatedRecordMap.containsKey(acc.Id));
System.assertNotEquals(true, updatedRecordMap.containsKey(secondParent.Id));
System.assertEquals(cpas[0].Name, updatedRecordMap.get(acc.Id).get('Description'));
}
@IsTest
static void preventsParentRecordResetsWhenControlIsFlagged() {
Rollup.defaultControl = new RollupControl__mdt(MaxLookupRowsBeforeBatching__c = 2, ShouldSkipResettingParentFields__c = true);
Account acc = [SELECT Id FROM Account];
acc.AnnualRevenue = 75;
acc.NumberOfEmployees = 15;
update acc;
List<ContactPointAddress> cpas = new List<ContactPointAddress>{ new ContactPointAddress(PreferenceRank = 500, ParentId = acc.Id, Name = 'One') };
insert cpas;
List<Rollup__mdt> metadata = new List<Rollup__mdt>{
new Rollup__mdt(
CalcItem__c = 'ContactPointAddress',
LookupObject__c = 'Account',
LookupFieldOnCalcItem__c = 'ParentId',
RollupOperation__c = 'SUM',
RollupFieldOnLookupObject__c = 'AnnualRevenue',
RollupFieldOnCalcItem__c = 'PreferenceRank',
LookupFieldOnLookupObject__c = 'Id',
CalcItemWhereClause__c = 'Name != \'One\''
),
new Rollup__mdt(
CalcItem__c = 'ContactPointAddress',
LookupObject__c = 'Account',
LookupFieldOnCalcItem__c = 'ParentId',
RollupOperation__c = 'COUNT',
RollupFieldOnLookupObject__c = 'NumberOfEmployees',
RollupFieldOnCalcItem__c = 'Id',
LookupFieldOnLookupObject__c = 'Id',
CalcItemWhereClause__c = 'Name != \'One\''
),
new Rollup__mdt(
CalcItem__c = 'ContactPointAddress',
LookupObject__c = 'Account',
LookupFieldOnCalcItem__c = 'ParentId',
RollupOperation__c = 'CONCAT',
RollupFieldOnLookupObject__c = 'Description',
RollupFieldOnCalcItem__c = 'Name',
LookupFieldOnLookupObject__c = 'Id'
)
};
Test.startTest();
Rollup.performBulkFullRecalc(metadata, Rollup.InvocationPoint.FROM_FULL_RECALC_LWC.name());
Test.stopTest();
Account updatedAccount = [SELECT AnnualRevenue, NumberOfEmployees FROM Account WHERE Id = :acc.Id];
System.assertEquals(acc, updatedAccount);
}
@IsTest
static void doesNotUpdateParentRecordsWithoutUpdatesWhenRecordsExcludedByEvaluator() {
RollupTestUtils.DMLMock mock = new RollupTestUtils.DMLMock();
Rollup.DML = mock;
Account acc = [SELECT Id FROM Account];
Account secondParent = new Account(Name = 'Second');
insert secondParent;
List<ContactPointAddress> cpas = new List<ContactPointAddress>{
new ContactPointAddress(PreferenceRank = 500, ParentId = acc.Id, Name = 'One'),
new ContactPointAddress(ParentId = secondParent.Id, Name = 'Two')
};
insert cpas;
List<Rollup__mdt> metadata = new List<Rollup__mdt>{
new Rollup__mdt(
CalcItem__c = 'ContactPointAddress',
LookupObject__c = 'Account',
LookupFieldOnCalcItem__c = 'ParentId',
RollupOperation__c = 'SUM',
RollupFieldOnLookupObject__c = 'AnnualRevenue',
RollupFieldOnCalcItem__c = 'PreferenceRank',
LookupFieldOnLookupObject__c = 'Id'
),
new Rollup__mdt(
CalcItem__c = 'ContactPointAddress',
LookupObject__c = 'Account',
LookupFieldOnCalcItem__c = 'ParentId',
RollupOperation__c = 'COUNT',
RollupFieldOnLookupObject__c = 'NumberOfEmployees',
RollupFieldOnCalcItem__c = 'Id',
LookupFieldOnLookupObject__c = 'Id',
CalcItemWhereClause__c = 'ParentId != \'' + secondParent.Id + '\''
)
};
Test.startTest();
Rollup.performBulkFullRecalc(metadata, Rollup.InvocationPoint.FROM_FULL_RECALC_LWC.name());
Test.stopTest();
Map<Id, SObject> updatedRecordMap = new Map<Id, SObject>(mock.Records);
System.assertEquals(1, updatedRecordMap.size());
System.assertEquals(true, updatedRecordMap.containsKey(acc.Id));
acc = (Account) mock.Records[0];
System.assertEquals(500, acc.AnnualRevenue);
}
@IsTest
static void shouldProperlyBulkifyApexFullRecalcs() {
// cheating, but we'll use REFRESH_ rollup operations to reproduce this issue (REFRESH is typically only available in Flow)
Account acc = [SELECT Id, OwnerId FROM Account];
List<Event> events = new List<Event>{
new Event(WhatId = acc.Id, Subject = 'One', ActivityDatetime = System.now(), DurationInMinutes = 60),
new Event(WhatId = acc.Id, Subject = 'Two', ActivityDatetime = System.now(), DurationInMinutes = 60)
};
insert events;
RollupTestUtils.DMLMock mock = new RollupTestUtils.DMLMock();
Rollup.DML = mock;
Rollup.shouldRun = true;
Rollup.records = events;
Rollup.rollupMetadata = new List<Rollup__mdt>{
new Rollup__mdt(
CalcItem__c = 'Event',
RollupFieldOnCalcItem__c = 'Subject',
LookupFieldOnCalcItem__c = 'WhatId',
LookupObject__c = 'Account',
LookupFieldOnLookupObject__c = 'Id',
RollupFieldOnLookupObject__c = 'Name',
RollupOperation__c = 'REFRESH_CONCAT'
),
new Rollup__mdt(
CalcItem__c = 'Event',
RollupFieldOnCalcItem__c = 'Id',
LookupFieldOnCalcItem__c = 'WhatId',
LookupObject__c = 'Account',
LookupFieldOnLookupObject__c = 'Id',
RollupFieldOnLookupObject__c = 'AnnualRevenue',
RollupOperation__c = 'REFRESH_COUNT',
CalcItemWhereClause__c = 'WhatId = \'' + acc.Id + '\''
),
new Rollup__mdt(
CalcItem__c = 'Event',
RollupFieldOnCalcItem__c = 'DurationInMinutes',
LookupFieldOnCalcItem__c = 'WhatId',
LookupObject__c = 'Account',
LookupFieldOnLookupObject__c = 'Id',
RollupFieldOnLookupObject__c = 'NumberOfEmployees',
RollupOperation__c = 'REFRESH_SUM'
)
};
Rollup.apexContext = TriggerOperation.AFTER_DELETE;
Test.startTest();
Rollup.runFromTrigger();
Test.stopTest();
System.assertEquals(0, mock.Records.size(), 'No updates should have been made for non-merge delete');
}
@IsTest
static void shouldAllowGrandparentRollupsFromParent() {
// also an integration test for having an ORDER BY statement
// in RollupFullBatchRecalculator
Account acc = [SELECT Id, OwnerId FROM Account];
List<Event> events = new List<Event>{
new Event(WhatId = acc.Id, Subject = 'One', ActivityDatetime = System.now(), DurationInMinutes = 60),
new Event(WhatId = acc.Id, Subject = 'Two', ActivityDatetime = System.now(), DurationInMinutes = 60)
};
insert events;
RollupTestUtils.DMLMock mock = new RollupTestUtils.DMLMock();
Rollup.DML = mock;
Rollup.shouldRun = true;
Rollup.records = [SELECT Id, AboutMe FROM User WHERE Id = :acc.OwnerId];
Rollup.rollupMetadata = new List<Rollup__mdt>{
new Rollup__mdt(
CalcItem__c = 'Event',
RollupFieldOnCalcItem__c = 'Subject',
LookupFieldOnCalcItem__c = 'WhatId',
LookupObject__c = 'User',
LookupFieldOnLookupObject__c = 'Id',
RollupFieldOnLookupObject__c = 'AboutMe',
RollupOperation__c = 'CONCAT',
GrandparentRelationshipFieldPath__c = 'What.Owner.AboutMe',
IsRollupStartedFromParent__c = true,
CalcItemWhereClause__c = 'What.Type = \'Account\''
)
};
Rollup.apexContext = TriggerOperation.AFTER_INSERT;
Test.startTest();
Rollup.runFromTrigger();
Test.stopTest();
System.assertEquals(1, mock.Records.size(), 'Grandparent record should have been found!');
User updatedUser = (User) mock.Records[0];
System.assertEquals(events[0].Subject + ', ' + events[1].Subject, updatedUser.AboutMe, 'Grandparent rollup should have worked!');
}
@IsTest
static void shouldMapChildrenWithDifferentGrandparentWhereClauses() {
Account acc = [SELECT Id, OwnerId FROM Account];
List<Event> events = new List<Event>{
new Event(WhatId = acc.Id, Subject = 'Two', ActivityDatetime = System.now(), DurationInMinutes = 30),
new Event(WhatId = acc.Id, Subject = 'One', ActivityDatetime = System.now(), DurationInMinutes = 60)
};
insert events;
Rollup.defaultControl = Rollup.getDefaultControl();
Rollup.defaultControl.ShouldSkipResettingParentFields__c = true;
Rollup.rollupMetadata = new List<Rollup__mdt>{
new Rollup__mdt(
CalcItem__c = 'Event',
RollupFieldOnCalcItem__c = 'Subject',
LookupFieldOnCalcItem__c = 'WhatId',
LookupObject__c = 'User',
LookupFieldOnLookupObject__c = 'Id',
RollupFieldOnLookupObject__c = 'AboutMe',
RollupOperation__c = 'CONCAT',
GrandparentRelationshipFieldPath__c = 'What.Owner.AboutMe',
CalcItemWhereClause__c = 'DurationInMinutes <= 30'
),
new Rollup__mdt(
CalcItem__c = 'Event',
RollupFieldOnCalcItem__c = 'Subject',
LookupFieldOnCalcItem__c = 'WhatId',
LookupObject__c = 'User',
LookupFieldOnLookupObject__c = 'Id',
RollupFieldOnLookupObject__c = 'CompanyName',
RollupOperation__c = 'CONCAT',
GrandparentRelationshipFieldPath__c = 'What.Owner.CompanyName',
CalcItemWhereClause__c = 'DurationInMinutes > 30'
)
};
Test.startTest();
Rollup.performBulkFullRecalc(Rollup.rollupMetadata, Rollup.InvocationPoint.FROM_FULL_RECALC_LWC.name());
Test.stopTest();
User updatedUser = [SELECT AboutMe, CompanyName FROM User WHERE Id = :acc.OwnerId];
System.assertEquals(events[0].Subject, updatedUser.AboutMe);
System.assertEquals(events[1].Subject, updatedUser.CompanyName);
}
@IsTest
static void shouldAllowGrandparentRollupFromParentWithPolymorphicFields() {
// technically shouldAllowGrandparentRollupsFromParent also tests polymorphic fields
// this test does that in conjunction with a more complicated where clause
Account acc = [SELECT Id, OwnerId FROM Account];
List<ContactPointAddress> cpas = new List<ContactPointAddress>{
new ContactPointAddress(ParentId = acc.Id, Name = 'One'),
new ContactPointAddress(ParentId = acc.Id, Name = 'Two')
};
insert cpas;
RollupTestUtils.DMLMock mock = new RollupTestUtils.DMLMock();
Rollup.DML = mock;
Rollup.shouldRun = true;
Rollup.records = [SELECT Id, AboutMe FROM User WHERE Id = :acc.OwnerId];
Rollup.rollupMetadata = new List<Rollup__mdt>{
new Rollup__mdt(
CalcItem__c = 'ContactPointAddress',
RollupFieldOnCalcItem__c = 'Name',
LookupFieldOnCalcItem__c = 'ParentId',
LookupObject__c = 'User',
LookupFieldOnLookupObject__c = 'Id',
RollupFieldOnLookupObject__c = 'AboutMe',
RollupOperation__c = 'CONCAT',
GrandparentRelationshipFieldPath__c = 'Parent.Owner.AboutMe',
IsRollupStartedFromParent__c = true,
CalcItemWhereClause__c = 'ParentId = \'' + acc.Id + '\' OR Parent.Type = \'Account\''
)
};
Rollup.apexContext = TriggerOperation.AFTER_INSERT;
Test.startTest();
Rollup.runFromTrigger();
Test.stopTest();
System.assertEquals(1, mock.Records.size(), 'Grandparent record should have been found!');
User updatedUser = (User) mock.Records[0];
System.assertEquals(cpas[0].Name + ', ' + cpas[1].Name, updatedUser.AboutMe, 'Grandparent rollup should have worked!');
}
/** Parent-initiated rollups */
@IsTest
static void shouldAllowRollupToBeInitiatedFromTheParent() {
Account acc = [SELECT Id, AnnualRevenue FROM Account];
Asset asset = new Asset(AccountId = acc.Id, Name = 'Rollup initiated from parent', Quantity = 500);
insert asset;
Rollup.rollupMetadata = new List<Rollup__mdt>{
new Rollup__mdt(
RollupFieldOnCalcItem__c = 'Quantity',
LookupObject__c = 'Account',
LookupFieldOnCalcItem__c = 'AccountId',
LookupFieldOnLookupObject__c = 'Id',
RollupFieldOnLookupObject__c = 'AnnualRevenue',
RollupOperation__c = 'COUNT',
CalcItem__c = 'Asset',
IsRollupStartedFromParent__c = true
),
new Rollup__mdt(
RollupFieldOnCalcItem__c = 'Name',
LookupObject__c = 'Account',
LookupFieldOnCalcItem__c = 'AccountId',
LookupFieldOnLookupObject__c = 'Id',
RollupFieldOnLookupObject__c = 'Name',
RollupOperation__c = 'CONCAT',
CalcItem__c = 'Asset',
IsRollupStartedFromParent__c = true
)
};
Rollup.apexContext = TriggerOperation.AFTER_UPDATE;
Rollup.records = new List<Account>{ acc };
Rollup.oldRecordsMap = new Map<Id, Account>{ acc.Id => new Account(Id = acc.Id, AnnualRevenue = 0) };
Rollup.shouldRun = true;
RollupTestUtils.DMLMock mock = new RollupTestUtils.DMLMock();
Rollup.DML = mock;
Test.startTest();
Rollup.runFromTrigger();
Test.stopTest();
System.assertEquals(1, mock.Records.size(), 'Records should have been updated based on parent metadata AFTER_UPDATE');
Account updatedAcc = (Account) mock.Records[0];
System.assertNotEquals(acc.AnnualRevenue, updatedAcc.AnnualRevenue, 'Account should now have been updated');
System.assertEquals(1, updatedAcc.AnnualRevenue, 'Account should have been updated with asset count');
System.assertEquals(asset.Name, updatedAcc.Name);
}
@IsTest
static void shouldNotFailForTruncatedTextFields() {
Account acc = [SELECT Id FROM Account];
Integer maxAccountNameLength = Account.Name.getDescribe().getLength();
Contact con = new Contact(AccountId = acc.Id, Description = '0'.repeat(maxAccountNameLength + 1), LastName = 'Truncate');
insert con;
Rollup__mdt meta = new Rollup__mdt(
CalcItem__c = 'Contact',
RollupFieldOnCalcItem__c = 'Description',
LookupFieldOnCalcItem__c = 'AccountId',
LookupFieldOnLookupObject__c = 'Id',
RollupFieldOnLookupObject__c = 'Name',
LookupObject__c = 'Account',
RollupOperation__c = 'CONCAT'
);
Test.startTest();
Rollup.performFullRecalculation(meta);
Test.stopTest();
acc = [SELECT Name FROM Account];
System.assertEquals(maxAccountNameLength, acc.Name.length(), acc.Name);
}
@IsTest
static void shouldEnqueueCMDTRecalcWhenBelowQueryLimits() {
Account acc = [SELECT Id FROM Account];
List<ContactPointAddress> cpas = new List<ContactPointAddress>{
new ContactPointAddress(Name = 'oneCMDT', ParentId = acc.Id, PreferenceRank = 1),
new ContactPointAddress(Name = 'twoCMDT', ParentId = acc.Id, PreferenceRank = 1),
new ContactPointAddress(Name = 'threeCMDT', ParentId = acc.Id, PreferenceRank = 1),
new ContactPointAddress(Name = 'fourCMDT', ParentId = acc.Id, PreferenceRank = 1),
new ContactPointAddress(Name = 'fiveCMDT', ParentId = acc.Id, PreferenceRank = 1),
new ContactPointAddress(Name = 'sixCMDT', ParentId = acc.Id, PreferenceRank = 1)
};
insert cpas;
Rollup.rollupMetadata = new List<Rollup__mdt>{
new Rollup__mdt(
CalcItem__c = 'ContactPointAddress',
RollupFieldOnCalcItem__c = 'PreferenceRank',
LookupFieldOnCalcItem__c = 'ParentId',
LookupObject__c = 'Account',
LookupFieldOnLookupObject__c = 'Id',
RollupFieldOnLookupObject__c = 'AnnualRevenue',
RollupOperation__c = 'SUM',
CalcItemWhereClause__c = 'PreferenceRank = ' + cpas[0].PreferenceRank
)
};
Map<String, List<Rollup__mdt>> calcItemToMetadata = Rollup.getRollupMetadataByCalcItem();
Test.startTest();
Rollup.performBulkFullRecalc(calcItemToMetadata.get('ContactPointAddress'), Rollup.InvocationPoint.FROM_FULL_RECALC_LWC.name());
Test.stopTest();
acc = [SELECT AnnualRevenue FROM Account];
System.assertEquals(6, acc.AnnualRevenue);
System.assertEquals('Completed', [SELECT Status FROM AsyncApexJob WHERE JobType = 'Queueable' LIMIT 1]?.Status);
}
@IsTest
static void shouldRunRollupSyncWhenFiredFromParentRecalcInvocation() {
Account acc = [SELECT Id FROM Account];
List<ContactPointAddress> cpas = new List<ContactPointAddress>{
new ContactPointAddress(Name = 'oneCMDTSync', ParentId = acc.Id, PreferenceRank = 1),
new ContactPointAddress(Name = 'twoCMDTSync', ParentId = acc.Id, PreferenceRank = 1),
new ContactPointAddress(Name = 'threeCMDTSync', ParentId = acc.Id, PreferenceRank = 1),
new ContactPointAddress(Name = 'fourCMDTSync', ParentId = acc.Id, PreferenceRank = 1),
new ContactPointAddress(Name = 'fiveCMDTSync', ParentId = acc.Id, PreferenceRank = 1),
new ContactPointAddress(Name = 'sixCMDTSync', ParentId = acc.Id, PreferenceRank = 1)
};
insert cpas;
Rollup.rollupMetadata = new List<Rollup__mdt>{
new Rollup__mdt(
CalcItem__c = 'ContactPointAddress',
RollupFieldOnCalcItem__c = 'PreferenceRank',
LookupFieldOnCalcItem__c = 'ParentId',
LookupObject__c = 'Account',
LookupFieldOnLookupObject__c = 'Id',
RollupFieldOnLookupObject__c = 'AnnualRevenue',
RollupOperation__c = 'SUM',
CalcItemWhereClause__c = 'PreferenceRank = ' + cpas[0].PreferenceRank + ' ||| ParentId = \'' + acc.Id + '\''
)
};
Map<String, List<Rollup__mdt>> calcItemToMetadata = Rollup.getRollupMetadataByCalcItem();
// specifically don't wrap in start/stopTest to prove it ran sync
Rollup.performBulkFullRecalc(calcItemToMetadata.get('ContactPointAddress'), Rollup.InvocationPoint.FROM_SINGULAR_PARENT_RECALC_LWC.name());
acc = [SELECT AnnualRevenue FROM Account];
System.assertEquals(6, acc.AnnualRevenue);
}
@IsTest
static void shouldCorrectlyQueryOrderByFieldForBulkRecalc() {
Account acc = [SELECT Id FROM Account];
List<ContactPointAddress> cpas = new List<ContactPointAddress>{
new ContactPointAddress(Name = 'oneLast', ParentId = acc.Id, PreferenceRank = 2),
new ContactPointAddress(Name = 'twoLast', ParentId = acc.Id, PreferenceRank = 1)
};
insert cpas;
Rollup.rollupMetadata = new List<Rollup__mdt>{
Rollup.appendOrderByMetadata(
new Rollup__mdt(
CalcItem__c = 'ContactPointAddress',
RollupFieldOnCalcItem__c = 'Name',
LookupFieldOnCalcItem__c = 'ParentId',
LookupObject__c = 'Account',
LookupFieldOnLookupObject__c = 'Id',
RollupFieldOnLookupObject__c = 'Name',
RollupOperation__c = 'LAST',
CalcItemWhereClause__c = ' ||| ParentId = \'' + acc.Id + '\''
),
new List<RollupOrderBy__mdt>{ new RollupOrderBy__mdt(FieldName__c = 'PreferenceRank') }
)
};
Map<String, List<Rollup__mdt>> calcItemToMetadata = Rollup.getRollupMetadataByCalcItem();
// specifically don't wrap in start/stopTest to prove it ran sync
Rollup.performBulkFullRecalc(calcItemToMetadata.get('ContactPointAddress'), Rollup.InvocationPoint.FROM_SINGULAR_PARENT_RECALC_LWC.name());
acc = [SELECT Name FROM Account];
System.assertEquals(cpas[0].Name, acc.Name, 'Last should have been selected based on order field');
}
@IsTest
static void shouldCorrectlyBulkRecalcWhenParentObjectsDiffer() {
Account acc = [SELECT Id FROM Account];
List<ContactPointAddress> cpas = new List<ContactPointAddress>{
new ContactPointAddress(Name = 'oneLast', ParentId = acc.Id, PreferenceRank = 2),
new ContactPointAddress(Name = 'twoLast', ParentId = acc.Id, PreferenceRank = 1)
};
insert cpas;
insert new Task(Subject = 'Task rollup', WhatId = acc.Id);
List<Rollup__mdt> metas = new List<Rollup__mdt>{
Rollup.appendOrderByMetadata(
new Rollup__mdt(
CalcItem__c = 'ContactPointAddress',
RollupFieldOnCalcItem__c = 'Name',
LookupFieldOnCalcItem__c = 'ParentId',
LookupObject__c = 'Account',
LookupFieldOnLookupObject__c = 'Id',
RollupFieldOnLookupObject__c = 'Name',
RollupOperation__c = 'LAST',
CalcItemWhereClause__c = ' ||| ParentId = \'' + acc.Id + '\''
),
new List<RollupOrderBy__mdt>{ new RollupOrderBy__mdt(FieldName__c = 'PreferenceRank') }
),
new Rollup__mdt(
CalcItem__c = 'Task',
RollupFieldOnCalcItem__c = 'Subject',
LookupFieldOnCalcItem__c = 'WhatId',
LookupObject__c = 'Account',
LookupFieldOnLookupObject__c = 'Id',
RollupFieldOnLookupObject__c = 'AccountNumber',
RollupOperation__c = 'CONCAT'
)
};
// no start/stopTest since singular parent recalc entry point _should_ run sync
Rollup.performBulkFullRecalc(metas, Rollup.InvocationPoint.FROM_SINGULAR_PARENT_RECALC_LWC.name());
acc = [SELECT Name, AccountNumber FROM Account];
System.assertEquals(cpas[0].Name, acc.Name, 'Last should have been selected based on order field');
System.assertEquals('Task rollup', acc.AccountNumber);
}
@IsTest
static void shouldCorrectlyHandleNotLikeInWhereClause() {
Account acc = [SELECT Id FROM Account];
insert new Task(Subject = 'Task rollup', WhatId = acc.Id);
List<Rollup__mdt> metas = new List<Rollup__mdt>{
new Rollup__mdt(
CalcItem__c = 'Task',
RollupFieldOnCalcItem__c = 'Subject',
LookupFieldOnCalcItem__c = 'WhatId',
LookupObject__c = 'Account',
LookupFieldOnLookupObject__c = 'Id',
RollupFieldOnLookupObject__c = 'AccountNumber',
RollupOperation__c = 'CONCAT',
CalcItemWhereClause__c = '(NOT Subject LIKE \'Email\') ||| WhatId = \'' + acc.Id + '\''
)
};
Rollup.performBulkFullRecalc(metas, Rollup.InvocationPoint.FROM_SINGULAR_PARENT_RECALC_LWC.name());
acc = [SELECT AccountNumber FROM Account];
System.assertEquals('Task rollup', acc.AccountNumber);
}
@IsTest
static void shouldEnqueueFullRecalculationWhenBelowQueryLimits() {
Account acc = [SELECT Id FROM Account];
Account secondAccount = new Account(Name = 'Second');
insert secondAccount;
List<ContactPointAddress> cpas = new List<ContactPointAddress>{
new ContactPointAddress(Name = 'one', ParentId = acc.Id, PreferenceRank = 1),
new ContactPointAddress(Name = 'two', ParentId = acc.Id, PreferenceRank = 1),
new ContactPointAddress(Name = 'three', ParentId = acc.Id, PreferenceRank = 1),
new ContactPointAddress(Name = 'four', ParentId = acc.Id, PreferenceRank = 1),
new ContactPointAddress(Name = 'five', ParentId = acc.Id, PreferenceRank = 1),
new ContactPointAddress(Name = 'six', ParentId = acc.Id, PreferenceRank = 1),
new ContactPointAddress(Name = 'secondAccount', ParentId = secondAccount.Id, PreferenceRank = 1)
};
insert cpas;
Rollup__mdt meta = new Rollup__mdt(
CalcItem__c = 'ContactPointAddress',
RollupFieldOnCalcItem__c = 'PreferenceRank',
LookupFieldOnCalcItem__c = 'ParentId',
LookupFieldOnLookupObject__c = 'Id',
RollupFieldOnLookupObject__c = 'AnnualRevenue',
LookupObject__c = 'Account',
RollupOperation__c = 'SUM'
);
Test.startTest();
Rollup.performFullRecalculation(meta);
Test.stopTest();
List<Account> accounts = [SELECT Id, AnnualRevenue FROM Account];
System.assertEquals(2, accounts.size());
for (Account account : accounts) {
System.assertEquals(account.Id == acc.Id ? 6 : 1, account.AnnualRevenue);
}
System.assertEquals('Completed', [SELECT Status FROM AsyncApexJob WHERE JobType = 'Queueable' LIMIT 1]?.Status);
}
@IsTest
static void shouldBatchForFullRecalcWhenOverLimits() {
Account acc = [SELECT Id FROM Account];
acc.AnnualRevenue = 5; // validate that the pre-existing value is cleared
update acc;
List<ContactPointAddress> cpas = new List<ContactPointAddress>{
new ContactPointAddress(Name = 'oneBatch', ParentId = acc.Id, PreferenceRank = 1),
new ContactPointAddress(Name = 'twoBatch', ParentId = acc.Id, PreferenceRank = 1),
new ContactPointAddress(Name = 'threeBatch', ParentId = acc.Id, PreferenceRank = 1),
new ContactPointAddress(Name = 'fourBatch', ParentId = acc.Id, PreferenceRank = 1),
new ContactPointAddress(Name = 'fiveBatch', ParentId = acc.Id, PreferenceRank = 1),
new ContactPointAddress(Name = 'sixBatch', ParentId = acc.Id, PreferenceRank = 1)
};
insert cpas;
Rollup.defaultControl = new RollupControl__mdt(
// prove that it works with multiple chunks
BatchChunkSize__c = 2,
MaxLookupRowsBeforeBatching__c = 1,
// validate that it still batches
ShouldRunAs__c = RollupMetaPicklists.ShouldRunAs.Queueable,
IsRollupLoggingEnabled__c = true
);
Rollup__mdt meta = new Rollup__mdt(
CalcItem__c = 'ContactPointAddress',
RollupFieldOnCalcItem__c = 'PreferenceRank',
LookupFieldOnCalcItem__c = 'ParentId',
LookupFieldOnLookupObject__c = 'Id',
RollupFieldOnLookupObject__c = 'AnnualRevenue',
LookupObject__c = 'Account',
RollupOperation__c = 'SUM',
CalcItemWhereClause__c = 'Name != \'\''
);
Test.startTest();
Rollup.performFullRecalculation(meta);
Test.stopTest();
acc = [SELECT AnnualRevenue FROM Account];
System.assertEquals(6, acc.AnnualRevenue);
System.assertEquals(
'Completed',
[
SELECT Status
FROM AsyncApexJob
WHERE JobType = 'Queueable' AND ApexClass.Name = :getNamespaceSafeClassName(RollupFullBatchRecalculator.class)
LIMIT 1
]
?.Status
);
}
@IsTest
static void shouldUseSmallerBatchSizeWhenSpecificRollupControlLowerThanOrgDefaults() {
Account acc = [SELECT Id FROM Account];
Account secondAccount = new Account(Name = 'Second');
insert secondAccount;
List<ContactPointAddress> cpas = new List<ContactPointAddress>{
new ContactPointAddress(Name = 'oneBatch', ParentId = acc.Id, PreferenceRank = 1),
new ContactPointAddress(Name = 'twoBatch', ParentId = acc.Id, PreferenceRank = 1),
new ContactPointAddress(Name = 'threeBatch', ParentId = acc.Id, PreferenceRank = 1),
new ContactPointAddress(Name = 'fourBatch', ParentId = acc.Id, PreferenceRank = 1),
new ContactPointAddress(Name = 'fiveBatch', ParentId = acc.Id, PreferenceRank = 1),
new ContactPointAddress(Name = 'sixBatch', ParentId = acc.Id, PreferenceRank = 1),
new ContactPointAddress(Name = 'secondAccount', ParentId = secondAccount.Id, PreferenceRank = 1)
};
insert cpas;
Rollup.defaultControl = new RollupControl__mdt(
MaxLookupRowsBeforeBatching__c = 1,
// validate that it still batches
ShouldRunAs__c = RollupMetaPicklists.ShouldRunAs.Queueable,
IsRollupLoggingEnabled__c = true
);
Rollup.specificControl = new RollupControl__mdt(BatchChunkSize__c = 1);
Rollup__mdt meta = new Rollup__mdt(
CalcItem__c = 'ContactPointAddress',
RollupFieldOnCalcItem__c = 'PreferenceRank',
LookupFieldOnCalcItem__c = 'ParentId',
LookupFieldOnLookupObject__c = 'Id',
RollupFieldOnLookupObject__c = 'AnnualRevenue',
LookupObject__c = 'Account',
RollupOperation__c = 'SUM',
CalcItemWhereClause__c = 'Name != \'\''
);
Test.startTest();
Rollup.performFullRecalculation(meta);
Test.stopTest();
List<AsyncApexJob> jobs = [
SELECT Status
FROM AsyncApexJob
WHERE JobType = 'Queueable' AND ApexClass.Name = :getNamespaceSafeClassName(RollupFullBatchRecalculator.class)
];
System.assertEquals(false, jobs.isEmpty());
for (AsyncApexJob job : jobs) {
System.assertEquals('Completed', job.Status);
}
}