From 49d47c9a74ae1b9c9072f74368da2d1f8cc7f3a6 Mon Sep 17 00:00:00 2001 From: Robert Baillie Date: Thu, 24 Feb 2022 14:54:56 +0000 Subject: [PATCH] Finished the date literal tests and fixed a boundary bug --- .../fflib-extension/ortoo_DateLiterals.cls | 8 +- .../tests/ortoo_DateLiteralsTest.cls | 104 ++++++++++++++++++ 2 files changed, 110 insertions(+), 2 deletions(-) diff --git a/framework/default/ortoo-core/default/classes/fflib-extension/ortoo_DateLiterals.cls b/framework/default/ortoo-core/default/classes/fflib-extension/ortoo_DateLiterals.cls index 7933f5cf064..79fde39d568 100644 --- a/framework/default/ortoo-core/default/classes/fflib-extension/ortoo_DateLiterals.cls +++ b/framework/default/ortoo-core/default/classes/fflib-extension/ortoo_DateLiterals.cls @@ -600,11 +600,13 @@ public inherited sharing class ortoo_DateLiterals public Integer compare( DateTime value, Date startDate, Date endDate ) { Contract.requires( startDate <= endDate, 'compare called with a start date that is higher than the end date ('+ startDate + ', ' + endDate + ')' ); + if ( value < DateTime.newInstance( startDate.year(), startDate.month(), startDate.day(), 0, 0, 0 ) ) { return -1; } - if ( value > DateTime.newInstance( endDate.year(), endDate.month(), endDate.day(), 23, 59, 29 ) ) + Date dayAfterTheEndDate = endDate.addDays( 1 ); + if ( value >= DateTime.newInstance( dayAfterTheEndDate.year(), dayAfterTheEndDate.month(), dayAfterTheEndDate.day(), 0, 0, 0 ) ) { return 1; } @@ -614,8 +616,10 @@ public inherited sharing class ortoo_DateLiterals private class DateToDateRangeComparer { - public Integer compare( Date value, Date startDate, Date endDate ) { + public Integer compare( Date value, Date startDate, Date endDate ) + { Contract.requires( startDate <= endDate, 'compare called with a start date that is higher than the end date ('+ startDate + ', ' + endDate + ')' ); + if ( value < startDate ) { return -1; diff --git a/framework/default/ortoo-core/default/classes/fflib-extension/tests/ortoo_DateLiteralsTest.cls b/framework/default/ortoo-core/default/classes/fflib-extension/tests/ortoo_DateLiteralsTest.cls index eec22907b8b..8572ccac3bd 100644 --- a/framework/default/ortoo-core/default/classes/fflib-extension/tests/ortoo_DateLiteralsTest.cls +++ b/framework/default/ortoo-core/default/classes/fflib-extension/tests/ortoo_DateLiteralsTest.cls @@ -602,4 +602,108 @@ private without sharing class ortoo_DateLiteralsTest System.assertEquals( Date.newInstance( 2024, 03, 01 ), literal.getStartDate(), 'NextNMonths_getStartDate, will return the start of next month' ); System.assertEquals( Date.newInstance( 2024, 07, 31 ), literal.getEndDate(), 'NextNMonths_getEndDate, will return the end of the month, n months in the future' ); } + + @isTest + private static void compare_whenGivenADateBeforeTheRange_returnsMinus1() // NOPMD: Test method name format + { + ortoo_DateLiterals.today = Date.newInstance( 2024, 02, 23 ); + ortoo_DateLiterals.DateRangeLiteral literal = new ortoo_DateLiterals.LastNDays( 1 ); + + Test.startTest(); + Integer got = literal.compare( Date.newInstance( 2024, 02, 21 ) ); + Test.stopTest(); + + System.assertEquals( -1, got, 'compare, when given a date before the range, will return minus 1' ); + } + + @isTest + private static void compare_whenGivenADateInsideTheRange_returnsZero() // NOPMD: Test method name format + { + ortoo_DateLiterals.today = Date.newInstance( 2024, 02, 23 ); + ortoo_DateLiterals.DateRangeLiteral literal = new ortoo_DateLiterals.LastNDays( 1 ); + + Test.startTest(); + Integer got = literal.compare( Date.newInstance( 2024, 02, 23 ) ); + Test.stopTest(); + + System.assertEquals( 0, got, 'compare, when given a date inside the range, will return zero' ); + } + + @isTest + private static void compare_whenGivenADateAfterTheRange_returns1() // NOPMD: Test method name format + { + ortoo_DateLiterals.today = Date.newInstance( 2024, 02, 23 ); + ortoo_DateLiterals.DateRangeLiteral literal = new ortoo_DateLiterals.LastNDays( 1 ); + + Test.startTest(); + Integer got = literal.compare( Date.newInstance( 2024, 02, 24 ) ); + Test.stopTest(); + + System.assertEquals( 1, got, 'compare, when given a date after the range, will return one' ); + } + + @isTest + private static void compare_whenGivenADateTimeJustBeforeTheRange_returnsMinus1() // NOPMD: Test method name format + { + ortoo_DateLiterals.today = Date.newInstance( 2024, 02, 23 ); + ortoo_DateLiterals.DateRangeLiteral literal = new ortoo_DateLiterals.LastNDays( 1 ); + + Test.startTest(); + Integer got = literal.compare( DateTime.newInstance( 2024, 02, 21, 23, 59, 59 ) ); + Test.stopTest(); + + System.assertEquals( -1, got, 'compare, when given a datetime just before the range, will return minus 1' ); + } + + @isTest + private static void compare_whenGivenADateTimeJustAtStartOfTheRange_returnsZero() // NOPMD: Test method name format + { + ortoo_DateLiterals.today = Date.newInstance( 2024, 02, 23 ); + ortoo_DateLiterals.DateRangeLiteral literal = new ortoo_DateLiterals.LastNDays( 1 ); + + Test.startTest(); + Integer got = literal.compare( DateTime.newInstance( 2024, 02, 22, 0, 0, 1 ) ); + Test.stopTest(); + + System.assertEquals( 0, got, 'compare, when given a datetime just at the start of the range, will return 0' ); + } + + @isTest + private static void compare_whenGivenADateTimeInTheMiddleOfTheRange_returnsZero() // NOPMD: Test method name format + { + ortoo_DateLiterals.today = Date.newInstance( 2024, 02, 23 ); + ortoo_DateLiterals.DateRangeLiteral literal = new ortoo_DateLiterals.LastNDays( 1 ); + + Test.startTest(); + Integer got = literal.compare( DateTime.newInstance( 2024, 02, 22, 12, 0, 0 ) ); + Test.stopTest(); + + System.assertEquals( 0, got, 'compare, when given a datetime in the middle of the range, will return 0' ); + } + + @isTest + private static void compare_whenGivenADateTimeJustAtEndOfTheRange_returnsZero() // NOPMD: Test method name format + { + ortoo_DateLiterals.today = Date.newInstance( 2024, 02, 23 ); + ortoo_DateLiterals.DateRangeLiteral literal = new ortoo_DateLiterals.LastNDays( 1 ); + + Test.startTest(); + Integer got = literal.compare( DateTime.newInstance( 2024, 02, 23, 23, 59, 59 ) ); + Test.stopTest(); + + System.assertEquals( 0, got, 'compare, when given a datetime just at the end of the range, will return 0' ); + } + + @isTest + private static void compare_whenGivenADateTimeJustAfterTheRange_returns1() // NOPMD: Test method name format + { + ortoo_DateLiterals.today = Date.newInstance( 2024, 02, 23 ); + ortoo_DateLiterals.DateRangeLiteral literal = new ortoo_DateLiterals.LastNDays( 1 ); + + Test.startTest(); + Integer got = literal.compare( DateTime.newInstance( 2024, 02, 24, 0, 0, 0 ) ); + Test.stopTest(); + + System.assertEquals( 1, got, 'compare, when given a datetime just after the range, will return 1' ); + } } \ No newline at end of file