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

[FEATURE] THIS_FISCAL_QUARTER #257

Merged
merged 1 commit into from
Feb 2, 2025
Merged
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
28 changes: 28 additions & 0 deletions moxygen/main/default/classes/database/MockDatabaseTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -7309,4 +7309,32 @@ private class MockDatabaseTest {
'Incorrect number of opportunities'
);
}

@isTest
static void ensureThisFiscalQuarterWorksInWhereClause() {
List<Opportunity> oppList = new List<Opportunity>();
Date startOfThisFiscalQuarter = Gmt.startOfThisFiscalQuarter();
for (Integer i = 0; i < 36; i++) {
oppList.add(
new Opportunity(
Name = 'Opp' + i,
CloseDate = startOfThisFiscalQuarter.addMonths(i)
)
);
}

MockDatabase.doInsert(oppList, true);

Test.startTest();
List<Opportunity> opportunities = MockDatabase.query(
'SELECT Id, CloseDate FROM Opportunity WHERE CloseDate = THIS_FISCAL_QUARTER'
);
Test.stopTest();

Assert.areEqual(
3,
opportunities.size(),
'Incorrect number of opportunities'
);
}
}
4 changes: 3 additions & 1 deletion moxygen/main/default/classes/database/Token.cls
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ public with sharing class Token {
public final static String NEXT_FISCAL_QUARTER = 'next_fiscal_quarter';
public final static String NEXT_N_FISCAL_QUARTERS = 'next_n_fiscal_quarters';
public final static String LAST_FISCAL_QUARTER = 'last_fiscal_quarter';
public final static String THIS_FISCAL_QUARTER = 'this_fiscal_quarter';

public final static Set<String> DATE_LITERAL_TOKENS = new Set<String>{
TODAY_LITERAL,
Expand Down Expand Up @@ -190,6 +191,7 @@ public with sharing class Token {
LAST_N_FISCAL_QUARTERS,
NEXT_FISCAL_QUARTER,
NEXT_N_FISCAL_QUARTERS,
LAST_FISCAL_QUARTER
LAST_FISCAL_QUARTER,
THIS_FISCAL_QUARTER
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public with sharing class DateLiteralComparableFactory {
Token.LAST_N_FISCAL_QUARTERS => LastNFiscalQuartersComparable.class,
Token.NEXT_FISCAL_QUARTER => NextFiscalQuarterComparable.class,
Token.NEXT_N_FISCAL_QUARTERS => NextNFiscalQuartersComparable.class,
Token.LAST_FISCAL_QUARTER => LastFiscalQuarterComparable.class
Token.LAST_FISCAL_QUARTER => LastFiscalQuarterComparable.class,
Token.THIS_FISCAL_QUARTER => ThisFiscalQuarterComparable.class
};

@TestVisible
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @description Comparable class for THIS_FISCAL_QUARTER
* @author Zackary Frazier
* @since 2/2/2025
*/
public with sharing class ThisFiscalQuarterComparable extends DateLiteralComparable {
public override Boolean isLessThan(Datetime fieldValue) {
Date startOfThisFiscalQuarter = Gmt.startOfThisFiscalQuarter();
return fieldValue < startOfThisFiscalQuarter;
}

public override Boolean isGreaterThan(Datetime fieldValue) {
Date startOfThisFiscalQuarter = Gmt.startOfThisFiscalQuarter();
Date startOfNextFiscalQuarter = startOfThisFiscalQuarter.addMonths(3);
return fieldValue >= startOfNextFiscalQuarter;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
@isTest
private class ThisFiscalQuarterComparableTest {
@isTest
static void ensureIsLessThanWorks() {
Date startOfThisFiscalQuarter = Gmt.startOfThisFiscalQuarter();
Date startOfThisFiscalQuarterMinusOneDay = startOfThisFiscalQuarter.addDays(
-1
);

ThisFiscalQuarterComparable dateLiteralComparable = new ThisFiscalQuarterComparable();

Assert.isTrue(
dateLiteralComparable.isLessThan(
startOfThisFiscalQuarterMinusOneDay
),
'Start of this fiscal quarter minus one day should be less than the this fiscal quarter'
);

Assert.isFalse(
dateLiteralComparable.isLessThan(startOfThisFiscalQuarter),
'Start of this fiscal quarter should not be less than the this fiscal quarter'
);
}

@isTest
static void ensureIsGreaterThanWorks() {
Date startOfThisFiscalQuarter = Gmt.startOfThisFiscalQuarter();
Date startOfNextFiscalQuarter = startOfThisFiscalQuarter.addMonths(3);

ThisFiscalQuarterComparable dateLiteralComparable = new ThisFiscalQuarterComparable();

Assert.isTrue(
dateLiteralComparable.isGreaterThan(startOfNextFiscalQuarter),
'Start of next fiscal quarter should be greater than the this fiscal quarter'
);

Assert.isFalse(
dateLiteralComparable.isGreaterThan(startOfThisFiscalQuarter),
'Start of this fiscal quarter should not be greater than the this fiscal quarter'
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
Original file line number Diff line number Diff line change
Expand Up @@ -806,4 +806,22 @@ private class ParserTest {
}
Test.stopTest();
}

@isTest
static void ensureThisFiscalQuarterCanBeParsed() {
Parser p = new Parser();
String soqlQuery = 'SELECT Id FROM Opportunity WHERE CreatedDate = THIS_FISCAL_QUARTER';
Test.startTest();
try {
p.parse(soqlQuery);
} catch (Exception e) {
Assert.fail(
'Expected no exception but got ' +
e.getMessage() +
' for ' +
soqlQuery
);
}
Test.stopTest();
}
}