Skip to content

Commit

Permalink
Added ability to specify criteria for related fields with IN
Browse files Browse the repository at this point in the history
Added tests for generation of IN clauses
Fixed bug in fflib_Criteria regarding datetime
  • Loading branch information
rob-baillie-ortoo committed Feb 14, 2022
1 parent dd6c931 commit 58a1c78
Show file tree
Hide file tree
Showing 3 changed files with 342 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ public virtual with sharing class fflib_Criteria
}
else if (value instanceof Datetime)
{
return '' + ((Datetime) value).format('yyyy-MM-dd') + 'T' + ((Datetime) value).format('HH:mm:ss') + 'Z';
return '' + ((Datetime) value).format('yyyy-MM-dd', 'GMT+0') + 'T' + ((Datetime) value).format('HH:mm:ss', 'GMT+0') + 'Z';
}

throw new CriteriaException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,54 @@ public inherited sharing virtual class ortoo_Criteria extends fflib_Criteria imp
return this;
}

public ortoo_Criteria inSet( String relatedField, Set<Object> values )
{
return inSet( relatedField, new fflib_Objects( new List<Object>( values ) ) );
}

public ortoo_Criteria inSet( String relatedField, Set<DateTime> values )
{
return inSet( relatedField, new fflib_DateTimes( values ) );
}

public ortoo_Criteria inSet( String relatedField, Set<Date> values )
{
return inSet( relatedField, new fflib_Dates( values ) );
}

public ortoo_Criteria inSet( String relatedField, Set<Decimal> values )
{
return inSet( relatedField, new fflib_Decimals( values ) );
}

public ortoo_Criteria inSet( String relatedField, Set<Double> values )
{
return inSet( relatedField, new fflib_Doubles( values ) );
}

public ortoo_Criteria inSet( String relatedField, Set<Id> values )
{
return inSet( relatedField, new fflib_Ids( values ) );
}

public ortoo_Criteria inSet( String relatedField, Set<Integer> values )
{
return inSet( relatedField, new fflib_Integers( values ) );
}

public ortoo_Criteria inSet( String relatedField, Set<Long> values )
{
return inSet( relatedField, new fflib_Longs( values ) );
}

public ortoo_Criteria inSet( String relatedField, Set<String> values )
{
return inSet( relatedField, new fflib_Strings( values ) );
}

protected ortoo_Criteria inSet( String relatedField, fflib_Objects values )
{
addEvaluator( new RelatedFieldSetEvaluator( relatedField, fflib_Operator.INx, values ) );
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ private without sharing class ortoo_CriteriaTest
String expected = 'Name LIKE \'thing%\'';
String got = criteria.toSOQL();

System.assertEquals( expected, got, 'likeString, when called with a field, will add a LIKE to the Generated SOQL' );
System.assertEquals( expected, got, 'likeString, when called with a field, will add a LIKE to the generated SOQL' );
}

@isTest
Expand All @@ -28,6 +28,295 @@ private without sharing class ortoo_CriteriaTest
String expected = 'Account.Name LIKE \'thing%\'';
String got = criteria.toSOQL();

System.assertEquals( expected, got, 'likeString, when called with a string name for a field will add a LIKE to the Generated SOQL' );
System.assertEquals( expected, got, 'likeString, when called with a string name for a field will add a LIKE to the generated SOQL' );
}

@isTest
private static void inSet_field_stringValues_whenCalled_addsAnInToTheGeneratedSoql() // NOPMD: Test method name format
{
ortoo_Criteria criteria = new ortoo_Criteria();

Test.startTest();
criteria.inSet( Account.Name, new Set<String>{ 'one', 'two' } );
Test.stopTest();

String expected = 'Name IN (\'one\',\'two\')';
String got = criteria.toSOQL();

System.assertEquals( expected, got, 'inSet, when called with a field and a set of Strings, will add an IN to the generated SOQL' );
}

@isTest
private static void inSet_stringName_stringValues_whenCalled_addsAnInToTheGeneratedSoql() // NOPMD: Test method name format
{
ortoo_Criteria criteria = new ortoo_Criteria();

Test.startTest();
criteria.inSet( 'Account.Name', new Set<String>{ 'one', 'two' } );
Test.stopTest();

String expected = 'Account.Name IN (\'one\',\'two\')';
String got = criteria.toSOQL();

System.assertEquals( expected, got, 'inSet, when called with a string field name and a set of Strings, will add an IN to the generated SOQL' );
}

@isTest
private static void inSet_field_longValues_whenCalled_addsAnInToTheGeneratedSoql() // NOPMD: Test method name format
{
ortoo_Criteria criteria = new ortoo_Criteria();

Test.startTest();
criteria.inSet( Account.Name, new Set<Long>{ 1, 2, 3 } );
Test.stopTest();

String expected = 'Name IN (1,2,3)';
String got = criteria.toSOQL();

System.assertEquals( expected, got, 'inSet, when called with a field and a set of Longs, will add an IN to the generated SOQL' );
}

