forked from jamessimone/apex-dml-mocking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AggregateRepositoryTests.cls
112 lines (101 loc) · 4.05 KB
/
AggregateRepositoryTests.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
@IsTest
private class AggregateRepositoryTests {
@IsTest
static void shouldAggregateSum() {
Account parent = new Account(Name = AggregateRepositoryTests.class.getName());
Account secondParent = new Account(Name = 'Second parent');
insert new List<Account>{ parent, secondParent };
Opportunity opp = new Opportunity(
Name = 'opp',
Amount = 1,
AccountId = parent.Id,
StageName = 'sum',
CloseDate = System.today()
);
Opportunity secondOpp = new Opportunity(
Name = 'opp2',
Amount = 1,
AccountId = secondParent.Id,
StageName = 'sum',
CloseDate = System.today()
);
Opportunity anotherSecondParentMatch = new Opportunity(
Name = 'opp3',
Amount = 1,
AccountId = secondParent.Id,
StageName = 'sum',
CloseDate = System.today()
);
insert new List<Opportunity>{ opp, secondOpp, anotherSecondParentMatch };
Aggregation sum = Aggregation.sum(Opportunity.Amount, 'oppSum');
IAggregateRepository repo = new AggregateRepository(
Opportunity.SObjectType,
new List<SObjectField>{ Opportunity.AccountId, Opportunity.Id, Opportunity.Amount },
new RepoFactory()
);
repo.groupBy(Opportunity.AccountId);
List<AggregateRecord> results = repo.aggregate(sum);
System.assertEquals(2, results?.size());
for (AggregateRecord res : results) {
if (res.get('AccountId') == secondParent.Id) {
System.assertEquals(2, res.get(sum.getAlias()));
} else {
System.assertEquals(1, res.get(sum.getAlias()));
}
}
}
@IsTest
static void shouldReturnCountOnFieldNameCorrectly() {
insert new List<Opportunity>{
new Opportunity(Name = 'opp', StageName = 'sum', CloseDate = System.today()),
new Opportunity(Name = 'opp2', Amount = 1, StageName = 'sum', CloseDate = System.today())
};
IAggregateRepository repo = new AggregateRepository(
Opportunity.SObjectType,
new List<SObjectField>{ Opportunity.AccountId, Opportunity.Id, Opportunity.Amount },
new RepoFactory()
);
Aggregation countOfAmount = Aggregation.count(Opportunity.Amount, 'wowza');
List<AggregateRecord> results = repo.aggregate(countOfAmount);
System.assertEquals(1, results.size());
System.assertEquals(1, results[0].get(countOfAmount.getAlias()));
}
@IsTest
static void shouldReturnCountAsInteger() {
insert new List<Opportunity>{
new Opportunity(Name = 'opp', StageName = 'sum', CloseDate = System.today()),
new Opportunity(Name = 'opp2', Amount = 1, StageName = 'sum', CloseDate = System.today())
};
IAggregateRepository repo = new AggregateRepository(
Opportunity.SObjectType,
new List<SObjectField>{ Opportunity.AccountId, Opportunity.Id, Opportunity.Amount },
new RepoFactory()
);
System.assertEquals(2, repo.count());
}
@IsTest
static void mocksAggregateResultsSuccessfully() {
Aggregation countOfAmount = Aggregation.count(Opportunity.Amount, 'wowza');
Aggregation sum = Aggregation.sum(Opportunity.Amount, 'oppSum');
String accountKey = 'AccountId';
Map<String, Object> mockAggregateResult = new Map<String, Object>{
countOfAmount.getAlias() => 5,
sum.getAlias() => 10,
accountKey => TestingUtils.generateId(Account.SObjectType)
};
AggregateRecord res = new AggregateRecord().putAll(mockAggregateResult);
RepoFactoryMock.AggregateResults.put(Opportunity.SObjectType, new List<AggregateRecord>{ res });
List<AggregateRecord> results = new RepoFactoryMock()
.getOppRepo()
.groupBy(Opportunity.AccountId)
.aggregate(new List<Aggregation>{ countOfAmount, sum });
System.assertEquals(1, results.size());
AggregateRecord returnedResult = results.get(0);
System.assertEquals(mockAggregateResult.get(accountKey), returnedResult.get(accountKey));
System.assertEquals(
mockAggregateResult.get(countOfAmount.getAlias()),
returnedResult.get(countOfAmount.getAlias())
);
System.assertEquals(mockAggregateResult.get(sum.getAlias()), returnedResult.get(sum.getAlias()));
}
}