diff --git a/framework/default/fflib-apex-extensions/default/classes/domains/fflib_SObjects2.cls b/framework/default/fflib-apex-extensions/default/classes/domains/fflib_SObjects2.cls index cb93eb772c6..e379261dbdc 100644 --- a/framework/default/fflib-apex-extensions/default/classes/domains/fflib_SObjects2.cls +++ b/framework/default/fflib-apex-extensions/default/classes/domains/fflib_SObjects2.cls @@ -120,7 +120,7 @@ public virtual class fflib_SObjects2 * * @return Return the SObject records contained in the domain matching the criteria */ - public virtual List getRecords(fflib_Criteria criteria) + public virtual List getRecords(fflib_Criteria.Evaluator criteria) { List result = new List(); for (SObject record : getRecords()) diff --git a/framework/default/ortoo-core/default/classes/fflib-extension/ortoo_Criteria.cls b/framework/default/ortoo-core/default/classes/fflib-extension/ortoo_Criteria.cls index fb0ecfd84e1..77978eaccb1 100644 --- a/framework/default/ortoo-core/default/classes/fflib-extension/ortoo_Criteria.cls +++ b/framework/default/ortoo-core/default/classes/fflib-extension/ortoo_Criteria.cls @@ -7,23 +7,6 @@ public inherited sharing virtual class ortoo_Criteria implements ISearchCriteria { fflib_Criteria criteria; - // TODO: write tests for SOQL generation of: - // orCriteria - // andCriteria - // andOrCriteria - // andAndCriteria - // formulaCriteria - // - // equalTo - // notEqualTo - // greaterOrEqualTo - // greaterThan - // lessOrEqualTo - // lessThan - // - // TODO: consider a test for filtering of records? - // TODO: change SObjects2 to allow ortoo_Criteria - maybe define an interface instead - public ortoo_Criteria() { criteria = new fflib_Criteria(); @@ -106,7 +89,7 @@ public inherited sharing virtual class ortoo_Criteria implements ISearchCriteria * .equalTo(Account.AccountNumber, '0002')) * * Evaluates: - * Account.Name = 'Example' AND (Account.AccountNumber = '0001' OR Account.AccountNumber = '0002') + * Name = 'Example' AND (AccountNumber = '0001' OR AccountNumber = '0002') */ public ortoo_Criteria addOrCriteria( ortoo_Criteria subCriteria ) { @@ -155,10 +138,10 @@ public inherited sharing virtual class ortoo_Criteria implements ISearchCriteria * .formulaCriteria('(1 OR 2) AND 3') * .equalTo(Account.AccountNumber, '0001') * .equalTo(Account.AccountNumber, '0002') - * .equalTo(Account.ShippingCountry, 'USA')) + * .equalTo(Account.ShippingCountry, 'USA') * * Evaluates: - * (AccountNumber = '0001' OR AccountNumber = '0001') AND ShippingCountry = 'USA' + * (AccountNumber = '0001' OR AccountNumber = '0002') AND ShippingCountry = 'USA' */ public ortoo_Criteria formulaCriteria( String formula ) { diff --git a/framework/default/ortoo-core/default/classes/fflib-extension/tests/ortoo_CriteriaTest.cls b/framework/default/ortoo-core/default/classes/fflib-extension/tests/ortoo_CriteriaTest.cls index b291384ede0..8e4cabf699f 100644 --- a/framework/default/ortoo-core/default/classes/fflib-extension/tests/ortoo_CriteriaTest.cls +++ b/framework/default/ortoo-core/default/classes/fflib-extension/tests/ortoo_CriteriaTest.cls @@ -1,6 +1,522 @@ @isTest private without sharing class ortoo_CriteriaTest { + @isTest + private static void byDefault_joinsCriteriaWithAnd() // NOPMD: Test method name format + { + ortoo_Criteria criteria = new ortoo_Criteria(); + + Test.startTest(); + String got = criteria + .equalTo(Account.Name, 'Example') + .equalTo(Account.AccountNumber, '1234567') + .toSoql(); + Test.stopTest(); + + String expected = 'Name=\'Example\' ANd AccountNumber=\'1234567\''; + + System.assertEquals( expected, got, 'by default, will join the generated SOQL with AND' ); + } + + @isTest + private static void orCriteria_whenGivenCriteria_resultsInOrsInTheGeneratedSoql() // NOPMD: Test method name format + { + ortoo_Criteria criteria = new ortoo_Criteria(); + + Test.startTest(); + String got = criteria + .orCriteria() + .equalTo(Account.Name, 'Example') + .equalTo(Account.AccountNumber, '1234567') + .toSoql(); + Test.stopTest(); + + String expected = 'Name=\'Example\' OR AccountNumber=\'1234567\''; + + System.assertEquals( expected, got, 'orCriteria, when given further criteria, will join the generated SOQL with OR' ); + } + + @isTest + private static void andCriteria_whenGivenCriteria_resultsInAndsInTheGeneratedSoql() // NOPMD: Test method name format + { + ortoo_Criteria criteria = new ortoo_Criteria(); + + Test.startTest(); + String got = criteria + .andCriteria() + .equalTo(Account.Name, 'Example') + .equalTo(Account.AccountNumber, '1234567') + .toSoql(); + Test.stopTest(); + + String expected = 'Name=\'Example\' AND AccountNumber=\'1234567\''; + + System.assertEquals( expected, got, 'andCriteria, when given further criteria, will join the generated SOQL with AND' ); + } + + @isTest + private static void andOrCriteria_whenGivenSubCriteria_addsTheSubCriteriaSetAsOr() // NOPMD: Test method name format + { + ortoo_Criteria criteria = new ortoo_Criteria(); + + Test.startTest(); + String got = criteria + .equalTo(Account.Name, 'Example') + .addOrCriteria( + new ortoo_Criteria() + .equalTo(Account.AccountNumber, '0001') + .equalTo(Account.AccountNumber, '0002')) + .toSoql(); + Test.stopTest(); + + String expected = 'Name=\'Example\' AND (AccountNumber=\'0001\' OR AccountNumber=\'0002\')'; + + System.assertEquals( expected, got, 'andOrCriteria, when given sub criteria, will add the subcriteria set as OR criteria' ); + } + + @isTest + private static void andAndCriteria_whenGivenSubCriteria_addsTheSubCriteriaSetAsAnd() // NOPMD: Test method name format + { + ortoo_Criteria criteria = new ortoo_Criteria(); + + Test.startTest(); + String got = criteria + .equalTo(Account.Name, 'Example') + .addAndCriteria( + new ortoo_Criteria() + .equalTo(Account.AccountNumber, '0001') + .equalTo(Account.AccountNumber, '0002')) + .toSoql(); + Test.stopTest(); + + String expected = 'Name=\'Example\' AND (AccountNumber=\'0001\' AND AccountNumber=\'0002\')'; + + System.assertEquals( expected, got, 'andAndCriteria, when given sub criteria, will add the subcriteria set as AND criteria' ); + } + + @isTest + private static void formulaCriteria_whenGivenCriteria_addsTheCriteriaInLineWithTheFormula() // NOPMD: Test method name format + { + ortoo_Criteria criteria = new ortoo_Criteria(); + + Test.startTest(); + String got = criteria + .formulaCriteria('(1 OR 2) AND 3') + .equalTo(Account.AccountNumber, '0001') + .equalTo(Account.AccountNumber, '0002') + .equalTo(Account.ShippingCountry, 'USA') + .toSoql(); + Test.stopTest(); + + String expected = '(AccountNumber=\'0001\' OR AccountNumber=\'0002\') AND ShippingCountry=\'USA\''; + + System.assertEquals( expected, got, 'formulaCriteria, when given futher criteria, will add the criteria in line with the formula' ); + } + + @isTest + private static void formulaCriteria_whenGivenRepeatedReferences_addsTheCriteriaInLineWithTheFormula() // NOPMD: Test method name format + { + ortoo_Criteria criteria = new ortoo_Criteria(); + + Test.startTest(); + String got = criteria + .formulaCriteria('(1 OR 2) AND (1 OR 3)') + .equalTo(Account.AccountNumber, '0001') + .equalTo(Account.AccountNumber, '0002') + .equalTo(Account.ShippingCountry, 'USA') + .toSoql(); + Test.stopTest(); + + String expected = '(AccountNumber=\'0001\' OR AccountNumber=\'0002\') AND (AccountNumber=\'0001\' OR ShippingCountry=\'USA\')'; + + System.assertEquals( expected, got, 'formulaCriteria, when given repeated references in the formula criteria, will add the repeated references' ); + } + + @isTest + private static void formulaCriteria_whenGivenFewerCriteriaThanReferenced_willThrowAnException() // NOPMD: Test method name format + { + ortoo_Criteria criteria = new ortoo_Criteria(); + + Test.startTest(); + String exceptionMessage; + try + { + String got = criteria + .formulaCriteria('(1 OR 2) AND 3') + .equalTo(Account.AccountNumber, '0001') + .equalTo(Account.AccountNumber, '0002') + .toSoql(); + } + catch ( fflib_Criteria.CriteriaException e ) + { + exceptionMessage = e.getMessage(); + } + Test.stopTest(); + + Amoss_Asserts.assertContains( 'Expected a formula expression with number: 3', exceptionMessage, 'formulaCriteria, when given a formula and fewer criteria than referenced, will throw an exception' ); + } + + @isTest + private static void equalTo_field_whenGivenAString_addsAnEqualsToTheGeneratedSoql() // NOPMD: Test method name format + { + ortoo_Criteria criteria = new ortoo_Criteria(); + + Test.startTest(); + criteria.equalTo( Contact.Name, 'contactName' ); + Test.stopTest(); + + String expected = 'Name=\'contactName\''; + String got = criteria.toSOQL(); + + System.assertEquals( expected, got, 'equalTo, when called with a field and a string value, will add a = to the generated SOQL' ); + } + + @isTest + private static void equalTo_field_whenGivenANumber_addsAnEqualsToTheGeneratedSoql() // NOPMD: Test method name format + { + ortoo_Criteria criteria = new ortoo_Criteria(); + + Test.startTest(); + criteria.equalTo( Contact.Name, 12 ); + Test.stopTest(); + + String expected = 'Name=12'; + String got = criteria.toSOQL(); + + System.assertEquals( expected, got, 'equalTo, when called with a field and a numeric value, will add a = to the generated SOQL' ); + } + + @isTest + private static void equalTo_stringName_whenGivenAString_addsAnEqualsToTheGeneratedSoql() // NOPMD: Test method name format + { + ortoo_Criteria criteria = new ortoo_Criteria(); + + Test.startTest(); + criteria.equalTo( 'Contact.Name', 'contactName' ); + Test.stopTest(); + + String expected = 'Contact.Name=\'contactName\''; + String got = criteria.toSOQL(); + + System.assertEquals( expected, got, 'equalTo, when called with a string field name and a string value, will add a = to the generated SOQL' ); + } + + @isTest + private static void equalTo_stringName_whenGivenANumber_addsAnEqualsToTheGeneratedSoql() // NOPMD: Test method name format + { + ortoo_Criteria criteria = new ortoo_Criteria(); + + Test.startTest(); + criteria.equalTo( 'Contact.Name', 12 ); + Test.stopTest(); + + String expected = 'Contact.Name=12'; + String got = criteria.toSOQL(); + + System.assertEquals( expected, got, 'equalTo, when called with a string field name and a numeric value, will add a = to the generated SOQL' ); + } + + @isTest + private static void notEqualTo_field_whenGivenAString_addsANotEqualsToTheGeneratedSoql() // NOPMD: Test method name format + { + ortoo_Criteria criteria = new ortoo_Criteria(); + + Test.startTest(); + criteria.notEqualTo( Contact.Name, 'contactName' ); + Test.stopTest(); + + String expected = 'Name!=\'contactName\''; + String got = criteria.toSOQL(); + + System.assertEquals( expected, got, 'notEqualTo, when called with a field and a string value, will add a != to the generated SOQL' ); + } + + @isTest + private static void notEqualTo_field_whenGivenANumber_addsANotEqualsToTheGeneratedSoql() // NOPMD: Test method name format + { + ortoo_Criteria criteria = new ortoo_Criteria(); + + Test.startTest(); + criteria.notEqualTo( Contact.Name, 12 ); + Test.stopTest(); + + String expected = 'Name!=12'; + String got = criteria.toSOQL(); + + System.assertEquals( expected, got, 'notEqualTo, when called with a field and a numeric value, will add a != to the generated SOQL' ); + } + + @isTest + private static void notEqualTo_stringName_whenGivenAString_addsANotEqualsToTheGeneratedSoql() // NOPMD: Test method name format + { + ortoo_Criteria criteria = new ortoo_Criteria(); + + Test.startTest(); + criteria.notEqualTo( 'Contact.Name', 'contactName' ); + Test.stopTest(); + + String expected = 'Contact.Name!=\'contactName\''; + String got = criteria.toSOQL(); + + System.assertEquals( expected, got, 'notEqualTo, when called with a string field name and a string value, will add a != to the generated SOQL' ); + } + + @isTest + private static void notEqualTo_stringName_whenGivenANumber_addsANotEqualsToTheGeneratedSoql() // NOPMD: Test method name format + { + ortoo_Criteria criteria = new ortoo_Criteria(); + + Test.startTest(); + criteria.notEqualTo( 'Contact.Name', 12 ); + Test.stopTest(); + + String expected = 'Contact.Name!=12'; + String got = criteria.toSOQL(); + + System.assertEquals( expected, got, 'notEqualTo, when called with a string field name and a numeric value, will add a != to the generated SOQL' ); + } + + @isTest + private static void greaterOrEqualTo_field_whenGivenAString_addsAClauseToTheGeneratedSoql() // NOPMD: Test method name format + { + ortoo_Criteria criteria = new ortoo_Criteria(); + + Test.startTest(); + criteria.greaterOrEqualTo( Contact.Name, 'contactName' ); + Test.stopTest(); + + String expected = 'Name>=\'contactName\''; + String got = criteria.toSOQL(); + + System.assertEquals( expected, got, 'greaterOrEqualTo, when called with a field and a string value, will add a >= to the generated SOQL' ); + } + + @isTest + private static void greaterOrEqualTo_field_whenGivenANumber_addsAClauseToTheGeneratedSoql() // NOPMD: Test method name format + { + ortoo_Criteria criteria = new ortoo_Criteria(); + + Test.startTest(); + criteria.greaterOrEqualTo( Contact.Name, 12 ); + Test.stopTest(); + + String expected = 'Name>=12'; + String got = criteria.toSOQL(); + + System.assertEquals( expected, got, 'greaterOrEqualTo, when called with a field and a numeric value, will add a >= to the generated SOQL' ); + } + + @isTest + private static void greaterOrEqualTo_stringName_whenGivenAString_addsAClauseToTheGeneratedSoql() // NOPMD: Test method name format + { + ortoo_Criteria criteria = new ortoo_Criteria(); + + Test.startTest(); + criteria.greaterOrEqualTo( 'Contact.Name', 'contactName' ); + Test.stopTest(); + + String expected = 'Contact.Name>=\'contactName\''; + String got = criteria.toSOQL(); + + System.assertEquals( expected, got, 'greaterOrEqualTo, when called with a string field name and a string value, will add a >= to the generated SOQL' ); + } + + @isTest + private static void greaterOrEqualTo_stringName_whenGivenANumber_addsAClauseToTheGeneratedSoql() // NOPMD: Test method name format + { + ortoo_Criteria criteria = new ortoo_Criteria(); + + Test.startTest(); + criteria.greaterOrEqualTo( 'Contact.Name', 12 ); + Test.stopTest(); + + String expected = 'Contact.Name>=12'; + String got = criteria.toSOQL(); + + System.assertEquals( expected, got, 'greaterOrEqualTo, when called with a string field name and a numeric value, will add a >= to the generated SOQL' ); + } + + @isTest + private static void greaterThan_field_whenGivenAString_addsAClauseToTheGeneratedSoql() // NOPMD: Test method name format + { + ortoo_Criteria criteria = new ortoo_Criteria(); + + Test.startTest(); + criteria.greaterThan( Contact.Name, 'contactName' ); + Test.stopTest(); + + String expected = 'Name>\'contactName\''; + String got = criteria.toSOQL(); + + System.assertEquals( expected, got, 'greaterThan, when called with a field and a string value, will add a > to the generated SOQL' ); + } + + @isTest + private static void greaterThan_field_whenGivenANumber_addsAClauseToTheGeneratedSoql() // NOPMD: Test method name format + { + ortoo_Criteria criteria = new ortoo_Criteria(); + + Test.startTest(); + criteria.greaterThan( Contact.Name, 12 ); + Test.stopTest(); + + String expected = 'Name>12'; + String got = criteria.toSOQL(); + + System.assertEquals( expected, got, 'greaterThan, when called with a field and a numeric value, will add a > to the generated SOQL' ); + } + + @isTest + private static void greaterThan_stringName_whenGivenAString_addsAClauseToTheGeneratedSoql() // NOPMD: Test method name format + { + ortoo_Criteria criteria = new ortoo_Criteria(); + + Test.startTest(); + criteria.greaterThan( 'Contact.Name', 'contactName' ); + Test.stopTest(); + + String expected = 'Contact.Name>\'contactName\''; + String got = criteria.toSOQL(); + + System.assertEquals( expected, got, 'greaterThan, when called with a string field name and a string value, will add a > to the generated SOQL' ); + } + + @isTest + private static void greaterThan_stringName_whenGivenANumber_addsAClauseToTheGeneratedSoql() // NOPMD: Test method name format + { + ortoo_Criteria criteria = new ortoo_Criteria(); + + Test.startTest(); + criteria.greaterThan( 'Contact.Name', 12 ); + Test.stopTest(); + + String expected = 'Contact.Name>12'; + String got = criteria.toSOQL(); + + System.assertEquals( expected, got, 'greaterThan, when called with a string field name and a numeric value, will add a > to the generated SOQL' ); + } + + @isTest + private static void lessOrEqualTo_field_whenGivenAString_addsAClauseToTheGeneratedSoql() // NOPMD: Test method name format + { + ortoo_Criteria criteria = new ortoo_Criteria(); + + Test.startTest(); + criteria.lessOrEqualTo( Contact.Name, 'contactName' ); + Test.stopTest(); + + String expected = 'Name<=\'contactName\''; + String got = criteria.toSOQL(); + + System.assertEquals( expected, got, 'lessOrEqualTo, when called with a field and a string value, will add a <= to the generated SOQL' ); + } + + @isTest + private static void lessOrEqualTo_field_whenGivenANumber_addsAClauseToTheGeneratedSoql() // NOPMD: Test method name format + { + ortoo_Criteria criteria = new ortoo_Criteria(); + + Test.startTest(); + criteria.lessOrEqualTo( Contact.Name, 12 ); + Test.stopTest(); + + String expected = 'Name<=12'; + String got = criteria.toSOQL(); + + System.assertEquals( expected, got, 'lessOrEqualTo, when called with a field and a numeric value, will add a <= to the generated SOQL' ); + } + + @isTest + private static void lessOrEqualTo_stringName_whenGivenAString_addsAClauseToTheGeneratedSoql() // NOPMD: Test method name format + { + ortoo_Criteria criteria = new ortoo_Criteria(); + + Test.startTest(); + criteria.lessOrEqualTo( 'Contact.Name', 'contactName' ); + Test.stopTest(); + + String expected = 'Contact.Name<=\'contactName\''; + String got = criteria.toSOQL(); + + System.assertEquals( expected, got, 'lessOrEqualTo, when called with a string field name and a string value, will add a <= to the generated SOQL' ); + } + + @isTest + private static void lessOrEqualTo_stringName_whenGivenANumber_addsAClauseToTheGeneratedSoql() // NOPMD: Test method name format + { + ortoo_Criteria criteria = new ortoo_Criteria(); + + Test.startTest(); + criteria.lessOrEqualTo( 'Contact.Name', 12 ); + Test.stopTest(); + + String expected = 'Contact.Name<=12'; + String got = criteria.toSOQL(); + + System.assertEquals( expected, got, 'lessOrEqualTo, when called with a string field name and a numeric value, will add a <= to the generated SOQL' ); + } + + + @isTest + private static void lessThan_field_whenGivenAString_addsAClauseToTheGeneratedSoql() // NOPMD: Test method name format + { + ortoo_Criteria criteria = new ortoo_Criteria(); + + Test.startTest(); + criteria.lessThan( Contact.Name, 'contactName' ); + Test.stopTest(); + + String expected = 'Name<\'contactName\''; + String got = criteria.toSOQL(); + + System.assertEquals( expected, got, 'lessThan, when called with a field and a string value, will add a < to the generated SOQL' ); + } + + @isTest + private static void lessThan_field_whenGivenANumber_addsAClauseToTheGeneratedSoql() // NOPMD: Test method name format + { + ortoo_Criteria criteria = new ortoo_Criteria(); + + Test.startTest(); + criteria.lessThan( Contact.Name, 12 ); + Test.stopTest(); + + String expected = 'Name<12'; + String got = criteria.toSOQL(); + + System.assertEquals( expected, got, 'lessThan, when called with a field and a numeric value, will add a < to the generated SOQL' ); + } + + @isTest + private static void lessThan_stringName_whenGivenAString_addsAClauseToTheGeneratedSoql() // NOPMD: Test method name format + { + ortoo_Criteria criteria = new ortoo_Criteria(); + + Test.startTest(); + criteria.lessThan( 'Contact.Name', 'contactName' ); + Test.stopTest(); + + String expected = 'Contact.Name<\'contactName\''; + String got = criteria.toSOQL(); + + System.assertEquals( expected, got, 'lessThan, when called with a string field name and a string value, will add a < to the generated SOQL' ); + } + + @isTest + private static void lessThan_stringName_whenGivenANumber_addsAClauseToTheGeneratedSoql() // NOPMD: Test method name format + { + ortoo_Criteria criteria = new ortoo_Criteria(); + + Test.startTest(); + criteria.lessThan( 'Contact.Name', 12 ); + Test.stopTest(); + + String expected = 'Contact.Name<12'; + String got = criteria.toSOQL(); + + System.assertEquals( expected, got, 'lessThan, when called with a string field name and a numeric value, will add a < to the generated SOQL' ); + } + @isTest private static void likeString_field_whenCalled_addsALikeToTheGeneratedSoql() // NOPMD: Test method name format {