Improve fflib_SObjects2.getSObjectsByIdField

Add unit-test and method to domain template

Signed-off-by: WimVelzeboer <wimvelzeboer@protonmail.com>
This commit is contained in:
WimVelzeboer
2022-02-24 09:43:38 +00:00
parent acb5cdc98b
commit 969b0fbba7
3 changed files with 34 additions and 3 deletions
@@ -703,8 +703,8 @@ public virtual class fflib_SObjects2
* @return Returns a map with the records mapped to the given String field value
*
* @example
* Account account = Account.newInstance(records);
* Map<String, SObject> accountByName = account.getSObjectsByStringField(Account.AccountName);
* Accounts accounts = Account.newInstance(records);
* Map<String, List<SObject>> accountByName = accounts.getSObjectsByStringField(Account.AccountName);
*/
@TestVisible
protected virtual Map<String, List<SObject>> getSObjectsByStringField(Schema.SObjectField sObjectField)
@@ -112,7 +112,6 @@ private class fflib_SObjects2Test
domain.getDoubleFieldValues(Account.BillingLatitude);
domain.getDoubleFieldValues(Account.BillingLatitude, criteria);
domain.getSObjectByIdField(Account.Id);
domain.getSObjectsByIdField(Account.Id);
domain.getSObjectByStringField(Account.Name);
domain.getSObjectsByStringField(Account.Name);
domain.getStringFieldByStringField(Account.Name, Account.AccountNumber);
@@ -516,6 +515,27 @@ private class fflib_SObjects2Test
System.assertEquals(accountName, (String) results.get(0).get(Schema.Account.Name));
}
@IsTest
static void itShouldGetRecordsByIdFieldValues()
{
final Id accountIdA = fflib_IDGenerator.generate(Schema.Account.SObjectType);
final Id accountIdB = fflib_IDGenerator.generate(Schema.Account.SObjectType);
List<Contact> records = new List<Contact> {
new Contact(AccountId = accountIdA),
new Contact(AccountId = accountIdA),
new Contact(AccountId = accountIdB),
new Contact(AccountId = accountIdB),
new Contact(AccountId = accountIdB)
};
fflib_SObjects2 domain = new fflib_SObjects2(records);
Map<Id, List<SObject>> results = domain.getSObjectsByIdField(Schema.Contact.AccountId);
System.assertEquals(2, results.size());
System.assertEquals(2, results.get(accountIdA).size());
System.assertEquals(3, results.get(accountIdB).size());
}
@IsTest
static void itShouldSetField()
{