Skip to content

Commit 3f82f67

Browse files
authored
calculate total number of queries issued [DRAFT] (#134)
* calculate total number of queries issued Signed-off-by: Piotr PG Gajek <[email protected]> * queryIssued Signed-off-by: Piotr PG Gajek <[email protected]> * QueryException Signed-off-by: Piotr PG Gajek <[email protected]> * synchronous query issued count Signed-off-by: Piotr PG Gajek <[email protected]> --------- Signed-off-by: Piotr PG Gajek <[email protected]>
1 parent 1002d9a commit 3f82f67

File tree

2 files changed

+116
-27
lines changed

2 files changed

+116
-27
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ public virtual inherited sharing class SOQL implements Queryable {
334334

335335
private static Mock mock = new Mock();
336336
private static Binder binder = new Binder();
337+
private static Integer syncQueriesIssued = 0;
337338

338339
private SoqlBuilder builder;
339340
private Executor executor;
@@ -2216,6 +2217,8 @@ public virtual inherited sharing class SOQL implements Queryable {
22162217
}
22172218

22182219
public List<SObject> toList() {
2220+
incrementQueryIssued();
2221+
22192222
if (mock.hasMock(mockId)) {
22202223
return mock.getSObjectsMock(mockId);
22212224
}
@@ -2231,6 +2234,8 @@ public virtual inherited sharing class SOQL implements Queryable {
22312234
}
22322235

22332236
public Integer toInteger() {
2237+
incrementQueryIssued();
2238+
22342239
if (mock.hasCountMock(mockId)) {
22352240
return mock.getCountMock(mockId);
22362241
}
@@ -2239,8 +2244,22 @@ public virtual inherited sharing class SOQL implements Queryable {
22392244
}
22402245

22412246
public Database.QueryLocator toQueryLocator() {
2247+
incrementQueryIssued();
2248+
22422249
return sharingExecutor.toQueryLocator(builder.toString(), binder.getBindingMap(), accessMode);
22432250
}
2251+
2252+
private void incrementQueryIssued() {
2253+
if (!Test.isRunningTest() || System.isBatch() || System.isFuture() || System.isQueueable() || System.isScheduled()) {
2254+
return;
2255+
}
2256+
2257+
SOQL.syncQueriesIssued++;
2258+
2259+
if (SOQL.syncQueriesIssued > 100) {
2260+
throw new QueryException('Too many SOQL queries.');
2261+
}
2262+
}
22442263
}
22452264

22462265
private interface DatabaseQuery {

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

Lines changed: 97 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3480,6 +3480,103 @@ private class SOQL_Test {
34803480
}
34813481
}
34823482

3483+
@IsTest
3484+
static void querySynchronousCloseToLimitWithMocking() {
3485+
// Setup
3486+
Exception queryException = null;
3487+
3488+
SOQL.setMock('mockingQuery', new List<Account>{
3489+
new Account(Name = 'Test 1'),
3490+
new Account(Name = 'Test 2')
3491+
});
3492+
3493+
// Test
3494+
try {
3495+
Test.startTest();
3496+
for (Integer i = 0 ; i < 100 ; i++) {
3497+
SOQL.of(Account.SObjectType).mockId('mockingQuery').toList();
3498+
}
3499+
Test.stopTest();
3500+
} catch (QueryException e) {
3501+
queryException = e;
3502+
}
3503+
3504+
// Verify
3505+
Assert.isNull(queryException, 'The synchronous query should not have exceeded the limit.');
3506+
}
3507+
3508+
@IsTest
3509+
static void querySynchronousLimitExceptionWithMocking() {
3510+
// Setup
3511+
Exception queryException = null;
3512+
3513+
SOQL.setMock('mockingQuery', new List<Account>{
3514+
new Account(Name = 'Test 1'),
3515+
new Account(Name = 'Test 2')
3516+
});
3517+
3518+
// Test
3519+
try {
3520+
Test.startTest();
3521+
for (Integer i = 0 ; i < 101 ; i++) {
3522+
SOQL.of(Account.SObjectType).mockId('mockingQuery').toList();
3523+
}
3524+
Test.stopTest();
3525+
} catch (QueryException e) {
3526+
queryException = e;
3527+
}
3528+
3529+
// Verify
3530+
Assert.isNotNull(queryException, 'The synchronous query should have exceeded the limit.');
3531+
Assert.areEqual('Too many SOQL queries.', queryException.getMessage(), 'The synchronous query should not have been exceeded.');
3532+
}
3533+
3534+
@IsTest
3535+
static void querySynchronousLimitExceptionWithoutMocking() {
3536+
// Setup
3537+
Exception queryException = null;
3538+
3539+
// Test
3540+
try {
3541+
for (Integer i = 0 ; i < 102 ; i++) {
3542+
SOQL.of(Account.SObjectType).toList();
3543+
}
3544+
} catch (QueryException e) {
3545+
queryException = e;
3546+
}
3547+
3548+
// Verify
3549+
Assert.isNotNull(queryException, 'The synchronous query should have exceeded the limit.');
3550+
Assert.areEqual('Too many SOQL queries.', queryException.getMessage(), 'The synchronous query should have been exceeded.');
3551+
}
3552+
3553+
@SuppressWarnings('PMD.ApexUnitTestClassShouldHaveAsserts')
3554+
@IsTest
3555+
static void preview() {
3556+
// Test
3557+
SOQL.of(Account.SObjectType).preview().toList();
3558+
}
3559+
3560+
@SuppressWarnings('PMD.ApexUnitTestClassShouldHaveAsserts')
3561+
@IsTest
3562+
static void previewWithConditions() {
3563+
// Test
3564+
SOQL.of(Account.SObjectType)
3565+
.whereAre(SOQL.FilterGroup
3566+
.add(SOQL.Filter.with(Account.Name).equal('Test'))
3567+
.add(SOQL.Filter.with(Account.Industry).equal('IT'))
3568+
)
3569+
.preview()
3570+
.toList();
3571+
}
3572+
3573+
@SuppressWarnings('PMD.ApexUnitTestClassShouldHaveAsserts')
3574+
@IsTest
3575+
static void previewCount() {
3576+
// Test
3577+
SOQL.of(Account.SObjectType).count().preview().toInteger();
3578+
}
3579+
34833580
static List<Account> insertAccounts() {
34843581
List<Account> accounts = new List<Account>{
34853582
new Account(Name = 'Test 1'),
@@ -3534,31 +3631,4 @@ private class SOQL_Test {
35343631
UserName = '[email protected]'
35353632
);
35363633
}
3537-
3538-
@SuppressWarnings('PMD.ApexUnitTestClassShouldHaveAsserts')
3539-
@IsTest
3540-
static void preview() {
3541-
// Test
3542-
SOQL.of(Account.SObjectType).preview().toList();
3543-
}
3544-
3545-
@SuppressWarnings('PMD.ApexUnitTestClassShouldHaveAsserts')
3546-
@IsTest
3547-
static void previewWithConditions() {
3548-
// Test
3549-
SOQL.of(Account.SObjectType)
3550-
.whereAre(SOQL.FilterGroup
3551-
.add(SOQL.Filter.with(Account.Name).equal('Test'))
3552-
.add(SOQL.Filter.with(Account.Industry).equal('IT'))
3553-
)
3554-
.preview()
3555-
.toList();
3556-
}
3557-
3558-
@SuppressWarnings('PMD.ApexUnitTestClassShouldHaveAsserts')
3559-
@IsTest
3560-
static void previewCount() {
3561-
// Test
3562-
SOQL.of(Account.SObjectType).count().preview().toInteger();
3563-
}
35643634
}

0 commit comments

Comments
 (0)