Skip to content
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
4 changes: 0 additions & 4 deletions Makefile

This file was deleted.

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 @@ -7477,4 +7477,32 @@ private class MockDatabaseTest {
'Incorrect number of opportunities'
);
}

@isTest
static void ensureNWeeksAgoWorksInWhereClause() {
List<Opportunity> oppList = new List<Opportunity>();
Date startOfWeek = Gmt.today().toStartOfWeek();
for (Integer i = 0; i < 36; i++) {
oppList.add(
new Opportunity(
Name = 'Opp' + i,
CloseDate = startOfWeek.addDays(-7 * i)
)
);
}

MockDatabase.doInsert(oppList, true);

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

Assert.areEqual(
1,
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 @@ -162,6 +162,7 @@ public with sharing class Token {
public final static String LAST_YEAR_LITERAL = 'last_year';
public final static String NEXT_N_YEARS_LITERAL = 'next_n_years';
public final static String LAST_N_QUARTERS_LITERAL = 'last_n_quarters';
public final static String N_WEEKS_AGO_LITERAL = 'n_weeks_ago';

public final static Set<String> DATE_LITERAL_TOKENS = new Set<String>{
TODAY_LITERAL,
Expand Down Expand Up @@ -202,6 +203,7 @@ public with sharing class Token {
LAST_N_YEARS_LITERAL,
LAST_YEAR_LITERAL,
NEXT_N_YEARS_LITERAL,
LAST_N_QUARTERS_LITERAL
LAST_N_QUARTERS_LITERAL,
N_WEEKS_AGO_LITERAL
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public with sharing class DateLiteralComparableFactory {
Token.LAST_YEAR_LITERAL => LastYearComparable.class,
Token.NEXT_N_YEARS_LITERAL => NextNYearsComparable.class,
Token.LAST_N_QUARTERS_LITERAL => LastNQuartersComparable.class,
Token.N_QUARTERS_AGO_LITERAL => NQuartersAgoComparable.class
Token.N_QUARTERS_AGO_LITERAL => NQuartersAgoComparable.class,
Token.N_WEEKS_AGO_LITERAL => NWeeksAgoComparable.class
};

@TestVisible
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @description Compares a date to N weeks ago
* @author Zackary Frazier
* @since 2/12/2025
* @group Soql Engine
*/
public with sharing class NWeeksAgoComparable extends DateLiteralComparable {
public override Boolean isLessThan(Datetime fieldValue) {
Date startOfNWeeksAgo = Gmt.today()
.addDays(-7 * this.n)
.toStartOfWeek();
return fieldValue < startOfNWeeksAgo;
}

public override Boolean isGreaterThan(Datetime fieldValue) {
Date startOfNWeeksAgo = Gmt.today()
.addDays(-7 * (this.n - 1))
.toStartOfWeek();
return fieldValue >= startOfNWeeksAgo;
}
}
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,32 @@
@IsTest
private class NWeeksAgoComparableTest {
static final Date START_OF_WEEK = Gmt.today().toStartOfWeek();

@IsTest
static void ensureIsLessThanWorks() {
DateLiteralComparable comparable = new NWeeksAgoComparable()
.withToken('n_weeks_ago:3');
Assert.isTrue(
comparable.isLessThan(START_OF_WEEK.addDays(-22)),
'Expected date to be less than 3 weeks ago'
);
Assert.isFalse(
comparable.isLessThan(START_OF_WEEK.addDays(-14)),
'Expected date to be greater than 2 weeks ago'
);
}

@IsTest
static void ensureIsGreaterThanWorks() {
DateLiteralComparable comparable = new NWeeksAgoComparable()
.withToken('n_weeks_ago:3');
Assert.isTrue(
comparable.isGreaterThan(START_OF_WEEK.addDays(-14)),
'Expected date to be greater than 3 weeks ago'
);
Assert.isFalse(
comparable.isGreaterThan(START_OF_WEEK.addDays(-20)),
'Expected date to be less than 2 weeks ago'
);
}
}
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 @@ -914,4 +914,22 @@ private class ParserTest {
}
Test.stopTest();
}

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