Skip to content

Commit

Permalink
Issue SFDO-Community#239 - test to demonstrate shared context default…
Browse files Browse the repository at this point in the history
… ordering issue
  • Loading branch information
jondavis9898 committed Aug 29, 2015
1 parent 98ee4da commit 84caee2
Showing 1 changed file with 126 additions and 0 deletions.
126 changes: 126 additions & 0 deletions rolluptool/src/classes/RollupServiceTest4.cls
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,132 @@ private class RollupServiceTest4 {
System.assertEquals(42, (Decimal) assertParents.get(parentC.id).get(aggregateResultField2));
}

private testmethod static void testLimitsAndContextsUsedMultipleQueryRollupsDifferByOperationFieldAndCaseSameCriteriaSameCaseNoOrderBy()
{
// Test supported?
if(!TestContext.isSupported())
return;

Schema.SObjectType parentType = LookupParent__c.sObjectType;
Schema.SObjectType childType = LookupChild__c.sObjectType;
String parentObjectName = parentType.getDescribe().getName();
String childObjectName = childType.getDescribe().getName();
String relationshipField = LookupChild__c.LookupParent__c.getDescribe().getName();
String aggregateField1 = LookupChild__c.Color__c.getDescribe().getName();
String aggregateField2 = LookupChild__c.Amount__c.getDescribe().getName();
String aggregateResultField1 = LookupParent__c.Colours__c.getDescribe().getName();
String aggregateResultField2 = LookupParent__c.Total2__c.getDescribe().getName();
String condition = 'Amount__c > 1';
String relationshipCriteriaFields = 'Amount__c';
String sharingMode = LREngine.SharingMode.User.name();
String fieldToOrderBy = null;

// Configure rollups
LookupRollupSummary__c rollupSummaryA = new LookupRollupSummary__c();
rollupSummaryA.Name = 'Test Rollup A';
rollupSummaryA.ParentObject__c = parentObjectName.toLowerCase();
rollupSummaryA.ChildObject__c = childObjectName.toLowerCase();
rollupSummaryA.RelationShipField__c = relationshipField.toLowerCase();
rollupSummaryA.RelationShipCriteria__c = condition;
rollupSummaryA.RelationShipCriteriaFields__c = relationshipCriteriaFields;
rollupSummaryA.FieldToAggregate__c = aggregateField1.toLowerCase();
rollupSummaryA.FieldToOrderBy__c = fieldToOrderBy;
rollupSummaryA.AggregateOperation__c = RollupSummaries.AggregateOperation.First.name();
rollupSummaryA.AggregateResultField__c = aggregateResultField1.toLowerCase();
rollupSummaryA.Active__c = true;
rollupSummaryA.CalculationMode__c = RollupSummaries.CalculationMode.Realtime.name();
rollupSummaryA.CalculationSharingMode__c = sharingMode.toLowerCase();

LookupRollupSummary__c rollupSummaryB = new LookupRollupSummary__c();
rollupSummaryB.Name = 'Test Rollup B';
rollupSummaryB.ParentObject__c = parentObjectName;
rollupSummaryB.ChildObject__c = childObjectName;
rollupSummaryB.RelationShipField__c = relationshipField;
rollupSummaryB.RelationShipCriteria__c = condition;
rollupSummaryB.RelationShipCriteriaFields__c = relationshipCriteriaFields;
rollupSummaryB.FieldToAggregate__c = aggregateField2;
rollupSummaryB.FieldToOrderBy__c = fieldToOrderBy;
rollupSummaryB.AggregateOperation__c = RollupSummaries.AggregateOperation.Last.name();
rollupSummaryB.AggregateResultField__c = aggregateResultField2;
rollupSummaryB.Active__c = true;
rollupSummaryB.CalculationMode__c = RollupSummaries.CalculationMode.Realtime.name();
rollupSummaryB.CalculationSharingMode__c = sharingMode;

List<LookupRollupSummary__c> rollups = new List<LookupRollupSummary__c> { rollupSummaryA, rollupSummaryB };
insert rollups;

// Insert parents
SObject parentA = parentType.newSObject();
parentA.put('Name', 'ParentA');
SObject parentB = parentType.newSObject();
parentB.put('Name', 'ParentB');
SObject parentC = parentType.newSObject();
parentC.put('Name', 'ParentC');
List<SObject> parents = new List<SObject> { parentA, parentB, parentC };
insert parents;

// Insert children
List<SObject> children = new List<SObject>();
for(SObject parent : parents)
{
SObject child1 = childType.newSObject();
child1.put(relationshipField, parent.Id);
child1.put(aggregateField1, 'orange');
child1.put(aggregateField2, 42);
children.add(child1);
SObject child2 = childType.newSObject();
child2.put(relationshipField, parent.Id);
child2.put(aggregateField1, 'purple');
child2.put(aggregateField2, 15);
children.add(child2);
SObject child3 = childType.newSObject();
child3.put(relationshipField, parent.Id);
child3.put(aggregateField1, 'cyan');
child3.put(aggregateField2, 10);
children.add(child3);
}

// Sample various limits prior to an update
Integer beforeQueries = Limits.getQueries();
Integer beforeRows = Limits.getQueryRows();
Integer beforeDMLRows = Limits.getDMLRows();

insert children;

// Assert limits
// + One query on Rollup object
// + One query on LookupChild__c for rollup
System.assertEquals(beforeQueries + 2, Limits.getQueries());

// + Two rows for Rollup object
// + Nine rows for LookupChild__c
System.assertEquals(beforeRows + 11, Limits.getQueryRows());

// + Nine rows for LookupChild__c (from the update statement itself)
// + Three rows for LookupParent__c (from rollup processing)
System.assertEquals(beforeDMLRows + 12, Limits.getDMLRows());

// Assert rollups
Map<Id, SObject> assertParents = new Map<Id, SObject>(Database.query(String.format('select id, {0}, {1} from {2}', new List<String>{ aggregateResultField1, aggregateResultField2, parentObjectName })));
System.assertEquals('cyan', (String) assertParents.get(parentA.id).get(aggregateResultField1));
// This assert will fail as the actual results as 15 currently because the order by is Color__c, Amount__c
// even though the documentation states that when no order by is specified, the default ordering will be
// field to aggregate
System.assertEquals(42, (Decimal) assertParents.get(parentA.id).get(aggregateResultField2));

System.assertEquals('cyan', (String) assertParents.get(parentB.id).get(aggregateResultField1));
// This assert will fail as the actual results as 15 currently because the order by is Color__c, Amount__c
// even though the documentation states that when no order by is specified, the default ordering will be
// field to aggregate
System.assertEquals(42, (Decimal) assertParents.get(parentB.id).get(aggregateResultField2));

System.assertEquals('cyan', (String) assertParents.get(parentC.id).get(aggregateResultField1));
// This assert will fail as the actual results as 15 currently because the order by is Color__c, Amount__c
// even though the documentation states that when no order by is specified, the default ordering will be
// field to aggregate
System.assertEquals(42, (Decimal) assertParents.get(parentC.id).get(aggregateResultField2));
}

/**
* Test for issue https://github.com/afawcett/declarative-lookup-rollup-summaries/issues/229
* Two similar rollups
Expand Down

0 comments on commit 84caee2

Please sign in to comment.