Skip to content

Commit

Permalink
Adds count() override for mocking and getCount() method on AggregateR…
Browse files Browse the repository at this point in the history
…ecord
  • Loading branch information
jamessimone committed Apr 24, 2024
1 parent 194114e commit b45ba98
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
14 changes: 14 additions & 0 deletions force-app/factory/RepoFactoryMock.cls
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,28 @@ public class RepoFactoryMock {

public override List<SObject> get(List<Query> queries) {
QueriesMade.addAll(queries);
this.clearState();
return this.results;
}

public override List<List<SObject>> getSosl(String searchTerm, List<Query> queries) {
QueriesMade.addAll(queries);
this.clearState();
return new List<List<SObject>>{ this.results };
}

public override Integer count(List<Query> queries) {
QueriesMade.addAll(queries);
List<AggregateRecord> results = AggregateResults.get(this.repoType);
this.clearState();
if (results == null) {
return super.count(queries);
} else if (results.isEmpty() == false) {
return results.remove(0).getCount();
}
return null;
}

public override List<AggregateRecord> aggregate(List<Aggregation> aggregations, List<Query> queries) {
AggregatesMade.addAll(aggregations);
QueriesMade.addAll(queries);
Expand Down
10 changes: 10 additions & 0 deletions force-app/repository/AggregateRecord.cls
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
public class AggregateRecord {
private final Map<String, Object> keyToAggregateResult = new Map<String, Object>();
private static final String COUNT_KEY = 'countKey';

public AggregateRecord putAll(Map<String, Object> values) {
this.keyToAggregateResult.putAll(values);
Expand All @@ -10,6 +11,15 @@ public class AggregateRecord {
return this.keyToAggregateResult.get(key);
}

public Integer getCount() {
return (Integer) this.keyToAggregateResult.get(COUNT_KEY);
}

public AggregateRecord setCount(Integer countAmount) {
this.keyToAggregateResult.put(COUNT_KEY, countAmount);
return this;
}

public Boolean equals(Object that) {
if (that instanceof AggregateResult) {
Map<String, Object> thatKeyToAggregateResult = ((AggregateResult) that).getPopulatedFieldsAsMap();
Expand Down
2 changes: 1 addition & 1 deletion force-app/repository/AggregateRepository.cls
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public without sharing virtual class AggregateRepository extends Repository impl
public Integer count(Query query) {
return this.count(new List<Query>{ query });
}
public Integer count(List<Query> queries) {
public virtual Integer count(List<Query> queries) {
this.isNumberCountQuery = true;
Integer recordCount = Database.countQueryWithBinds(this.getFinalQuery(queries), this.bindVars, this.accessLevel);
this.clearState();
Expand Down

0 comments on commit b45ba98

Please sign in to comment.