Skip to content

Commit 870450f

Browse files
committed
Fix Unit Tests
1 parent 67f3569 commit 870450f

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
lines changed

force-app/main/default/classes/main/cached-soql/SOQLCache.cls

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,17 @@ public virtual inherited sharing class SOQLCache implements Cacheable {
6767
// Mocking
6868

6969
public interface Mockable {
70-
void thenReturn(SObject record);
70+
// SObject
71+
Mockable thenReturn(SObject record);
7172
}
7273

7374
@TestVisible
7475
private static Mockable mock(String mockId) {
75-
queryIdToMock.put(mockId, new SoqlMock());
76-
return queryIdToMock.get(mockId);
76+
if (!SOQLCache.queryIdToMock.containsKey(mockId)) {
77+
SOQLCache.queryIdToMock.put(mockId, new List<SoqlMock>());
78+
}
79+
SOQLCache.queryIdToMock.get(mockId).add(new SoqlMock());
80+
return SOQLCache.queryIdToMock.get(mockId).get(SOQLCache.queryIdToMock.get(mockId).size() - 1);
7781
}
7882

7983
public static void removeFromCache(List<SObject> records) {
@@ -93,12 +97,13 @@ public virtual inherited sharing class SOQLCache implements Cacheable {
9397

9498
@TestVisible // deprecated
9599
private static void setMock(String mockId, SObject record) {
96-
mock(mockId).thenReturn(record);
100+
SOQLCache.queryIdToMock.put(mockId, new List<SoqlMock>{ new SoqlMock() });
101+
SOQLCache.queryIdToMock.get(mockId).get(SOQLCache.queryIdToMock.get(mockId).size() - 1).thenReturn(record);
97102
}
98103

99104
// Implementation
100105

101-
private static Map<String, SoqlMock> queryIdToMock = new Map<String, SoqlMock>();
106+
private static Map<String, List<SoqlMock>> queryIdToMock = new Map<String, List<SoqlMock>>();
102107

103108
private Executor executor;
104109
private Cache cache;
@@ -517,10 +522,12 @@ public virtual inherited sharing class SOQLCache implements Cacheable {
517522
}
518523

519524
public class SoqlMock implements Mockable {
520-
private List<SObject> mockedRecords = null;
525+
private List<SObject> mockedRecords = new List<SObject>();
526+
private Integer countMock;
521527

522-
public void thenReturn(SObject record) {
523-
this.mockedRecords = new List<SObject>{ record };
528+
public Mockable thenReturn(SObject record) {
529+
this.mockedRecords.add(record);
530+
return this;
524531
}
525532

526533
public List<SObject> getMockedResult() {
@@ -532,19 +539,19 @@ public virtual inherited sharing class SOQLCache implements Cacheable {
532539
private SOQL.Queryable currentyQuery;
533540
private Cache cache;
534541
private AccessType accessType = null;
535-
private SoqlMock mock;
542+
private List<SoqlMock> mocks = new List<SoqlMock>();
536543

537544
public Executor(SOQL.Queryable currentyQuery, Cache cache) {
538545
this.currentyQuery = currentyQuery;
539546
this.cache = cache;
540547
}
541548

542549
public void stripInaccessible(AccessType type) {
543-
accessType = type;
550+
this.accessType = type;
544551
}
545552

546-
public void mock(SoqlMock mock) {
547-
this.mock = mock;
553+
public void mock(List<SoqlMock> mocks) {
554+
this.mocks = mocks ?? new List<SoqlMock>();
548555
}
549556

550557
public SObject toObject() {
@@ -562,8 +569,8 @@ public virtual inherited sharing class SOQLCache implements Cacheable {
562569
}
563570

564571
private List<SObject> toList() {
565-
if (this.mock != null) {
566-
return this.mock.getMockedResult();
572+
if (!this.mocks.isEmpty()) {
573+
return this.getMockedList();
567574
}
568575

569576
List<SObject> records = this.cache.toList();
@@ -583,6 +590,13 @@ public virtual inherited sharing class SOQLCache implements Cacheable {
583590

584591
return Security.stripInaccessible(this.accessType, records).getRecords();
585592
}
593+
594+
private List<SObject> getMockedList() {
595+
if (this.mocks.size() == 1) {
596+
return this.mocks[0].getMockedResult();
597+
}
598+
return this.mocks.remove(0).getMockedResult();
599+
}
586600
}
587601

588602
public class SoqlCacheException extends Exception {}

force-app/main/default/classes/main/cached-soql/SOQLCache_Test.cls

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ private class SOQLCache_Test {
8787
@IsTest
8888
static void cacheInOrgCache() {
8989
// Test
90-
Profile profile = (Profile) SOQLCache.of(Profile.SObjectType)
90+
SOQLCache.of(Profile.SObjectType)
9191
.cacheInOrgCache()
9292
.with(Profile.Id, Profile.Name)
9393
.whereEqual(Profile.Name, 'System Administrator')
@@ -105,7 +105,7 @@ private class SOQLCache_Test {
105105
@IsTest
106106
static void cacheInSessionCache() {
107107
// Test
108-
Profile profile = (Profile) SOQLCache.of(Profile.SObjectType)
108+
SOQLCache.of(Profile.SObjectType)
109109
.cacheInSessionCache()
110110
.with(Profile.Id, Profile.Name)
111111
.whereEqual(Profile.Name, 'System Administrator')

force-app/main/default/classes/main/standard-soql/SOQL.cls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2533,7 +2533,7 @@ public virtual inherited sharing class SOQL implements Queryable {
25332533
}
25342534

25352535
public void mock(List<SoqlMock> mocks) {
2536-
this.mocks = mocks;
2536+
this.mocks = mocks ?? new List<SoqlMock>();
25372537
}
25382538

25392539
public SObject toObject() {

0 commit comments

Comments
 (0)