From 52b98d34aa2b6262baebc5c1f0906dcc8684dc0f Mon Sep 17 00:00:00 2001 From: Tom Fuda Date: Thu, 12 Feb 2015 14:07:27 -0500 Subject: [PATCH 1/2] Modifies test assertions so they don't depend on fields being returned in a specific order. This is to fix intermittent unit test failure like this one: fflib_SObjectSelectorTest.testDefaultConfig -- System.AssertException: Assertion Failed: Expected: AccountNumber,AnnualRevenue,Id,Name, Actual: AccountNumber,Name,AnnualRevenue,Id --- .../src/classes/fflib_SObjectSelectorTest.cls | 62 ++++++++++++------- 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/fflib/src/classes/fflib_SObjectSelectorTest.cls b/fflib/src/classes/fflib_SObjectSelectorTest.cls index 3dd99c59484..86c5d99bb8c 100644 --- a/fflib/src/classes/fflib_SObjectSelectorTest.cls +++ b/fflib/src/classes/fflib_SObjectSelectorTest.cls @@ -31,19 +31,10 @@ private with sharing class fflib_SObjectSelectorTest static testMethod void testGetFieldListString() { Testfflib_SObjectSelector selector = new Testfflib_SObjectSelector(); - List fieldList = selector.getFieldListString().split(','); - Set fieldSet = new Set(fieldList); - system.assertEquals(Userinfo.isMultiCurrencyOrganization() ? 5 : 4, fieldSet.size()); - system.assert(fieldSet.contains('Name')); - system.assert(fieldSet.contains('Id')); - system.assert(fieldSet.contains('AccountNumber')); - system.assert(fieldSet.contains('AnnualRevenue')); - if(UserInfo.isMultiCurrencyOrganization()) - system.assert(fieldSet.contains('CurrencyIsoCode')); - - String relatedFieldListString = Userinfo.isMultiCurrencyOrganization() ? 'myprefix.AccountNumber,myprefix.CurrencyIsoCode,myprefix.AnnualRevenue,myprefix.Id,myprefix.Name' - : 'myprefix.AccountNumber,myprefix.AnnualRevenue,myprefix.Id,myprefix.Name'; - system.assertEquals(relatedFieldListString, selector.getRelatedFieldListString('myprefix')); + String fieldListString = selector.getFieldListString(); + assertFieldListString(fieldListString, null); + String relatedFieldListString = selector.getRelatedFieldListString('myprefix'); + assertFieldListString(relatedFieldListString, 'myprefix'); } static testMethod void testGetSObjectName() @@ -168,9 +159,17 @@ private with sharing class fflib_SObjectSelectorTest static testMethod void testSOQL() { Testfflib_SObjectSelector selector = new Testfflib_SObjectSelector(); - String soql = Userinfo.isMultiCurrencyOrganization() ? 'SELECT AccountNumber, CurrencyIsoCode, AnnualRevenue, Id, Name FROM Account ORDER BY Name DESC NULLS FIRST , AnnualRevenue ASC NULLS FIRST ' - : 'SELECT AccountNumber, AnnualRevenue, Id, Name FROM Account ORDER BY Name DESC NULLS FIRST , AnnualRevenue ASC NULLS FIRST '; - System.assertEquals(soql, selector.newQueryFactory().toSOQL()); + String soql = selector.newQueryFactory().toSOQL(); + Pattern p = Pattern.compile('SELECT (.*) FROM (.*) ORDER BY (.*)'); + Matcher m = p.matcher(soql); + System.assert(m.matches(), 'Generated SOQL does not match expected pattern. Here is the generated SOQL: ' + soql); + System.assertEquals(3, m.groupCount(), 'Unexpected number of groups captured.'); + String fieldListString = m.group(1); + assertFieldListString(fieldListString, null); + String fromString = m.group(2); + System.assertEquals('Account', fromString); + String orderByString = m.group(3); + System.assertEquals('Name DESC NULLS FIRST , AnnualRevenue ASC NULLS FIRST ', orderByString); } static testMethod void testDefaultConfig() @@ -180,19 +179,36 @@ private with sharing class fflib_SObjectSelectorTest System.assertEquals(true, selector.isEnforcingCRUD()); System.assertEquals(false, selector.isIncludeFieldSetFields()); - String fieldListString = Userinfo.isMultiCurrencyOrganization() ? 'AccountNumber,CurrencyIsoCode,AnnualRevenue,Id,Name' - : 'AccountNumber,AnnualRevenue,Id,Name'; - System.assertEquals(fieldListString, selector.getFieldListBuilder().getStringValue()); - System.assertEquals(fieldListString, selector.getFieldListString()); + String fieldListString = selector.getFieldListString(); + assertFieldListString(fieldListString, null); - String relatedFieldListString = Userinfo.isMultiCurrencyOrganization() ? 'LookupField__r.AccountNumber,LookupField__r.CurrencyIsoCode,LookupField__r.AnnualRevenue,LookupField__r.Id,LookupField__r.Name' - : 'LookupField__r.AccountNumber,LookupField__r.AnnualRevenue,LookupField__r.Id,LookupField__r.Name'; - System.assertEquals(relatedFieldListString, selector.getRelatedFieldListString('LookupField__r')); + String relatedFieldListString = selector.getRelatedFieldListString('LookupField__r'); + assertFieldListString(relatedFieldListString, 'LookupField__r'); System.assertEquals('Account', selector.getSObjectName()); System.assertEquals(Account.SObjectType, selector.getSObjectType2()); } + private static void assertFieldListString(String fieldListString, String prefix) { + String prefixString = (!String.isBlank(prefix)) ? prefix + '.' : ''; + List fieldList = fieldListString.split(',{1}\\s?'); + System.assertEquals(UserInfo.isMultiCurrencyOrganization() ? 5 : 4, fieldList.size()); + Set fieldSet = new Set(); + fieldSet.addAll(fieldList); + String expected = prefixString + 'AccountNumber'; + System.assert(fieldSet.contains(expected), expected + ' missing from field list string: ' + fieldListString); + expected = prefixString + 'AnnualRevenue'; + System.assert(fieldSet.contains(expected), expected + ' missing from field list string: ' + fieldListString); + expected = prefixString + 'Id'; + System.assert(fieldSet.contains(expected), expected + ' missing from field list string: ' + fieldListString); + expected = prefixString + 'Name'; + System.assert(fieldSet.contains(expected), expected + ' missing from field list string: ' + fieldListString); + if (UserInfo.isMultiCurrencyOrganization()) { + expected = prefixString + 'CurrencyIsoCode'; + System.assert(fieldSet.contains(expected), expected + ' missing from field list string: ' + fieldListString); + } + } + private class Testfflib_SObjectSelector extends fflib_SObjectSelector { public Testfflib_SObjectSelector() From 66d7c024abae3355719fc4e787259267986bd338 Mon Sep 17 00:00:00 2001 From: Tom Fuda Date: Fri, 13 Feb 2015 06:46:36 -0500 Subject: [PATCH 2/2] Simplifies testSOQL. --- fflib/src/classes/fflib_SObjectSelectorTest.cls | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/fflib/src/classes/fflib_SObjectSelectorTest.cls b/fflib/src/classes/fflib_SObjectSelectorTest.cls index 86c5d99bb8c..251dc8f77a8 100644 --- a/fflib/src/classes/fflib_SObjectSelectorTest.cls +++ b/fflib/src/classes/fflib_SObjectSelectorTest.cls @@ -160,16 +160,12 @@ private with sharing class fflib_SObjectSelectorTest { Testfflib_SObjectSelector selector = new Testfflib_SObjectSelector(); String soql = selector.newQueryFactory().toSOQL(); - Pattern p = Pattern.compile('SELECT (.*) FROM (.*) ORDER BY (.*)'); + Pattern p = Pattern.compile('SELECT (.*) FROM Account ORDER BY Name DESC NULLS FIRST , AnnualRevenue ASC NULLS FIRST '); Matcher m = p.matcher(soql); System.assert(m.matches(), 'Generated SOQL does not match expected pattern. Here is the generated SOQL: ' + soql); - System.assertEquals(3, m.groupCount(), 'Unexpected number of groups captured.'); + System.assertEquals(1, m.groupCount(), 'Unexpected number of groups captured.'); String fieldListString = m.group(1); assertFieldListString(fieldListString, null); - String fromString = m.group(2); - System.assertEquals('Account', fromString); - String orderByString = m.group(3); - System.assertEquals('Name DESC NULLS FIRST , AnnualRevenue ASC NULLS FIRST ', orderByString); } static testMethod void testDefaultConfig()