Merge pull request #35 from wimvelzeboer/feature/MoreDomainMethods
Add domain methods
This commit is contained in:
@@ -585,6 +585,12 @@ public virtual class fflib_SObjects2
|
||||
.getLongFieldValues(sObjectField);
|
||||
}
|
||||
|
||||
@TestVisible
|
||||
protected virtual List<SObject> getRecords(Schema.SObjectField sObjectField, Id idValue)
|
||||
{
|
||||
return getRecords(sObjectField, new Set<Id>{ idValue });
|
||||
}
|
||||
|
||||
@TestVisible
|
||||
protected virtual List<SObject> getRecords(Schema.SObjectField sObjectField, Set<Id> values)
|
||||
{
|
||||
@@ -599,6 +605,12 @@ public virtual class fflib_SObjects2
|
||||
return result;
|
||||
}
|
||||
|
||||
@TestVisible
|
||||
protected virtual List<SObject> getRecords(Schema.SObjectField sObjectField, String value)
|
||||
{
|
||||
return getRecords(sObjectField, new Set<String>{ value });
|
||||
}
|
||||
|
||||
@TestVisible
|
||||
protected virtual List<SObject> getRecords(Schema.SObjectField sObjectField, Set<String> values)
|
||||
{
|
||||
@@ -613,17 +625,35 @@ public virtual class fflib_SObjects2
|
||||
return result;
|
||||
}
|
||||
|
||||
protected virtual List<SObject> getRecordsByFieldValueIsNot(Schema.SObjectField sObjectField, Object value)
|
||||
@TestVisible
|
||||
protected virtual List<SObject> getRecordsIsNot(Schema.SObjectField sObjectField, String value)
|
||||
{
|
||||
return getRecordsByFieldValuesNotIn(sObjectField, new Set<Object>{ value });
|
||||
return getRecordsNotIn(sObjectField, new Set<String>{ value });
|
||||
}
|
||||
|
||||
protected virtual List<SObject> getRecordsByFieldValuesNotIn(Schema.SObjectField sObjectField, Set<Object> values)
|
||||
@TestVisible
|
||||
protected virtual List<SObject> getRecordsNotIn(Schema.SObjectField sObjectField, Set<String> values)
|
||||
{
|
||||
List<SObject> result = new List<SObject>();
|
||||
for (SObject record : getRecords())
|
||||
{
|
||||
if (values?.contains(record.get(sObjectField)) == false)
|
||||
if (values?.contains((String) record.get(sObjectField))) continue;
|
||||
result.add(record);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
protected virtual List<SObject> getRecordsIsNot(Schema.SObjectField sObjectField, Id idValue)
|
||||
{
|
||||
return getRecordsNotIn(sObjectField, new Set<Id>{ idValue });
|
||||
}
|
||||
|
||||
protected virtual List<SObject> getRecordsNotIn(Schema.SObjectField sObjectField, Set<Id> idValues)
|
||||
{
|
||||
List<SObject> result = new List<SObject>();
|
||||
for (SObject record : getRecords())
|
||||
{
|
||||
if (idValues?.contains((Id) record.get(sObjectField)) == false)
|
||||
{
|
||||
result.add(record);
|
||||
}
|
||||
@@ -767,6 +797,66 @@ public virtual class fflib_SObjects2
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a map with the given Date field value mapped to the given Id field
|
||||
* Key fields containing null values are omitted
|
||||
*
|
||||
* @param valueField The Date field to use as value for the map
|
||||
* @param keyField The Id field to use as key for the map
|
||||
*
|
||||
* @return a map with the given Date field value mapped to the given Id field
|
||||
*
|
||||
* @example
|
||||
* Account account = Account.newInstance(records);
|
||||
* Map<Id, Date> createdDateById = account.getDateFieldByIdField(Account.CreatedDate, Account.Id);
|
||||
*/
|
||||
@TestVisible
|
||||
protected virtual Map<Id, Date> getDateFieldByIdField(Schema.SObjectField valueField, Schema.SObjectField keyField)
|
||||
{
|
||||
Map<Id, Date> result = new Map<Id, Date>();
|
||||
Map<Object, Object> fieldByField = getFieldByField(valueField, keyField);
|
||||
for (Object obj : fieldByField.keySet())
|
||||
{
|
||||
Object value = fieldByField.get(obj);
|
||||
Date dateValue;
|
||||
if (value instanceof Date)
|
||||
{
|
||||
dateValue = (Date) value;
|
||||
}
|
||||
else if (value != null)
|
||||
{
|
||||
dateValue = ((Datetime) value).date();
|
||||
}
|
||||
result.put((Id) obj, dateValue);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a map with the given Date field value mapped to the given Id field
|
||||
* Key fields containing null values are omitted
|
||||
*
|
||||
* @param valueField The DateTime field to use as value for the map
|
||||
* @param keyField The Id field to use as key for the map
|
||||
*
|
||||
* @return a map with the given Date field value mapped to the given Id field
|
||||
*
|
||||
* @example
|
||||
* Account account = Account.newInstance(records);
|
||||
* Map<Id, Datetime> createdDateById = account.getDateFieldByIdField(Account.CreatedDate, Account.Id);
|
||||
*/
|
||||
@TestVisible
|
||||
protected virtual Map<Id, Datetime> getDateTimeFieldByIdField(Schema.SObjectField valueField, Schema.SObjectField keyField)
|
||||
{
|
||||
Map<Id, Datetime> result = new Map<Id, Datetime>();
|
||||
Map<Object, Object> fieldByField = getFieldByField(valueField, keyField);
|
||||
for (Object obj : fieldByField.keySet())
|
||||
{
|
||||
result.put((Id) obj, (Datetime) fieldByField.get(obj));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a map with the records mapped to the given String field value
|
||||
* Key fields containing null values are omitted
|
||||
|
||||
@@ -112,6 +112,7 @@ 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);
|
||||
@@ -155,6 +156,29 @@ private class fflib_SObjects2Test
|
||||
System.assertEquals(accountName, result.get(accountId), 'Incorrect returned account name');
|
||||
}
|
||||
|
||||
@IsTest
|
||||
static void itShouldReturnDateFieldByIdField()
|
||||
{
|
||||
final Date today = Date.today();
|
||||
final Datetime todayDatetime = Datetime.newInstance(today.year(), today.month(), today.day());
|
||||
final String accountName = 'My Account';
|
||||
final Id accountId = fflib_IDGenerator.generate(Account.SObjectType);
|
||||
DomainAccounts domain = new DomainAccounts(
|
||||
new List<Account>
|
||||
{
|
||||
(Account) fflib_MockSObjectUtil.addFieldValue(new Account(Id = accountId), Account.CreatedDate, todayDatetime),
|
||||
new Account(Name = accountName + ' 2') // This one should be ignored
|
||||
});
|
||||
|
||||
System.Test.startTest();
|
||||
Map<Id, Date> result = domain.getDateFieldByIdField(Account.CreatedDate, Account.Id);
|
||||
System.Test.stopTest();
|
||||
|
||||
System.assertEquals(1, result.size(), 'Incorrect returned amount of results');
|
||||
System.assert(result.keySet().contains(accountId), 'The accountId is missing from the results');
|
||||
System.assertEquals(today, result.get(accountId), 'Incorrect returned date');
|
||||
}
|
||||
|
||||
@IsTest
|
||||
static void itShouldReturnIntegerFieldValues()
|
||||
{
|
||||
@@ -481,61 +505,6 @@ private class fflib_SObjects2Test
|
||||
System.assert(domain.getFirstSObject().get('Rating') == 'Warm');
|
||||
}
|
||||
|
||||
@IsTest
|
||||
static void itShouldGetRecordByIdFieldValues()
|
||||
{
|
||||
final Id accountId = fflib_IDGenerator.generate(Schema.Account.SObjectType);
|
||||
List<Account> records = new List<Account> {
|
||||
new Account(Id = accountId),
|
||||
new Account(Id = fflib_IDGenerator.generate(Schema.Account.SObjectType)),
|
||||
new Account(Id = fflib_IDGenerator.generate(Schema.Account.SObjectType))
|
||||
};
|
||||
fflib_SObjects2 domain = new fflib_SObjects2(records);
|
||||
|
||||
List<SObject> results = domain.getRecords(Schema.Account.Id, new Set<Id> {accountId});
|
||||
|
||||
System.assertEquals(1, results.size());
|
||||
System.assertEquals(accountId, results.get(0).Id);
|
||||
}
|
||||
|
||||
@IsTest
|
||||
static void itShouldGetRecordByStringFieldValues()
|
||||
{
|
||||
final String accountName = 'Test';
|
||||
List<Account> records = new List<Account> {
|
||||
new Account(Name = accountName),
|
||||
new Account(Name = accountName + 'A'),
|
||||
new Account(Name = 'Demo')
|
||||
};
|
||||
fflib_SObjects2 domain = new fflib_SObjects2(records);
|
||||
|
||||
List<SObject> results = domain.getRecords(Schema.Account.Name, new Set<String> {accountName});
|
||||
|
||||
System.assertEquals(1, results.size());
|
||||
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()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user