Skip to content

Commit

Permalink
Tidies up #587
Browse files Browse the repository at this point in the history
  • Loading branch information
jamessimone committed May 3, 2024
1 parent 908512e commit 1d522b4
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 165 deletions.
105 changes: 44 additions & 61 deletions extra-tests/classes/RollupCalcItemSorterTests.cls
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ private class RollupCalcItemSorterTests {
Date severalDaysAgo = System.today().addDays(-2);
Opportunity expectedFirstItem = new Opportunity(Amount = null, CloseDate = severalDaysAgo);
Opportunity expectedSecondItem = new Opportunity(Amount = 1, CloseDate = severalDaysAgo);
List<RollupCalculator.WinnowResult> oppsToSort = new List<RollupCalculator.WinnowResult>{
new RollupCalculator.WinnowResult(new Opportunity(Amount = 1, CloseDate = System.today())),
List<Opportunity> oppsToSort = new List<Opportunity>{
new Opportunity(Amount = 1, CloseDate = System.today()),
// this record should essentially be thrown out of sorting since it "loses" on the first ordering,
// which is on Amount
new RollupCalculator.WinnowResult(new Opportunity(Amount = 3, CloseDate = severalDaysAgo.addDays(-1))),
new RollupCalculator.WinnowResult(expectedSecondItem),
new RollupCalculator.WinnowResult(expectedFirstItem)
new Opportunity(Amount = 3, CloseDate = severalDaysAgo.addDays(-1)),
expectedSecondItem,
expectedFirstItem
};
oppsToSort.sort(sorter);

System.assertEquals(expectedFirstItem, oppsToSort[0].item);
System.assertEquals(expectedSecondItem, oppsToSort[1].item);
System.assertEquals(expectedFirstItem, oppsToSort[0]);
System.assertEquals(expectedSecondItem, oppsToSort[1]);
}

@IsTest
Expand All @@ -41,18 +41,13 @@ private class RollupCalcItemSorterTests {
Opportunity expectedThirdItem = new Opportunity(Amount = 2, CloseDate = today, Name = 'a');
Opportunity expectedSecondItem = new Opportunity(Amount = 1, CloseDate = today, Name = 'c');
Opportunity expectedFourthItem = new Opportunity(Amount = 2, CloseDate = today.addDays(1), Name = 'a');
List<RollupCalculator.WinnowResult> oppsToSort = new List<RollupCalculator.WinnowResult>{
new RollupCalculator.WinnowResult(expectedSecondItem),
new RollupCalculator.WinnowResult(expectedFourthItem),
new RollupCalculator.WinnowResult(expectedThirdItem),
new RollupCalculator.WinnowResult(expectedFirstItem)
};
List<Opportunity> oppsToSort = new List<Opportunity>{ expectedSecondItem, expectedFourthItem, expectedThirdItem, expectedFirstItem };
oppsToSort.sort(sorter);

System.assertEquals(expectedFirstItem, oppsToSort[0].item);
System.assertEquals(expectedSecondItem, oppsToSort[1].item);
System.assertEquals(expectedThirdItem, oppsToSort[2].item);
System.assertEquals(expectedFourthItem, oppsToSort[3].item);
System.assertEquals(expectedFirstItem, oppsToSort[0]);
System.assertEquals(expectedSecondItem, oppsToSort[1]);
System.assertEquals(expectedThirdItem, oppsToSort[2]);
System.assertEquals(expectedFourthItem, oppsToSort[3]);
}

@IsTest
Expand All @@ -67,15 +62,15 @@ private class RollupCalcItemSorterTests {
Date severalDaysAgo = System.today().addDays(-2);
Opportunity expectedFirstItem = new Opportunity(Amount = 1, CloseDate = System.today());
Opportunity expectedSecondItem = new Opportunity(Amount = 3, CloseDate = severalDaysAgo.addDays(-1));
List<RollupCalculator.WinnowResult> oppsToSort = new List<RollupCalculator.WinnowResult>{
new RollupCalculator.WinnowResult(new Opportunity(Amount = 3, CloseDate = severalDaysAgo.addDays(-1))),
new RollupCalculator.WinnowResult(expectedSecondItem),
new RollupCalculator.WinnowResult(expectedFirstItem)
List<Opportunity> oppsToSort = new List<Opportunity>{
new Opportunity(Amount = 3, CloseDate = severalDaysAgo.addDays(-1)),
expectedSecondItem,
expectedFirstItem
};
oppsToSort.sort(sorter);

System.assertEquals(expectedFirstItem, oppsToSort[0].item);
System.assertEquals(expectedSecondItem, oppsToSort[1].item);
System.assertEquals(expectedFirstItem, oppsToSort[0]);
System.assertEquals(expectedSecondItem, oppsToSort[1]);
}

@IsTest
Expand All @@ -93,33 +88,29 @@ private class RollupCalcItemSorterTests {
Opportunity expectedSecondItem = new Opportunity(Amount = 5, CloseDate = today);
Opportunity expectedThirdItem = new Opportunity(Amount = 1, CloseDate = today);

List<RollupCalculator.WinnowResult> oppsToSort = new List<RollupCalculator.WinnowResult>{
new RollupCalculator.WinnowResult(expectedThirdItem),
new RollupCalculator.WinnowResult(expectedFirstItem),
new RollupCalculator.WinnowResult(expectedSecondItem)
};
List<Opportunity> oppsToSort = new List<Opportunity>{ expectedThirdItem, expectedFirstItem, expectedSecondItem };
oppsToSort.sort(sorter);

System.assertEquals(expectedFirstItem, oppsToSort[0].item);
System.assertEquals(expectedSecondItem, oppsToSort[1].item);
System.assertEquals(expectedThirdItem, oppsToSort[2].item);
System.assertEquals(expectedFirstItem, oppsToSort[0]);
System.assertEquals(expectedSecondItem, oppsToSort[1]);
System.assertEquals(expectedThirdItem, oppsToSort[2]);
}

@IsTest
static void shouldProperlySortPicklists() {
RollupCalcItemSorter sorter = new RollupCalcItemSorter(new List<RollupOrderBy__mdt>{ new RollupOrderBy__mdt(Ranking__c = 0, FieldName__c = 'Industry') });

List<Schema.PicklistEntry> picklistEntries = Account.Industry.getDescribe().getPicklistValues();
List<RollupCalculator.WinnowResult> accs = new List<RollupCalculator.WinnowResult>();
List<Account> accs = new List<Account>();

for (Integer reverseIndex = picklistEntries.size() - 1; reverseIndex >= 0; reverseIndex--) {
Schema.PicklistEntry entry = picklistEntries[reverseIndex];
accs.add(new RollupCalculator.WinnowResult(new Account(Name = entry.getValue(), Industry = entry.getValue())));
accs.add(new Account(Name = entry.getValue(), Industry = entry.getValue()));
}
accs.sort(sorter);

for (Integer index = 0; index < accs.size(); index++) {
System.assertEquals(picklistEntries[index].getValue(), accs[index].item.get(Account.Industry), 'Account at index: ' + index + ' should have matched');
System.assertEquals(picklistEntries[index].getValue(), accs[index].Industry, 'Account at index: ' + index + ' should have matched');
}
}

Expand All @@ -138,14 +129,11 @@ private class RollupCalcItemSorterTests {
}
);

List<RollupCalculator.WinnowResult> opps = new List<RollupCalculator.WinnowResult>{
new RollupCalculator.WinnowResult(second),
new RollupCalculator.WinnowResult(opp)
};
List<Opportunity> opps = new List<Opportunity>{ second, opp };
opps.sort(sorter);

System.assertEquals(opp, opps[0].item);
System.assertEquals(second, opps[1].item);
System.assertEquals(opp, opps[0]);
System.assertEquals(second, opps[1]);
}