@isTest
private static void inSet_stringName_longValues_whenCalled_addsAnInToTheGeneratedSoql() // NOPMD: Test method name format
{
ortoo_Criteria criteria = new ortoo_Criteria();

Test.startTest();
criteria.inSet( 'Account.Name', new Set<Long>{ 1, 2, 3 } );
Test.stopTest();

String expected = 'Account.Name IN (1,2,3)';
String got = criteria.toSOQL();

System.assertEquals( expected, got, 'inSet, when called with a string field name and a set of Longs, will add an IN to the generated SOQL' );
}

@isTest
private static void inSet_field_integerValues_whenCalled_addsAnInToTheGeneratedSoql() // NOPMD: Test method name format
{
ortoo_Criteria criteria = new ortoo_Criteria();

Test.startTest();
criteria.inSet( Account.Name, new Set<Integer>{ 1, 2, 3 } );
Test.stopTest();

String expected = 'Name IN (1,2,3)';
String got = criteria.toSOQL();

System.assertEquals( expected, got, 'inSet, when called with a field and a set of Integers, will add an IN to the generated SOQL' );
}

@isTest
private static void inSet_stringName_integerValues_whenCalled_addsAnInToTheGeneratedSoql() // NOPMD: Test method name format
{
ortoo_Criteria criteria = new ortoo_Criteria();

Test.startTest();
criteria.inSet( 'Account.Name', new Set<Integer>{ 1, 2, 3 } );
Test.stopTest();

String expected = 'Account.Name IN (1,2,3)';
String got = criteria.toSOQL();

System.assertEquals( expected, got, 'inSet, when called with a string field name and a set of Integers, will add an IN to the generated SOQL' );
}

@isTest
private static void inSet_field_idValues_whenCalled_addsAnInToTheGeneratedSoql() // NOPMD: Test method name format
{
ortoo_Criteria criteria = new ortoo_Criteria();

Id testId1 = TestIdUtils.generateId( Account.sobjectType );
Id testId2 = TestIdUtils.generateId( Account.sobjectType );

Test.startTest();
criteria.inSet( Account.Name, new Set<Id>{ testId1, testId2 } );
Test.stopTest();

String expected = 'Name IN (\''+ testId1 +'\',\''+ testId2 +'\')';
String got = criteria.toSOQL();

System.assertEquals( expected, got, 'inSet, when called with a field and a set of Ids, will add an IN to the generated SOQL' );
}

@isTest
private static void inSet_stringName_idValues_whenCalled_addsAnInToTheGeneratedSoql() // NOPMD: Test method name format
{
ortoo_Criteria criteria = new ortoo_Criteria();

Id testId1 = TestIdUtils.generateId( Account.sobjectType );
Id testId2 = TestIdUtils.generateId( Account.sobjectType );

Test.startTest();
criteria.inSet( 'Account.Name', new Set<Id>{ testId1, testId2 } );
Test.stopTest();

String expected = 'Account.Name IN (\''+ testId1 +'\',\''+ testId2 +'\')';
String got = criteria.toSOQL();

System.assertEquals( expected, got, 'inSet, when called with a string field name and a set of Ids, will add an IN to the generated SOQL' );
}

@isTest
private static void inSet_field_doubleValues_whenCalled_addsAnInToTheGeneratedSoql() // NOPMD: Test method name format
{
ortoo_Criteria criteria = new ortoo_Criteria();

Test.startTest();
criteria.inSet( Account.Name, new Set<Double>{ 1.1, 2.2, 3.3 } );
Test.stopTest();

String expected = 'Name IN (1.1,2.2,3.3)';
String got = criteria.toSOQL();

System.assertEquals( expected, got, 'inSet, when called with a field and a set of Doubles, will add an IN to the generated SOQL' );
}

@isTest
private static void inSet_stringName_doubleValues_whenCalled_addsAnInToTheGeneratedSoql() // NOPMD: Test method name format
{
ortoo_Criteria criteria = new ortoo_Criteria();

Test.startTest();
criteria.inSet( 'Account.Name', new Set<Double>{ 1.1, 2.2, 3.3 } );
Test.stopTest();

String expected = 'Account.Name IN (1.1,2.2,3.3)';
String got = criteria.toSOQL();

System.assertEquals( expected, got, 'inSet, when called with a string field name and a set of Doubles, will add an IN to the generated SOQL' );
}

@isTest
private static void inSet_field_decimalValues_whenCalled_addsAnInToTheGeneratedSoql() // NOPMD: Test method name format
{
ortoo_Criteria criteria = new ortoo_Criteria();

Test.startTest();
criteria.inSet( Account.Name, new Set<Decimal>{ 1.1, 2.2, 3.3 } );
Test.stopTest();

String expected = 'Name IN (1.1,2.2,3.3)';
String got = criteria.toSOQL();

System.assertEquals( expected, got, 'inSet, when called with a field and a set of Decimals, will add an IN to the generated SOQL' );
}

