-
Notifications
You must be signed in to change notification settings - Fork 22
/
AggregateRepositoryTests.cls
165 lines (149 loc) · 6.32 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
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
@IsTest
private class AggregateRepositoryTests {
@IsTest
static void shouldAggregateSum() {
Account parent = new Account(Name = AggregateRepositoryTests.class.getName(), NumberOfEmployees = 1);
Account secondParent = new Account(Name = 'Second parent', NumberOfEmployees = 1);
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()));
}
}
System.assertEquals(
1,
repo.groupBy(new List<Schema.SObjectField>{ Opportunity.AccountId, Account.NumberOfEmployees })
.aggregate(sum)
.size()
);
}
@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()));
System.assertEquals(
1,
repo.addHaving(countOfAmount, Query.Operator.GREATER_THAN, 0)
.groupBy(Opportunity.CloseDate)
.aggregate(countOfAmount)[0]
.get(countOfAmount.getAlias())
);
// prove equality works
System.assert(results.get(0).equals([SELECT COUNT(Amount) wowza FROM Opportunity].get(0)));
System.assertEquals(results.get(0), results.get(0));
}
@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 RepoFactory()
.setFacade(new RepoFactoryMock.FacadeMock())
.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()));
}
@IsTest
static void shouldOrderByDateFunction() {
IAggregateRepository cpaRepo = new AggregateRepository(
ContactPointAddress.SObjectType,
new List<SObjectField>{ ContactPointAddress.Name, ContactPointAddress.ActiveFromDate },
new RepoFactory()
);
insert new List<ContactPointAddress>{
new ContactPointAddress(Name = 'A', ActiveFromDate = System.today()),
new ContactPointAddress(Name = 'B', ActiveFromDate = System.today().addYears(-2)),
new ContactPointAddress(Name = 'C', ActiveFromDate = System.today().addYears(2)),
new ContactPointAddress(Name = 'D', ActiveFromDate = System.today().addYears(5)) // should be excluded through limit
};
String nameAlias = 'name';
cpaRepo.setLimit(3);
List<AggregateRecord> results = cpaRepo.groupBy(
DateFunction.CALENDAR_YEAR,
ContactPointAddress.ActiveFromDate,
'activeDate'
)
.addSortOrder(DateFunction.CALENDAR_YEAR, ContactPointAddress.ActiveFromDate, RepositorySortOrder.ASCENDING)
.aggregate(Aggregation.max(ContactPointAddress.Name, nameAlias));
Assert.areEqual(3, results.size());
Assert.isNotNull(results.get(0).get('activeDate'));
Assert.areEqual('B', results.get(0).get(nameAlias));
Assert.areEqual('A', results.get(1).get(nameAlias));
Assert.areEqual('C', results.get(2).get(nameAlias));
Aggregation maxName = Aggregation.max(ContactPointAddress.Name, 'maxName');
AggregateRecord result = cpaRepo.addSortOrder(maxName, RepositorySortOrder.DESCENDING).aggregate(maxName).get(0);
Assert.areEqual('D', result.get(maxName.getAlias()));
}
}