@IsTest
Expand All @@ -156,37 +144,32 @@ private class RollupCalcItemSorterTests {

Opportunity expectedFirstItem = new Opportunity(Amount = null);
Opportunity expectedSecondItem = new Opportunity(Amount = 1);
List<RollupCalculator.WinnowResult> oppsToSort = new List<RollupCalculator.WinnowResult>{
new RollupCalculator.WinnowResult(new Opportunity(Amount = 1)),
new RollupCalculator.WinnowResult(new Opportunity(Amount = 3)),
new RollupCalculator.WinnowResult(expectedSecondItem),
new RollupCalculator.WinnowResult(expectedFirstItem)
};
List<Opportunity> oppsToSort = new List<Opportunity>{ new Opportunity(Amount = 1), new Opportunity(Amount = 3), expectedSecondItem, expectedFirstItem };
oppsToSort.sort(sorter);

System.assert(true, 'Should make it here');
}

@IsTest
static void sortsFieldNames() {
List<RollupCalculator.WinnowResult> itemsToSort = new List<RollupCalculator.WinnowResult>{
new RollupCalculator.WinnowResult(new Opportunity(StageName = 'Two')),
new RollupCalculator.WinnowResult(new Opportunity(StageName = 'Uno Reverse Card')),
new RollupCalculator.WinnowResult(new Opportunity(StageName = 'Two')),
new RollupCalculator.WinnowResult(new Opportunity()),
new RollupCalculator.WinnowResult(new Opportunity(StageName = 'Z')),
new RollupCalculator.WinnowResult(new Opportunity(StageName = 'One')),
new RollupCalculator.WinnowResult(new Opportunity(StageName = 'One'))
List<Opportunity> itemsToSort = new List<Opportunity>{
new Opportunity(StageName = 'Two'),
new Opportunity(StageName = 'Uno Reverse Card'),
new Opportunity(StageName = 'Two'),
new Opportunity(),
new Opportunity(StageName = 'Z'),
new Opportunity(StageName = 'One'),
new Opportunity(StageName = 'One')
};

itemsToSort.sort(new RollupCalcItemSorter(new List<String>{ Opportunity.Name.getDescribe().getName(), Opportunity.StageName.getDescribe().getName() }));

System.assertEquals(null, itemsToSort.get(0).item.get(Opportunity.StageName));
System.assertEquals('One', itemsToSort.get(1).item.get(Opportunity.StageName));
System.assertEquals('One', itemsToSort.get(2).item.get(Opportunity.StageName));
System.assertEquals('Two', itemsToSort.get(3).item.get(Opportunity.StageName));
System.assertEquals('Two', itemsToSort.get(4).item.get(Opportunity.StageName));
System.assertEquals('Uno Reverse Card', itemsToSort.get(5).item.get(Opportunity.StageName));
System.assertEquals('Z', itemsToSort.get(6).item.get(Opportunity.StageName));
System.assertEquals(null, itemsToSort.get(0).StageName);
System.assertEquals('One', itemsToSort.get(1).StageName);
System.assertEquals('One', itemsToSort.get(2).StageName);
System.assertEquals('Two', itemsToSort.get(3).StageName);
System.assertEquals('Two', itemsToSort.get(4).StageName);
System.assertEquals('Uno Reverse Card', itemsToSort.get(5).StageName);
System.assertEquals('Z', itemsToSort.get(6).StageName);
}
}
22 changes: 15 additions & 7 deletions extra-tests/classes/RollupCalculatorTests.cls
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@SuppressWarnings('PMD.NcssTypeCount')
@IsTest
private class RollupCalculatorTests {
// use these tests when DML is not required, or only *light* DML is necessary
Expand Down Expand Up @@ -793,18 +794,23 @@ private class RollupCalculatorTests {

@IsTest
static void sumsDistinctWhenFlagged() {
Id stubParentId = RollupTestUtils.createId(Schema.Account.SObjectType);
Object parentStartingValue = 0;
RollupCalculator calc = getCalculator(
0,
parentStartingValue,
Rollup.Op.SUM,
Opportunity.Amount,
Account.AnnualRevenue,
new Rollup__mdt(IsDistinct__c = true),
'0011g00003VDGbF002',
stubParentId,
Opportunity.AccountId
);

calc.performRollup(
new List<Opportunity>{ new Opportunity(Id = '0066g00003VDGbF001', Amount = 2), new Opportunity(Id = '0066g00003VDGbF002', Amount = 2) },
new List<Opportunity>{
new Opportunity(Id = RollupTestUtils.createId(Schema.Opportunity.SObjectType), Amount = 2, AccountId = stubParentId),
new Opportunity(Id = RollupTestUtils.createId(Schema.Opportunity.SObjectType), Amount = 2, AccountId = stubParentId)
},
new Map<Id, SObject>()
);

Expand Down Expand Up @@ -927,20 +933,22 @@ private class RollupCalculatorTests {
@IsTest
static void shouldDistinctByFlagForConcat() {
String distinct = 'distinct';
Object parentStartingValue = '';
Id stubParentId = RollupTestUtils.createId(Schema.Account.SObjectType);
RollupCalculator calc = getCalculator(
'',
parentStartingValue,
Rollup.Op.CONCAT,
Opportunity.Name,
Account.Name,
new Rollup__mdt(IsDistinct__c = true),
'0011g00003VDGbF002',
stubParentId,
Opportunity.AccountId
);

calc.performRollup(
new List<Opportunity>{
new Opportunity(Id = '0066g00003VDGbF001', Name = distinct, AccountId = '0016g00003VDGbF001'),
new Opportunity(Id = '0066g00003VDGbF002', Name = distinct, AccountId = '0016g00003VDGbF001')
new Opportunity(Id = RollupTestUtils.createId(Schema.Opportunity.SObjectType), Name = distinct, AccountId = stubParentId),
new Opportunity(Id = RollupTestUtils.createId(Schema.Opportunity.SObjectType), Name = distinct, AccountId = stubParentId)
},
new Map<Id, SObject>()
);
Expand Down
8 changes: 4 additions & 4 deletions rollup/core/classes/RollupCalcItemSorter.cls
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public without sharing class RollupCalcItemSorter implements System.Comparator<RollupCalculator.WinnowResult> {
public without sharing class RollupCalcItemSorter implements System.Comparator<SObject> {
private final List<RollupOrderBy__mdt> orderByOptions;
private final Map<Schema.DescribeFieldResult, RollupFieldInitializer.PicklistController> fieldToPicklistController;

Expand All @@ -11,18 +11,18 @@ public without sharing class RollupCalcItemSorter implements System.Comparator<R
this.orderByOptions = this.initializeOrderByOptions(orderBys);
}

public Integer compare(RollupCalculator.WinnowResult objOne, RollupCalculator.WinnowResult objTwo) {
public Integer compare(SObject objOne, SObject objTwo) {
Integer comparisonVal = 0;

Map<String, SObjectField> fieldTokens = objOne.item.getSObjectType().getDescribe().fields.getMap();
Map<String, SObjectField> fieldTokens = objOne.getSObjectType().getDescribe().fields.getMap();
for (RollupOrderBy__mdt orderByOption : this.orderByOptions) {
Schema.DescribeFieldResult fieldDescribe = fieldTokens.get(orderByOption.FieldName__c)?.getDescribe();
RollupFieldInitializer.PicklistController picklistController = this.fieldToPicklistController.get(fieldDescribe);
if (picklistController == null) {
picklistController = new RollupFieldInitializer.PicklistController(fieldDescribe);
this.fieldToPicklistController.put(fieldDescribe, picklistController);
}
comparisonVal = this.getSortRanking(objOne.item, objTwo.item, orderByOption, picklistController);
comparisonVal = this.getSortRanking(objOne, objTwo, orderByOption, picklistController);
if (comparisonVal != 0) {
return comparisonVal;
}
Expand Down
Loading

0 comments on commit 1d522b4

Please sign in to comment.