@isTest
private static void inSet_stringName_decimalValues_whenCalled_addsAnInToTheGeneratedSoql() // NOPMD: Test method name format
{
ortoo_Criteria criteria = new ortoo_Criteria();

Test.startTest();
criteria.inSet( 'Account.Name', new Set<Decimal>{ 1.1, 2.2, 3.3 } );
Test.stopTest();

String expected = 'Account.Name IN (1.1,2.2,3.3)';
String got = criteria.toSOQL();

System.assertEquals( expected, got, 'inSet, when called with a string field name and a set of Decimals, will add an IN to the generated SOQL' );
}

@isTest
private static void inSet_field_dateValues_whenCalled_addsAnInToTheGeneratedSoql() // NOPMD: Test method name format
{
ortoo_Criteria criteria = new ortoo_Criteria();

Date date1 = Date.newInstance( 2021, 10, 28 );
Date date2 = Date.newInstance( 2021, 1, 28 );

Test.startTest();
criteria.inSet( Account.Name, new Set<Date>{ date1, date2 } );
Test.stopTest();

String expected = 'Name IN (2021-10-28,2021-01-28)';
String got = criteria.toSOQL();

System.assertEquals( expected, got, 'inSet, when called with a field and a set of Dates, will add an IN to the generated SOQL' );
}

@isTest
private static void inSet_stringName_dateValues_whenCalled_addsAnInToTheGeneratedSoql() // NOPMD: Test method name format
{
ortoo_Criteria criteria = new ortoo_Criteria();

Date date1 = Date.newInstance( 2021, 10, 28 );
Date date2 = Date.newInstance( 2021, 1, 28 );

Test.startTest();
criteria.inSet( 'Account.Name', new Set<Date>{ date1, date2 } );
Test.stopTest();

String expected = 'Account.Name IN (2021-10-28,2021-01-28)';
String got = criteria.toSOQL();

System.assertEquals( expected, got, 'inSet, when called with a string field name and a set of Dates, will add an IN to the generated SOQL' );
}

@isTest
private static void inSet_field_dateTimeValues_whenCalled_addsAnInToTheGeneratedSoql() // NOPMD: Test method name format
{
ortoo_Criteria criteria = new ortoo_Criteria();

DateTime date1 = DateTime.newInstanceGmt( 2021, 10, 28, 11, 30, 00 );
DateTime date2 = DateTime.newInstanceGmt( 2021, 1, 28, 11, 35, 00 );

Test.startTest();
criteria.inSet( Account.Name, new Set<DateTime>{ date1, date2 } );
Test.stopTest();

String expected = 'Name IN (2021-10-28T11:30:00Z,2021-01-28T11:35:00Z)';
String got = criteria.toSOQL();

System.assertEquals( expected, got, 'inSet, when called with a field and a set of DateTimes, will add an IN to the generated SOQL' );
}

@isTest
private static void inSet_stringName_dateTimeValues_whenCalled_addsAnInToTheGeneratedSoql() // NOPMD: Test method name format
{
ortoo_Criteria criteria = new ortoo_Criteria();

DateTime date1 = DateTime.newInstanceGmt( 2021, 10, 28, 11, 30, 00 );
DateTime date2 = DateTime.newInstanceGmt( 2021, 1, 28, 11, 35, 00 );

Test.startTest();
criteria.inSet( 'Account.Name', new Set<DateTime>{ date1, date2 } );
Test.stopTest();

String expected = 'Account.Name IN (2021-10-28T11:30:00Z,2021-01-28T11:35:00Z)';
String got = criteria.toSOQL();

System.assertEquals( expected, got, 'inSet, when called with a string field name and a set of DateTimes, will add an IN to the generated SOQL' );
}

@isTest
private static void inSet_field_objectValues_whenCalled_addsAnInToTheGeneratedSoql() // NOPMD: Test method name format
{
ortoo_Criteria criteria = new ortoo_Criteria();

Test.startTest();
criteria.inSet( Account.Name, new Set<Object>{ 1, 2 } );
Test.stopTest();

String expected = 'Name IN (1,2)';
String got = criteria.toSOQL();

System.assertEquals( expected, got, 'inSet, when called with a field and a set of DateTimes, will add an IN to the generated SOQL' );
}

@isTest
private static void inSet_stringName_objectValues_whenCalled_addsAnInToTheGeneratedSoql() // NOPMD: Test method name format
{
ortoo_Criteria criteria = new ortoo_Criteria();

Test.startTest();
criteria.inSet( 'Account.Name', new Set<Object>{ 1, 2 } );
Test.stopTest();

String expected = 'Account.Name IN (1,2)';
String got = criteria.toSOQL();

System.assertEquals( expected, got, 'inSet, when called with a string field name and a set of DateTimes, will add an IN to the generated SOQL' );
}

}

0 comments on commit 58a1c78

Please sign in to comment.