Skip to content

Commit 53edd71

Browse files
Added test for age based cache retrieval in CachedSoqlExecutor
1 parent d122285 commit 53edd71

File tree

2 files changed

+156
-2
lines changed

2 files changed

+156
-2
lines changed

framework/default/ortoo-core/default/classes/fflib-extension/caching/CachedSoqlExecutor.cls

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ public inherited sharing class CachedSoqlExecutor //NOPMD: incorrect report of t
7171
* @param Long The maximum age that the retrieved entry is allowed to be before a cache miss is recorded
7272
* @return List<Sobject> The records that match
7373
*/
74-
// TODO: test
7574
public List<Sobject> query( String soql, Long maximumAgeInSeconds )
7675
{
7776
Contract.requires( maximumAgeInSeconds != null, 'query called with a null maximumAgeInSeconds' );
@@ -91,7 +90,6 @@ public inherited sharing class CachedSoqlExecutor //NOPMD: incorrect report of t
9190
* before it is registered as a cache miss
9291
* @return List<Sobject> The records that match
9392
*/
94-
// TODO: test
9593
public List<Sobject> query( String soql, DateTime minimumDateTimeAdded )
9694
{
9795
Contract.requires( minimumDateTimeAdded != null, 'query called with a null minimumDateTimeAdded' );

framework/default/ortoo-core/default/classes/fflib-extension/caching/tests/CachedSoqlExecutorTest.cls

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,162 @@ private without sharing class CachedSoqlExecutorTest
497497
logger.assertNumberOfLogCalls( 0, 'clearCacheFor against a none cache, when called by a user that does not have access to the org cache, will not create a log' );
498498
}
499499

500+
@isTest
501+
private static void query_maxAge_session_whenCalledTwiceWithinAge_onlyIssuesOneSoql() // NOPMD: Test method name format
502+
{
503+
TestLoggerService logger = TestLoggerUtils.registerTestLoggerService();
504+
505+
String soqlStatement = 'SELECT Id FROM Account';
506+
507+
setupAccessToSoqlCache( true );
508+
509+
CachedSoqlExecutor executor = new CachedSoqlExecutor().setScope( CachedSoqlExecutor.CacheScope.SESSION );
510+
511+
Test.startTest();
512+
TestDateTimeUtils.setCurrentDateTime( 0 );
513+
List<Sobject> originalResults = executor.query( soqlStatement, 100 );
514+
515+
TestDateTimeUtils.addToCurrentTime( 50 ); // it is now 50 seconds later
516+
logger.clearLogHistory();
517+
List<Sobject> secondResults = executor.query( soqlStatement, 100 ); // so this should still return the results
518+
Integer soqlCalls = Limits.getQueries();
519+
Test.stopTest();
520+
521+
System.assertEquals( 1, soqlCalls, 'query with a maximum age on session cache, when called twice within the required age, will only issue one SOQL statement' );
522+
System.assertEquals( originalResults, secondResults, 'query with a maximum age on session cache, when called twice within the required age, returns the same results in both calls' );
523+
524+
logger.assertNumberOfLogCalls( 0, 'query with a maximum age on session cache,when called twice within the required age, does not generate a log entry' );
525+
}
526+
527+
@isTest
528+
private static void query_maxAge_session_whenCalledTwiceOutsideAge_issuesTwoSoql() // NOPMD: Test method name format
529+
{
530+
TestLoggerService logger = TestLoggerUtils.registerTestLoggerService();
531+
532+
String soqlStatement = 'SELECT Id FROM Account';
533+
534+
setupAccessToSoqlCache( true );
535+
536+
CachedSoqlExecutor executor = new CachedSoqlExecutor().setScope( CachedSoqlExecutor.CacheScope.SESSION );
537+
538+
Test.startTest();
539+
TestDateTimeUtils.setCurrentDateTime( 0 );
540+
List<Sobject> originalResults = executor.query( soqlStatement, 100 );
541+
542+
TestDateTimeUtils.addToCurrentTime( 100 ); // it is now 50 seconds later
543+
logger.clearLogHistory();
544+
List<Sobject> secondResults = executor.query( soqlStatement, 50 ); // so this should result in a new query because the previous results are too old
545+
Integer soqlCalls = Limits.getQueries();
546+
Test.stopTest();
547+
548+
System.assertEquals( 2, soqlCalls, 'query with a maximum age on session cache, when called twice with the second outside the required age, will issue two SOQL statements' );
549+
}
550+
551+
@isTest
552+
private static void query_whenPassedANullMaxAge_throwsAnException() // NOPMD: Test method name format
553+
{
554+
String soqlStatement = 'SELECT Id FROM Account';
555+
CachedSoqlExecutor executor = new CachedSoqlExecutor();
556+
557+
Long nullAge;
558+
559+
Test.startTest();
560+
String exceptionMessage;
561+
try
562+
{
563+
executor.query( soqlStatement, nullAge );
564+
}
565+
catch ( Contract.RequiresException e )
566+
{
567+
exceptionMessage = e.getMessage();
568+
}
569+
Test.stopTest();
570+
571+
ortoo_Asserts.assertContains( 'query called with a null maximumAgeInSeconds', exceptionMessage, 'query, when passed a null maximumAgeInSeconds, will throw an exception' );
572+
}
573+
574+
@isTest
575+
private static void query_minDate_session_whenCalledTwiceWithinDate_onlyIssuesOneSoql() // NOPMD: Test method name format
576+
{
577+
TestLoggerService logger = TestLoggerUtils.registerTestLoggerService();
578+
579+
DateTime earliestDate = DateTime.newInstance( 2020, 2, 4, 14, 00, 00 );
580+
DateTime middleDate = DateTime.newInstance( 2020, 2, 5, 14, 00, 00 );
581+
DateTime latestDate = DateTime.newInstance( 2020, 2, 6, 14, 00, 00 );
582+
583+
String soqlStatement = 'SELECT Id FROM Account';
584+
585+
setupAccessToSoqlCache( true );
586+
587+
CachedSoqlExecutor executor = new CachedSoqlExecutor().setScope( CachedSoqlExecutor.CacheScope.SESSION );
588+
589+
Test.startTest();
590+
TestDateTimeUtils.setCurrentDateTime( middleDate );
591+
List<Sobject> originalResults = executor.query( soqlStatement, middleDate );
592+
593+
TestDateTimeUtils.setCurrentDateTime( latestDate );
594+
logger.clearLogHistory();
595+
List<Sobject> secondResults = executor.query( soqlStatement, earliestDate ); // this date is earlier than the first query ran, so the cache should be used
596+
Integer soqlCalls = Limits.getQueries();
597+
Test.stopTest();
598+
599+
System.assertEquals( 1, soqlCalls, 'query with a minimum date on session cache, when called twice within the required age, will only issue one SOQL statement' );
600+
System.assertEquals( originalResults, secondResults, 'query with a minimum date on session cache, when called twice within the required age, returns the same results in both calls' );
601+
602+
logger.assertNumberOfLogCalls( 0, 'query with a minimum date on session cache,when called twice within the required age, does not generate a log entry' );
603+
}
604+
605+
@isTest
606+
private static void query_minDate_session_whenCalledTwiceOutsideDate_issuesTwoSoql() // NOPMD: Test method name format
607+
{
608+
TestLoggerService logger = TestLoggerUtils.registerTestLoggerService();
609+
610+
DateTime earliestDate = DateTime.newInstance( 2020, 2, 4, 14, 00, 00 );
611+
DateTime middleDate = DateTime.newInstance( 2020, 2, 5, 14, 00, 00 );
612+
DateTime latestDate = DateTime.newInstance( 2020, 2, 6, 14, 00, 00 );
613+
614+
String soqlStatement = 'SELECT Id FROM Account';
615+
616+
setupAccessToSoqlCache( true );
617+
618+
CachedSoqlExecutor executor = new CachedSoqlExecutor().setScope( CachedSoqlExecutor.CacheScope.SESSION );
619+
620+
Test.startTest();
621+
TestDateTimeUtils.setCurrentDateTime( earliestDate );
622+
List<Sobject> originalResults = executor.query( soqlStatement, earliestDate );
623+
624+
TestDateTimeUtils.setCurrentDateTime( latestDate );
625+
logger.clearLogHistory();
626+
List<Sobject> secondResults = executor.query( soqlStatement, middleDate ); // this date is later than the first query ran, so the cache should not be used
627+
Integer soqlCalls = Limits.getQueries();
628+
Test.stopTest();
629+
630+
System.assertEquals( 2, soqlCalls, 'query with a maximum age on session cache, when called twice with the second outside the required age, will issue two SOQL statements' );
631+
}
632+
633+
@isTest
634+
private static void query_minDate_whenPassedANullMinDate_throwsAnException() // NOPMD: Test method name format
635+
{
636+
String soqlStatement = 'SELECT Id FROM Account';
637+
CachedSoqlExecutor executor = new CachedSoqlExecutor();
638+
639+
DateTime nullDateTime;
640+
641+
Test.startTest();
642+
String exceptionMessage;
643+
try
644+
{
645+
executor.query( soqlStatement, nullDateTime );
646+
}
647+
catch ( Contract.RequiresException e )
648+
{
649+
exceptionMessage = e.getMessage();
650+
}
651+
Test.stopTest();
652+
653+
ortoo_Asserts.assertContains( 'query called with a null minimumDateTimeAdded', exceptionMessage, 'query, when passed a null minimumDateTimeAdded, will throw an exception' );
654+
}
655+
500656
@isTest
501657
private static void query_none_whenRanFor100Queries_willNotThrow() // NOPMD: Test method name format
502658
{

0 commit comments

Comments
 (0)