From ef541debe64a8912e5309ac8a5e771d3b4beac3d Mon Sep 17 00:00:00 2001 From: Robert Baillie Date: Tue, 8 Mar 2022 09:20:23 +0000 Subject: [PATCH] Added tests for NONE cache scope --- .../tests/CachedSoqlExecutorTest.cls | 111 ++++-------------- 1 file changed, 24 insertions(+), 87 deletions(-) diff --git a/framework/default/ortoo-core/default/classes/fflib-extension/tests/CachedSoqlExecutorTest.cls b/framework/default/ortoo-core/default/classes/fflib-extension/tests/CachedSoqlExecutorTest.cls index 210cb71c5f8..7898d997d5f 100644 --- a/framework/default/ortoo-core/default/classes/fflib-extension/tests/CachedSoqlExecutorTest.cls +++ b/framework/default/ortoo-core/default/classes/fflib-extension/tests/CachedSoqlExecutorTest.cls @@ -470,15 +470,15 @@ private without sharing class CachedSoqlExecutorTest System.assertEquals( 2, soqlCalls, 'query against a NONE cache, when called twice by a user with access to the cache, will issue two SOQL statements' ); } -/* + @isTest - private static void query_session_whenCalledTwiceByAUserWithoutAccessToTheCache_issuesTwoSoqlStatements() // NOPMD: Test method name format + private static void query_none_whenCalledTwiceByAUserWithoutAccessToTheCache_issuesTwoSoqlStatements() // NOPMD: Test method name format { String soqlStatement = 'SELECT Id FROM Account'; setupAccessToSoqlCache( false ); - CachedSoqlExecutor executor = new CachedSoqlExecutor().setScope( CachedSoqlExecutor.CacheScope.SESSION ); + CachedSoqlExecutor executor = new CachedSoqlExecutor().setScope( CachedSoqlExecutor.CacheScope.NONE ); Test.startTest(); List originalResults = executor.query( soqlStatement ); @@ -486,25 +486,24 @@ private without sharing class CachedSoqlExecutorTest Integer soqlCalls = Limits.getQueries(); Test.stopTest(); - System.assertEquals( 2, soqlCalls, 'query, when called twice by a user with no access to the cache, will issue two SOQL statements' ); - System.assertEquals( originalResults, secondResults, 'query, when called twice by a user with not access to the cache, returns the same results in both calls' ); + System.assertEquals( 2, soqlCalls, 'query against a NONE cache, when called twice by a user with no access to the cache, will issue two SOQL statements' ); } @isTest - private static void clearAllCache_session_willClearAllStatementsAndResultsFromTheCache() // NOPMD: Test method name format + private static void clearAllCache_none_willNotThrowAnException() // NOPMD: Test method name format { String soqlStatement = 'SELECT Id FROM Account'; setupAccessToSoqlCache( true ); - CachedSoqlExecutor executor = new CachedSoqlExecutor().setScope( CachedSoqlExecutor.CacheScope.SESSION ); + CachedSoqlExecutor executor = new CachedSoqlExecutor().setScope( CachedSoqlExecutor.CacheScope.NONE ); Test.startTest(); - executor.query( soqlStatement ); // executes SOQL + executor.query( soqlStatement ); executor.clearAllCache(); - executor.query( soqlStatement ); // executes another SOQL + executor.query( soqlStatement ); executor.query( soqlStatement ); executor.query( soqlStatement ); @@ -512,29 +511,15 @@ private without sharing class CachedSoqlExecutorTest Integer soqlCalls = Limits.getQueries(); Test.stopTest(); - System.assertEquals( 2, soqlCalls, 'clearAllCache, when called, will clear statements and results from the cache meaning that SOQL executions will need to be repeated when query is called' ); - } - - @isTest - private static void clearAllCache_session_whenThereIsNothingInTheCache_willNotThrowAnException() // NOPMD: Test method name format - { - setupAccessToSoqlCache( true ); - - CachedSoqlExecutor executor = new CachedSoqlExecutor().setScope( CachedSoqlExecutor.CacheScope.SESSION ); - - Test.startTest(); - executor.clearAllCache(); - Test.stopTest(); - - System.assert( true, 'clearAllCache, when there is nothing in the cache, will not throw an exception' ); + System.assertEquals( 4, soqlCalls, 'clearAllCache against a none cache, when called, will not throw an exception and will not affect the number of SOQL statements issued' ); } @isTest - private static void clearAllCache_session_whenTheUserDoesNotHaveAccessToTheCache_throwsAnException() // NOPMD: Test method name format + private static void clearAllCache_none_whenTheUserDoesNotHaveAccessToTheCache_throwsAnException() // NOPMD: Test method name format { setupAccessToSoqlCache( false ); - CachedSoqlExecutor executor = new CachedSoqlExecutor().setScope( CachedSoqlExecutor.CacheScope.SESSION ); + CachedSoqlExecutor executor = new CachedSoqlExecutor().setScope( CachedSoqlExecutor.CacheScope.NONE ); Test.startTest(); Exception exceptionThrown; @@ -548,75 +533,38 @@ private without sharing class CachedSoqlExecutorTest } Test.stopTest(); - ortoo_Asserts.assertContains( Label.ortoo_core_soql_cache_access_violation, exceptionThrown?.getMessage(), 'clearAllCache, when the user does not have access to the cache, will throw an exception' ); + ortoo_Asserts.assertContains( Label.ortoo_core_soql_cache_access_violation, exceptionThrown?.getMessage(), 'clearAllCache against a none cache, when the user does not have access to the cache, will throw an exception' ); } @isTest - private static void clearCacheFor_session_whenGivenASoqlStatementThatHasBeenExecuted_willClearTheCacheForThatStatement() // NOPMD: Test method name format + private static void clearCacheFor_none_doesNotAffectTheNumberOfSoqlStatementsIssued() // NOPMD: Test method name format { String soqlStatement = 'SELECT Id FROM Account'; setupAccessToSoqlCache( true ); - CachedSoqlExecutor executor = new CachedSoqlExecutor().setScope( CachedSoqlExecutor.CacheScope.SESSION ); + CachedSoqlExecutor executor = new CachedSoqlExecutor().setScope( CachedSoqlExecutor.CacheScope.NONE ); executor.query( soqlStatement ); Test.startTest(); executor.clearCacheFor( soqlStatement ); - executor.query( soqlStatement ); // should execute another soql - Integer soqlCalls = Limits.getQueries(); - Test.stopTest(); - - System.assertEquals( 1, soqlCalls, 'clearCacheFor, when given a SOQL statement that is already in the cache, will clear that soql from the cache' ); - } - - @isTest - private static void clearCacheFor_session_whenGivenASoqlStatementThatHasBeenExecuted_willNotClearTheCacheForOtherStatements() // NOPMD: Test method name format - { - String soqlStatement1 = 'SELECT Id FROM Account'; - String soqlStatement2 = 'SELECT Id FROM Account LIMIT 1'; - - setupAccessToSoqlCache( true ); - - CachedSoqlExecutor executor = new CachedSoqlExecutor().setScope( CachedSoqlExecutor.CacheScope.SESSION ); - - executor.query( soqlStatement1 ); - executor.query( soqlStatement2 ); - - Test.startTest(); - executor.clearCacheFor( soqlStatement1 ); - executor.query( soqlStatement2 ); // should not execute another soql + executor.query( soqlStatement ); Integer soqlCalls = Limits.getQueries(); Test.stopTest(); - System.assertEquals( 0, soqlCalls, 'clearCacheFor, when given a SOQL statement that is already in the cache, will not clear other soql from the cache' ); + System.assertEquals( 1, soqlCalls, 'clearCacheFor against a none cache, does not affect the number of SOQL statements issued' ); } - @isTest - private static void clearCacheFor_session_whenGivenASoqlStatementThatHasNotBeenExecuted_willNotThrowAnException() // NOPMD: Test method name format - { - String soqlStatement = 'SELECT Id FROM Account'; - - setupAccessToSoqlCache( true ); - - CachedSoqlExecutor executor = new CachedSoqlExecutor().setScope( CachedSoqlExecutor.CacheScope.SESSION ); - - Test.startTest(); - executor.clearCacheFor( soqlStatement ); - Test.stopTest(); - - System.assert( true, 'clearCacheFor, when given a SOQL statement that has not been executed, will not throw an exception' ); - } @isTest - private static void clearCacheFor_session_whenTheUserDoesNotHaveAccessToTheCache_throwsAnException() // NOPMD: Test method name format + private static void clearCacheFor_none_whenTheUserDoesNotHaveAccessToTheCache_throwsAnException() // NOPMD: Test method name format { String soqlStatement = 'SELECT Id FROM Account'; setupAccessToSoqlCache( false ); - CachedSoqlExecutor executor = new CachedSoqlExecutor().setScope( CachedSoqlExecutor.CacheScope.SESSION ); + CachedSoqlExecutor executor = new CachedSoqlExecutor().setScope( CachedSoqlExecutor.CacheScope.NONE ); Test.startTest(); Exception exceptionThrown; @@ -630,11 +578,11 @@ private without sharing class CachedSoqlExecutorTest } Test.stopTest(); - ortoo_Asserts.assertContains( Label.ortoo_core_soql_cache_access_violation, exceptionThrown?.getMessage(), 'clearCacheFor, when the user does not have access to the cache, will throw an exception' ); + ortoo_Asserts.assertContains( Label.ortoo_core_soql_cache_access_violation, exceptionThrown?.getMessage(), 'clearCacheFor against a none cache, when the user does not have access to the cache, will throw an exception' ); } @isTest - private static void query_session_whenRanFor100Queries_willNotThrowAnException() + private static void query_none_whenRanFor100Queries_willNotThrowAnException() { List soqlStatements = new List(); for ( Integer i=1; i<=100; i++ ) @@ -644,31 +592,20 @@ private without sharing class CachedSoqlExecutorTest setupAccessToSoqlCache( true ); - CachedSoqlExecutor executor = new CachedSoqlExecutor().setScope( CachedSoqlExecutor.CacheScope.SESSION ); + CachedSoqlExecutor executor = new CachedSoqlExecutor().setScope( CachedSoqlExecutor.CacheScope.NONE ); Test.startTest(); - // Run each statement multiple times, one by one - for ( String thisSoqlStatement : soqlStatements ) - { - executor.query( thisSoqlStatement ); - executor.query( thisSoqlStatement ); - executor.query( thisSoqlStatement ); - executor.query( thisSoqlStatement ); - executor.query( thisSoqlStatement ); - } - - // Then run each statement again + // Run each statement once - this is the maximum we can do for ( String thisSoqlStatement : soqlStatements ) { executor.query( thisSoqlStatement ); } - Test.stopTest(); - System.assert( true, 'query, when run multiple times for 100 distinct queries, will not throw an exception' ); + System.assert( true, 'query against a none cache, when run for 100 queries, will not throw an exception' ); } -*/ + private static void setupAccessToSoqlCache( Boolean accessToCache ) { ApplicationMockRegistrar.registerMockService( IPermissionsService.class )