@@ -159,6 +159,18 @@ private with sharing class fflib_SObjectSelectorTest
159159 assertFieldListString (fieldListString , null );
160160 }
161161
162+ static testMethod void testSOQL_defaultSorting ()
163+ {
164+ Testfflib_SObjectSelectorDefaultSorting selector = new Testfflib_SObjectSelectorDefaultSorting (false );
165+ String soql = selector .newQueryFactory ().toSOQL ();
166+ Pattern p = Pattern .compile (' SELECT (.*) FROM Account ORDER BY Name ASC NULLS FIRST ' );
167+ Matcher m = p .matcher (soql );
168+ System .assert (m .matches (), ' Generated SOQL does not match expected pattern. Here is the generated SOQL: ' + soql );
169+ System .assertEquals (1 , m .groupCount (), ' Unexpected number of groups captured.' );
170+ String fieldListString = m .group (1 );
171+ assertFieldListString (fieldListString , null );
172+ }
173+
162174 static testMethod void testDefaultConfig ()
163175 {
164176 Testfflib_SObjectSelector selector = new Testfflib_SObjectSelector ();
@@ -239,7 +251,122 @@ private with sharing class fflib_SObjectSelectorTest
239251 Matcher soqlMatcher = soqlPattern .matcher (soql );
240252 system .assert (soqlMatcher .matches (), ' The SOQL should have that expected.' );
241253 }
242-
254+
255+ @IsTest
256+ static void testConfigureQueryFactoryFields () {
257+ // Given
258+ Testfflib_UserSObjectSelector selector = new Testfflib_UserSObjectSelector ();
259+ fflib_QueryFactory qf = new fflib_QueryFactory (Account .SObjectType );
260+
261+ Set <String > expectedSelectFields = new Set <String >{ ' Owner.Name' , ' Owner.Id' , ' Owner.Username' , ' Owner.LastLoginDate' };
262+ if (UserInfo .isMultiCurrencyOrganization ())
263+ {
264+ expectedSelectFields .add (' Owner.CurrencyIsoCode' );
265+ }
266+
267+ // When
268+ selector .configureQueryFactoryFields (qf , ' Owner' );
269+
270+ // Then
271+ String soql = qf .toSOQL ();
272+ Pattern soqlPattern = Pattern .compile (' SELECT (.*) FROM Account' );
273+ Matcher soqlMatcher = soqlPattern .matcher (soql );
274+ System .assert (soqlMatcher .matches (), ' Generated SOQL does not match expected pattern. Here is the generated SOQL: ' + soql );
275+
276+ List <String > actualSelectFields = soqlMatcher .group (1 ).deleteWhiteSpace ().split (' ,' );
277+ System .assertEquals (expectedSelectFields , new Set <String >(actualSelectFields ));
278+ }
279+
280+ @IsTest
281+ static void testAddQueryFactorySubselect () {
282+ // Given
283+ Testfflib_UserSObjectSelector selector = new Testfflib_UserSObjectSelector ();
284+ fflib_QueryFactory qf = new fflib_QueryFactory (Account .SObjectType );
285+
286+ Set <String > expectedSelectFields = new Set <String >{ ' Name' , ' Id' , ' Username' , ' LastLoginDate' };
287+ if (UserInfo .isMultiCurrencyOrganization ())
288+ {
289+ expectedSelectFields .add (' CurrencyIsoCode' );
290+ }
291+
292+ // When
293+ selector .addQueryFactorySubselect (qf );
294+
295+ // Then
296+ String soql = qf .toSOQL ();
297+ Pattern soqlPattern = Pattern .compile (' SELECT Id, \\ (SELECT (.*) FROM Users ORDER BY Name ASC NULLS FIRST \\ ) +FROM Account' );
298+ Matcher soqlMatcher = soqlPattern .matcher (soql );
299+ System .assert (soqlMatcher .matches (), ' Generated SOQL does not match expected pattern. Here is the generated SOQL: ' + soql );
300+
301+ List <String > actualSelectFields = soqlMatcher .group (1 ).deleteWhiteSpace ().split (' ,' );
302+ System .assertEquals (expectedSelectFields , new Set <String >(actualSelectFields ));
303+ }
304+
305+ @IsTest
306+ static void testAddQueryFactorySubselect2 () {
307+ // Given
308+ Testfflib_UserSObjectSelector selector = new Testfflib_UserSObjectSelector ();
309+ fflib_QueryFactory qf = new fflib_QueryFactory (Account .SObjectType );
310+
311+ Set <String > expectedSelectFields = new Set <String >{ ' Name' , ' Id' , ' Username' , ' LastLoginDate' };
312+ if (UserInfo .isMultiCurrencyOrganization ())
313+ {
314+ expectedSelectFields .add (' CurrencyIsoCode' );
315+ }
316+
317+ // When
318+ selector .addQueryFactorySubselect (qf , ' Users' );
319+
320+ // Then
321+ String soql = qf .toSOQL ();
322+ Pattern soqlPattern = Pattern .compile (' SELECT Id, \\ (SELECT (.*) FROM Users ORDER BY Name ASC NULLS FIRST \\ ) +FROM Account' );
323+ Matcher soqlMatcher = soqlPattern .matcher (soql );
324+ System .assert (soqlMatcher .matches (), ' Generated SOQL does not match expected pattern. Here is the generated SOQL: ' + soql );
325+
326+ List <String > actualSelectFields = soqlMatcher .group (1 ).deleteWhiteSpace ().split (' ,' );
327+ System .assertEquals (expectedSelectFields , new Set <String >(actualSelectFields ));
328+ }
329+
330+ @IsTest
331+ static void testGetFieldListString () {
332+ // Given
333+ Testfflib_UserSObjectSelector selector = new Testfflib_UserSObjectSelector ();
334+ fflib_QueryFactory qf = new fflib_QueryFactory (Account .SObjectType );
335+
336+ Set <String > expectedSelectFields = new Set <String >{ ' Name' , ' Id' , ' Username' , ' LastLoginDate' };
337+ if (UserInfo .isMultiCurrencyOrganization ())
338+ {
339+ expectedSelectFields .add (' CurrencyIsoCode' );
340+ }
341+
342+ // When
343+ String fieldListString = selector .getFieldListString ();
344+
345+ // Then
346+ List <String > actualSelectFields = fieldListString .deleteWhiteSpace ().split (' ,' );
347+ System .assertEquals (expectedSelectFields , new Set <String >(actualSelectFields ));
348+ }
349+
350+ @IsTest
351+ static void testGetRelatedFieldListString () {
352+ // Given
353+ Testfflib_UserSObjectSelector selector = new Testfflib_UserSObjectSelector ();
354+
355+ Set <String > expectedSelectFields = new Set <String >{ ' Owner.Name' , ' Owner.Id' , ' Owner.Username' , ' Owner.LastLoginDate' };
356+ if (UserInfo .isMultiCurrencyOrganization ())
357+ {
358+ expectedSelectFields .add (' Owner.CurrencyIsoCode' );
359+ }
360+
361+ // When
362+ String fieldListString = selector .getRelatedFieldListString (' Owner' );
363+
364+ // Then
365+ List <String > actualSelectFields = fieldListString .deleteWhiteSpace ().split (' ,' );
366+ System .assertEquals (expectedSelectFields , new Set <String >(actualSelectFields ));
367+
368+ }
369+
243370 private static void assertEqualsSelectFields (String expectedSelectFields , String actualSelectFields )
244371 {
245372 Set <String > expected = new Set <String >(expectedSelectFields .deleteWhiteSpace ().split (' ,' ));
@@ -281,6 +408,53 @@ private with sharing class fflib_SObjectSelectorTest
281408 }
282409 }
283410
411+ private class Testfflib_UserSObjectSelector extends fflib_SObjectSelector
412+ {
413+ public Testfflib_UserSObjectSelector ()
414+ {
415+ super ();
416+ }
417+
418+ public List <Schema .SObjectField > getSObjectFieldList ()
419+ {
420+ return new List <Schema .SObjectField > {
421+ User .Name ,
422+ User .Id ,
423+ User .Username ,
424+ User .LastLoginDate
425+ };
426+ }
427+
428+ public Schema.SObjectType getSObjectType ()
429+ {
430+ return User .SObjectType ;
431+ }
432+
433+ }
434+
435+ private class Testfflib_SObjectSelectorDefaultSorting extends fflib_SObjectSelector
436+ {
437+ public Testfflib_SObjectSelectorDefaultSorting (Boolean includeFieldSetFields )
438+ {
439+ super (includeFieldSetFields );
440+ }
441+
442+ public List <Schema .SObjectField > getSObjectFieldList ()
443+ {
444+ return new List <Schema .SObjectField > {
445+ Account .Name ,
446+ Account .Id ,
447+ Account .AccountNumber ,
448+ Account .AnnualRevenue
449+ };
450+ }
451+
452+ public Schema.SObjectType getSObjectType ()
453+ {
454+ return Account .sObjectType ;
455+ }
456+ }
457+
284458 /**
285459 * Create test user
286460 **/
@@ -301,4 +475,4 @@ private with sharing class fflib_SObjectSelectorTest
301475 }
302476 return testUser ;
303477 }
304- }
478+ }
0 commit comments