Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

calculate total number of queries issued #134

Open
wants to merge 1 commit into
base: release/v3.4.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions force-app/main/default/classes/SOQL.cls
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ public virtual inherited sharing class SOQL implements Queryable {

private static Mock mock = new Mock();
private static Binder binder = new Binder();
private static Integer queriesIssued = 0;

private QueryBuilder builder;
private Executor executor;
Expand Down Expand Up @@ -2189,6 +2190,8 @@ public virtual inherited sharing class SOQL implements Queryable {
}

public List<SObject> toList() {
calculateTotalNumberOfQueriesIssued();

if (mock.hasMock(mockId)) {
return mock.getSObjectsMock(mockId);
}
Expand Down Expand Up @@ -2262,6 +2265,8 @@ public virtual inherited sharing class SOQL implements Queryable {
}

public Integer toInteger() {
calculateTotalNumberOfQueriesIssued();

if (mock.hasCountMock(mockId)) {
return mock.getCountMock(mockId);
}
Expand All @@ -2270,8 +2275,21 @@ public virtual inherited sharing class SOQL implements Queryable {
}

public Database.QueryLocator toQueryLocator() {
calculateTotalNumberOfQueriesIssued();
return sharingExecutor.toQueryLocator(builder.toString(), binder.getBindingMap(), accessMode);
}

private void calculateTotalNumberOfQueriesIssued() {
queriesIssued++;

if (Test.isRunningTest()) {
Boolean isAsynchronous = System.isBatch() || System.isFuture() || System.isQueueable() || System.isScheduled();

if ((queriesIssued > 100 && !isAsynchronous) || (queriesIssued > 200 && isAsynchronous)) {
throw new QueryException('Too many SOQL queries.');
}
}
}
}

private interface DatabaseQuery {
Expand Down
150 changes: 123 additions & 27 deletions force-app/main/default/classes/SOQL_Test.cls
Original file line number Diff line number Diff line change
Expand Up @@ -3335,6 +3335,129 @@ private class SOQL_Test {
}
}

@IsTest
static void querySynchronousLimitExceptionWithMocking() {
// Setup
Exception queryException = null;

// Test
SOQL.setMock('mockingQuery', new List<Account>{
new Account(Name = 'Test 1'),
new Account(Name = 'Test 2')
});

try {
for (Integer i = 0 ; i < 102 ; i++) {
SOQL.of(Account.SObjectType).mockId('mockingQuery').toList();
}
} catch (Exception e) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use QueryException

queryException = e;
}

// Verify
Assert.areEqual('Too many SOQL queries.', queryException.getMessage());
}

@IsTest
static void queryAsynchronousLimitExceptionWithMocking() {
// Setup
Exception queryException = null;

// Test
SOQL.setMock('mockingQuery', new List<Account>{
new Account(Name = 'Test 1'),
new Account(Name = 'Test 2')
});

try {
Test.startTest();
asynchronousQueriesWithMocking();
Test.stopTest();
} catch (Exception e) {
queryException = e;
}

// Verify
Assert.areEqual('Too many SOQL queries.', queryException.getMessage());
}

@IsTest
static void querySynchronousLimitExceptionWithoutMocking() {
// Setup
Exception queryException = null;

// Test
try {
for (Integer i = 0 ; i < 102 ; i++) {
SOQL.of(Account.SObjectType).toList();
}
} catch (Exception e) {
queryException = e;
}

// Verify
Assert.areEqual('Too many SOQL queries.', queryException.getMessage());
}

@IsTest
static void queryAsynchronousLimitExceptionWithoutMocking() {
// Setup
Exception queryException = null;

// Test
try {
Test.startTest();
asynchronousQueriesWithoutMocking();
Test.stopTest();
} catch (Exception e) {
queryException = e;
}

// Verify
Assert.areEqual('Too many SOQL queries.', queryException.getMessage());
}

@SuppressWarnings('PMD.ApexUnitTestClassShouldHaveAsserts')
@IsTest
static void preview() {
// Test
SOQL.of(Account.SObjectType).preview().toList();
}

@SuppressWarnings('PMD.ApexUnitTestClassShouldHaveAsserts')
@IsTest
static void previewWithConditions() {
// Test
SOQL.of(Account.SObjectType)
.whereAre(SOQL.FilterGroup
.add(SOQL.Filter.with(Account.Name).equal('Test'))
.add(SOQL.Filter.with(Account.Industry).equal('IT'))
)
.preview()
.toList();
}

@SuppressWarnings('PMD.ApexUnitTestClassShouldHaveAsserts')
@IsTest
static void previewCount() {
// Test
SOQL.of(Account.SObjectType).count().preview().toInteger();
}

@future
static void asynchronousQueriesWithMocking() {
for (Integer i = 0 ; i < 202 ; i++) {
SOQL.of(Account.SObjectType).mockId('mockingQuery').toList();
}
}

@future
static void asynchronousQueriesWithoutMocking() {
for (Integer i = 0 ; i < 202 ; i++) {
SOQL.of(Account.SObjectType).toList();
}
}

static List<Account> insertAccounts() {
List<Account> accounts = new List<Account>{
new Account(Name = 'Test 1'),
Expand Down Expand Up @@ -3373,31 +3496,4 @@ private class SOQL_Test {
UserName = '[email protected]'
);
}

@SuppressWarnings('PMD.ApexUnitTestClassShouldHaveAsserts')
@IsTest
static void preview() {
// Test
SOQL.of(Account.SObjectType).preview().toList();
}

@SuppressWarnings('PMD.ApexUnitTestClassShouldHaveAsserts')
@IsTest
static void previewWithConditions() {
// Test
SOQL.of(Account.SObjectType)
.whereAre(SOQL.FilterGroup
.add(SOQL.Filter.with(Account.Name).equal('Test'))
.add(SOQL.Filter.with(Account.Industry).equal('IT'))
)
.preview()
.toList();
}

@SuppressWarnings('PMD.ApexUnitTestClassShouldHaveAsserts')
@IsTest
static void previewCount() {
// Test
SOQL.of(Account.SObjectType).count().preview().toInteger();
}
}