Skip to content

Commit 4294ab9

Browse files
committed
Merge pull request #33 from tfuda/master
Addresses intermittent unit test failures due to unexpected order of fields
2 parents 8e3013f + 66d7c02 commit 4294ab9

File tree

1 file changed

+35
-23
lines changed

1 file changed

+35
-23
lines changed

fflib/src/classes/fflib_SObjectSelectorTest.cls

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,10 @@ private with sharing class fflib_SObjectSelectorTest
3131
static testMethod void testGetFieldListString()
3232
{
3333
Testfflib_SObjectSelector selector = new Testfflib_SObjectSelector();
34-
List<String> fieldList = selector.getFieldListString().split(',');
35-
Set<String> fieldSet = new Set<String>(fieldList);
36-
system.assertEquals(Userinfo.isMultiCurrencyOrganization() ? 5 : 4, fieldSet.size());
37-
system.assert(fieldSet.contains('Name'));
38-
system.assert(fieldSet.contains('Id'));
39-
system.assert(fieldSet.contains('AccountNumber'));
40-
system.assert(fieldSet.contains('AnnualRevenue'));
41-
if(UserInfo.isMultiCurrencyOrganization())
42-
system.assert(fieldSet.contains('CurrencyIsoCode'));
43-
44-
String relatedFieldListString = Userinfo.isMultiCurrencyOrganization() ? 'myprefix.AccountNumber,myprefix.CurrencyIsoCode,myprefix.AnnualRevenue,myprefix.Id,myprefix.Name'
45-
: 'myprefix.AccountNumber,myprefix.AnnualRevenue,myprefix.Id,myprefix.Name';
46-
system.assertEquals(relatedFieldListString, selector.getRelatedFieldListString('myprefix'));
34+
String fieldListString = selector.getFieldListString();
35+
assertFieldListString(fieldListString, null);
36+
String relatedFieldListString = selector.getRelatedFieldListString('myprefix');
37+
assertFieldListString(relatedFieldListString, 'myprefix');
4738
}
4839

4940
static testMethod void testGetSObjectName()
@@ -168,9 +159,13 @@ private with sharing class fflib_SObjectSelectorTest
168159
static testMethod void testSOQL()
169160
{
170161
Testfflib_SObjectSelector selector = new Testfflib_SObjectSelector();
171-
String soql = Userinfo.isMultiCurrencyOrganization() ? 'SELECT AccountNumber, CurrencyIsoCode, AnnualRevenue, Id, Name FROM Account ORDER BY Name DESC NULLS FIRST , AnnualRevenue ASC NULLS FIRST '
172-
: 'SELECT AccountNumber, AnnualRevenue, Id, Name FROM Account ORDER BY Name DESC NULLS FIRST , AnnualRevenue ASC NULLS FIRST ';
173-
System.assertEquals(soql, selector.newQueryFactory().toSOQL());
162+
String soql = selector.newQueryFactory().toSOQL();
163+
Pattern p = Pattern.compile('SELECT (.*) FROM Account ORDER BY Name DESC NULLS FIRST , AnnualRevenue ASC NULLS FIRST ');
164+
Matcher m = p.matcher(soql);
165+
System.assert(m.matches(), 'Generated SOQL does not match expected pattern. Here is the generated SOQL: ' + soql);
166+
System.assertEquals(1, m.groupCount(), 'Unexpected number of groups captured.');
167+
String fieldListString = m.group(1);
168+
assertFieldListString(fieldListString, null);
174169
}
175170

176171
static testMethod void testDefaultConfig()
@@ -180,19 +175,36 @@ private with sharing class fflib_SObjectSelectorTest
180175
System.assertEquals(true, selector.isEnforcingCRUD());
181176
System.assertEquals(false, selector.isIncludeFieldSetFields());
182177

183-
String fieldListString = Userinfo.isMultiCurrencyOrganization() ? 'AccountNumber,CurrencyIsoCode,AnnualRevenue,Id,Name'
184-
: 'AccountNumber,AnnualRevenue,Id,Name';
185-
System.assertEquals(fieldListString, selector.getFieldListBuilder().getStringValue());
186-
System.assertEquals(fieldListString, selector.getFieldListString());
178+
String fieldListString = selector.getFieldListString();
179+
assertFieldListString(fieldListString, null);
187180

188-
String relatedFieldListString = Userinfo.isMultiCurrencyOrganization() ? 'LookupField__r.AccountNumber,LookupField__r.CurrencyIsoCode,LookupField__r.AnnualRevenue,LookupField__r.Id,LookupField__r.Name'
189-
: 'LookupField__r.AccountNumber,LookupField__r.AnnualRevenue,LookupField__r.Id,LookupField__r.Name';
190-
System.assertEquals(relatedFieldListString, selector.getRelatedFieldListString('LookupField__r'));
181+
String relatedFieldListString = selector.getRelatedFieldListString('LookupField__r');
182+
assertFieldListString(relatedFieldListString, 'LookupField__r');
191183

192184
System.assertEquals('Account', selector.getSObjectName());
193185
System.assertEquals(Account.SObjectType, selector.getSObjectType2());
194186
}
195187

188+
private static void assertFieldListString(String fieldListString, String prefix) {
189+
String prefixString = (!String.isBlank(prefix)) ? prefix + '.' : '';
190+
List<String> fieldList = fieldListString.split(',{1}\\s?');
191+
System.assertEquals(UserInfo.isMultiCurrencyOrganization() ? 5 : 4, fieldList.size());
192+
Set<String> fieldSet = new Set<String>();
193+
fieldSet.addAll(fieldList);
194+
String expected = prefixString + 'AccountNumber';
195+
System.assert(fieldSet.contains(expected), expected + ' missing from field list string: ' + fieldListString);
196+
expected = prefixString + 'AnnualRevenue';
197+
System.assert(fieldSet.contains(expected), expected + ' missing from field list string: ' + fieldListString);
198+
expected = prefixString + 'Id';
199+
System.assert(fieldSet.contains(expected), expected + ' missing from field list string: ' + fieldListString);
200+
expected = prefixString + 'Name';
201+
System.assert(fieldSet.contains(expected), expected + ' missing from field list string: ' + fieldListString);
202+
if (UserInfo.isMultiCurrencyOrganization()) {
203+
expected = prefixString + 'CurrencyIsoCode';
204+
System.assert(fieldSet.contains(expected), expected + ' missing from field list string: ' + fieldListString);
205+
}
206+
}
207+
196208
private class Testfflib_SObjectSelector extends fflib_SObjectSelector
197209
{
198210
public Testfflib_SObjectSelector()

0 commit comments

Comments
 (0)