Increase code coverage

Still in need of some work
This commit is contained in:
Wim Velzeboer
2021-07-28 12:32:58 +01:00
parent 92988355c0
commit 0277af7df2
7 changed files with 210 additions and 13 deletions
@@ -71,7 +71,7 @@ public virtual class fflib_ClassicDomainFactory
public fflib_ClassicDomainFactory(fflib_ISelectorFactory selectorFactory, public fflib_ClassicDomainFactory(fflib_ISelectorFactory selectorFactory,
Map<SObjectType, Type> sObjectByDomainConstructorType) Map<SObjectType, Type> sObjectByDomainConstructorType)
{ {
selectorFactory = selectorFactory; this.selectorFactory = selectorFactory;
this.constructorTypeByObject = getConstructorTypeByObject(sObjectByDomainConstructorType); this.constructorTypeByObject = getConstructorTypeByObject(sObjectByDomainConstructorType);
this.mockDomainByObject = new Map<Object, fflib_IDomain>(); this.mockDomainByObject = new Map<Object, fflib_IDomain>();
} }
@@ -104,7 +104,7 @@ public virtual class fflib_ClassicDomainFactory
**/ **/
public virtual fflib_IDomain newInstance(Set<Id> recordIds, Schema.SObjectType sObjectType) public virtual fflib_IDomain newInstance(Set<Id> recordIds, Schema.SObjectType sObjectType)
{ {
return newInstance(selectorFactory.selectById(recordIds, sObjectType)); return newInstance(selectorFactory.selectById(recordIds, sObjectType), sObjectType);
} }
/** /**
@@ -214,7 +214,7 @@ public virtual class SObjects
@TestVisible @TestVisible
protected virtual Dates getDateFieldValues(Schema.SObjectField sObjectField, fflib_Criteria criteria) protected virtual Dates getDateFieldValues(Schema.SObjectField sObjectField, fflib_Criteria criteria)
{ {
return new SObjects(getRecords(criteria)) return new SObjects(getRecords(criteria), getSObjectType())
.getDateFieldValues(sObjectField); .getDateFieldValues(sObjectField);
} }
@@ -245,7 +245,7 @@ public virtual class SObjects
@TestVisible @TestVisible
protected virtual DateTimes getDateTimeFieldValues(Schema.SObjectField sObjectField, fflib_Criteria criteria) protected virtual DateTimes getDateTimeFieldValues(Schema.SObjectField sObjectField, fflib_Criteria criteria)
{ {
return new SObjects(getRecords(criteria)) return new SObjects(getRecords(criteria), getSObjectType())
.getDateTimeFieldValues(sObjectField); .getDateTimeFieldValues(sObjectField);
} }
@@ -276,7 +276,7 @@ public virtual class SObjects
@TestVisible @TestVisible
protected virtual Decimals getDecimalFieldValues(Schema.SObjectField sObjectField, fflib_Criteria criteria) protected virtual Decimals getDecimalFieldValues(Schema.SObjectField sObjectField, fflib_Criteria criteria)
{ {
return new SObjects(getRecords(criteria)) return new SObjects(getRecords(criteria), getSObjectType())
.getDecimalFieldValues(sObjectField); .getDecimalFieldValues(sObjectField);
} }
@@ -307,7 +307,7 @@ public virtual class SObjects
@TestVisible @TestVisible
protected virtual Doubles getDoubleFieldValues(Schema.SObjectField sObjectField, fflib_Criteria criteria) protected virtual Doubles getDoubleFieldValues(Schema.SObjectField sObjectField, fflib_Criteria criteria)
{ {
return new SObjects(getRecords(criteria)) return new SObjects(getRecords(criteria), getSObjectType())
.getDoubleFieldValues(sObjectField); .getDoubleFieldValues(sObjectField);
} }
@@ -324,6 +324,7 @@ public virtual class SObjects
* Accounts accounts = Accounts.newInstance(records); * Accounts accounts = Accounts.newInstance(records);
* Map<Object, Object> accountNameById = accounts.getFieldByField(Account.Name, Account.Id); * Map<Object, Object> accountNameById = accounts.getFieldByField(Account.Name, Account.Id);
*/ */
@TestVisible
protected virtual Map<Object, Object> getFieldByField(Schema.SObjectField valueField, Schema.SObjectField keyField) protected virtual Map<Object, Object> getFieldByField(Schema.SObjectField valueField, Schema.SObjectField keyField)
{ {
Map<Object, Object> result = new Map<Object, Object>(); Map<Object, Object> result = new Map<Object, Object>();
@@ -352,6 +353,7 @@ public virtual class SObjects
* Contacts contacts = Contacts.newInstance(records); * Contacts contacts = Contacts.newInstance(records);
* Map<Object, Set<Object>> contactIdByAccountId = contacts.getFieldsByField(Contact.Id, Contact.AccountId); * Map<Object, Set<Object>> contactIdByAccountId = contacts.getFieldsByField(Contact.Id, Contact.AccountId);
*/ */
@TestVisible
protected virtual Map<Object, Set<Object>> getFieldsByField(Schema.SObjectField valueField, Schema.SObjectField keyField) protected virtual Map<Object, Set<Object>> getFieldsByField(Schema.SObjectField valueField, Schema.SObjectField keyField)
{ {
Map<Object, Set<Object>> result = new Map<Object, Set<Object>>(); Map<Object, Set<Object>> result = new Map<Object, Set<Object>>();
@@ -382,6 +384,7 @@ public virtual class SObjects
* Contacts contacts = Contacts.newInstance(records); * Contacts contacts = Contacts.newInstance(records);
* Map<Id, Id> accountIdByContactId = contacts.getIdFieldByIdField(Contact.AccountId, Contact.Id); * Map<Id, Id> accountIdByContactId = contacts.getIdFieldByIdField(Contact.AccountId, Contact.Id);
*/ */
@TestVisible
protected virtual Map<Id, Id> getIdFieldByIdField(Schema.SObjectField valueField, Schema.SObjectField keyField) protected virtual Map<Id, Id> getIdFieldByIdField(Schema.SObjectField valueField, Schema.SObjectField keyField)
{ {
Map<Id, Id> result = new Map<Id, Id>(); Map<Id, Id> result = new Map<Id, Id>();
@@ -410,6 +413,7 @@ public virtual class SObjects
* Contacts contacts = Contacts.newInstance(records); * Contacts contacts = Contacts.newInstance(records);
* Map<Id, Set<Id>> contactIdByAccountId = contacts.getIdFieldsByIdField(Contact.Id, Contact.AccountId); * Map<Id, Set<Id>> contactIdByAccountId = contacts.getIdFieldsByIdField(Contact.Id, Contact.AccountId);
*/ */
@TestVisible
protected virtual Map<Id, Set<Id>> getIdFieldsByIdField(Schema.SObjectField valueField, Schema.SObjectField keyField) protected virtual Map<Id, Set<Id>> getIdFieldsByIdField(Schema.SObjectField valueField, Schema.SObjectField keyField)
{ {
Map<Id, Set<Id>> result = new Map<Id, Set<Id>>(); Map<Id, Set<Id>> result = new Map<Id, Set<Id>>();
@@ -459,7 +463,7 @@ public virtual class SObjects
@TestVisible @TestVisible
protected virtual Ids getIdFieldValues(Schema.SObjectField sObjectField, fflib_Criteria criteria) protected virtual Ids getIdFieldValues(Schema.SObjectField sObjectField, fflib_Criteria criteria)
{ {
return new SObjects(getRecords(criteria)) return new SObjects(getRecords(criteria), getSObjectType())
.getIdFieldValues(sObjectField); .getIdFieldValues(sObjectField);
} }
@@ -491,7 +495,7 @@ public virtual class SObjects
@TestVisible @TestVisible
protected virtual Integers getIntegerFieldValues(Schema.SObjectField sObjectField, fflib_Criteria criteria) protected virtual Integers getIntegerFieldValues(Schema.SObjectField sObjectField, fflib_Criteria criteria)
{ {
return new SObjects(getRecords(criteria)) return new SObjects(getRecords(criteria), getSObjectType())
.getIntegerFieldValues(sObjectField); .getIntegerFieldValues(sObjectField);
} }
@@ -522,7 +526,7 @@ public virtual class SObjects
@TestVisible @TestVisible
protected virtual Longs getLongFieldValues(Schema.SObjectField sObjectField, fflib_Criteria criteria) protected virtual Longs getLongFieldValues(Schema.SObjectField sObjectField, fflib_Criteria criteria)
{ {
return new SObjects(getRecords(criteria)) return new SObjects(getRecords(criteria), getSObjectType())
.getLongFieldValues(sObjectField); .getLongFieldValues(sObjectField);
} }
@@ -538,6 +542,7 @@ public virtual class SObjects
* Account account = Account.newInstance(records); * Account account = Account.newInstance(records);
* Map<Id, SObject> accountById = account.getSObjectByIdField(Account.Id); * Map<Id, SObject> accountById = account.getSObjectByIdField(Account.Id);
*/ */
@TestVisible
protected virtual Map<Id, SObject> getSObjectByIdField(Schema.SObjectField sObjectField) protected virtual Map<Id, SObject> getSObjectByIdField(Schema.SObjectField sObjectField)
{ {
Map<Id, SObject> result = new Map<Id, SObject>(); Map<Id, SObject> result = new Map<Id, SObject>();
@@ -562,13 +567,14 @@ public virtual class SObjects
* Contacts contacts = Contacts.newInstance(records); * Contacts contacts = Contacts.newInstance(records);
* Map<Id, List<SObject>> contactsByAccountId = contacts.getSObjectsByIdField(Contact.AccountId); * Map<Id, List<SObject>> contactsByAccountId = contacts.getSObjectsByIdField(Contact.AccountId);
*/ */
@TestVisible
protected virtual Map<Id, List<SObject>> getSObjectsByIdField(Schema.SObjectField sObjectField) protected virtual Map<Id, List<SObject>> getSObjectsByIdField(Schema.SObjectField sObjectField)
{ {
Map<Id, List<SObject>> result = new Map<Id, List<SObject>>(); Map<Id, List<SObject>> result = new Map<Id, List<SObject>>();
for (SObject record : getRecords()) for (SObject record : getRecords())
{ {
Id fieldId = (Id) record.get(sObjectField); Id fieldId = (Id) record.get(sObjectField);
if (record.get(fieldId) == null) continue; if (fieldId == null) continue;
if (!result.containsKey(fieldId)) if (!result.containsKey(fieldId))
{ {
@@ -591,6 +597,7 @@ public virtual class SObjects
* Account account = Account.newInstance(records); * Account account = Account.newInstance(records);
* Map<String, SObject> accountByNumber = account.getSObjectByStringField(Account.AccountNumber); * Map<String, SObject> accountByNumber = account.getSObjectByStringField(Account.AccountNumber);
*/ */
@TestVisible
protected virtual Map<String, SObject> getSObjectByStringField(Schema.SObjectField sObjectField) protected virtual Map<String, SObject> getSObjectByStringField(Schema.SObjectField sObjectField)
{ {
Map<String, SObject> result = new Map<String, SObject>(); Map<String, SObject> result = new Map<String, SObject>();
@@ -616,6 +623,7 @@ public virtual class SObjects
* Account account = Account.newInstance(records); * Account account = Account.newInstance(records);
* Map<String, SObject> accountByName = account.getSObjectsByStringField(Account.AccountName); * Map<String, SObject> accountByName = account.getSObjectsByStringField(Account.AccountName);
*/ */
@TestVisible
protected virtual Map<String, List<SObject>> getSObjectsByStringField(Schema.SObjectField sObjectField) protected virtual Map<String, List<SObject>> getSObjectsByStringField(Schema.SObjectField sObjectField)
{ {
Map<String, List<SObject>> result = new Map<String, List<SObject>>(); Map<String, List<SObject>> result = new Map<String, List<SObject>>();
@@ -646,6 +654,7 @@ public virtual class SObjects
* Account account = Account.newInstance(records); * Account account = Account.newInstance(records);
* Map<Id, String> accountNameById = account.getStringFieldByIdField(Account.AccountName, Account.Id); * Map<Id, String> accountNameById = account.getStringFieldByIdField(Account.AccountName, Account.Id);
*/ */
@TestVisible
protected virtual Map<Id, String> getStringFieldByIdField(Schema.SObjectField valueField, Schema.SObjectField keyField) protected virtual Map<Id, String> getStringFieldByIdField(Schema.SObjectField valueField, Schema.SObjectField keyField)
{ {
Map<Id, String> result = new Map<Id, String>(); Map<Id, String> result = new Map<Id, String>();
@@ -670,6 +679,7 @@ public virtual class SObjects
* Account account = Account.newInstance(records); * Account account = Account.newInstance(records);
* Map<Id, String> accountNameById = account.getStringFieldByStringField(Account.AccountName, Account.Id); * Map<Id, String> accountNameById = account.getStringFieldByStringField(Account.AccountName, Account.Id);
*/ */
@TestVisible
protected virtual Map<String, String> getStringFieldByStringField(Schema.SObjectField valueField, Schema.SObjectField keyField) protected virtual Map<String, String> getStringFieldByStringField(Schema.SObjectField valueField, Schema.SObjectField keyField)
{ {
Map<String, String> result = new Map<String, String>(); Map<String, String> result = new Map<String, String>();
@@ -708,7 +718,7 @@ public virtual class SObjects
@TestVisible @TestVisible
protected virtual Strings getStringFieldValues(Schema.SObjectField sObjectField, fflib_Criteria criteria) protected virtual Strings getStringFieldValues(Schema.SObjectField sObjectField, fflib_Criteria criteria)
{ {
return new SObjects(getRecords(criteria)) return new SObjects(getRecords(criteria), getSObjectType())
.getStringFieldValues(sObjectField); .getStringFieldValues(sObjectField);
} }
@@ -722,7 +732,7 @@ public virtual class SObjects
@TestVisible @TestVisible
protected virtual void setFieldValue(Schema.SObjectField sObjectField, Object value, fflib_Criteria criteria) protected virtual void setFieldValue(Schema.SObjectField sObjectField, Object value, fflib_Criteria criteria)
{ {
new SObjects(getRecords(criteria)) new SObjects(getRecords(criteria), getSObjectType())
.setFieldValue(sObjectField, value); .setFieldValue(sObjectField, value);
} }
} }
@@ -0,0 +1,98 @@
/**
* File Name: fflib_ClassicDomainFactoryTest
*
* @author architect ir. Wilhelmus G.J. Velzeboer
*
* Copyright (c), W.G.J. Velzeboer,
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above author notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of the author nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
@IsTest
private class fflib_ClassicDomainFactoryTest
{
@IsTest
static void testBehavior()
{
// todo - fix assertions
fflib_ClassicSelectorFactory selectorFactory =
new fflib_ClassicSelectorFactory(
new Map<SObjectType, Type>
{
Account.SObjectType => MySelector.class
});
fflib_ClassicDomainFactory domainFactory = new fflib_ClassicDomainFactory(
selectorFactory,
new Map<SObjectType, Type>
{
Account.SObjectType => MyDomainConstructor.class
});
List<Account> records = new List<Account>();
domainFactory.newInstance(records);
domainFactory.newInstance(
new Set<Id>{fflib_IDGenerator.generate(Account.SObjectType)},
Account.SObjectType);
domainFactory.newInstance(new Set<Id>{fflib_IDGenerator.generate(Account.SObjectType)});
domainFactory.replaceWith(Account.SObjectType, MyDomain.class);
domainFactory.setMock(Account.SObjectType, new MyDomain(records));
domainFactory.setMock(new MyDomain(records));
}
public class MySelector extends fflib_SObjectSelector
{
public List<Schema.SObjectField> getSObjectFieldList()
{
return new List<Schema.SObjectField>
{
Account.Id
};
}
public override List<SObject> selectSObjectsById(Set<Id> idSet)
{
return new List<Account>{ new Account(Id = new List<Id>(idSet).get(0))};
}
public SObjectType getSObjectType()
{
return Account.SObjectType;
}
}
public class MyDomain extends SObjects
{
public MyDomain(List<Account> records)
{
super(records, Account.SObjectType);
}
}
public class MyDomainConstructor implements fflib_IDomainConstructor
{
public fflib_IDomain construct(List<Object> objects)
{
return new fflib_SObjects((List<SObject>) objects);
}
}
}
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>51.0</apiVersion>
<status>Active</status>
</ApexClass>
@@ -68,7 +68,7 @@ private class SObjectsTest
} }
@IsTest @IsTest
static void itShouldReturnTheDomainsType() static void itShouldReturnTheCreatedData()
{ {
System.assertEquals( System.assertEquals(
(Object) Schema.Account.SObjectType, (Object) Schema.Account.SObjectType,
@@ -84,6 +84,36 @@ private class SObjectsTest
); );
} }
@IsTest
static void itShouldReturnTheDomainsType()
{
fflib_Criteria criteria = new fflib_Criteria().equalTo(Account.Name, 'A');
DomainAccounts domain = generateDomain();
domain.getDateTimeFieldValues(Account.LastActivityDate);
domain.getDateTimeFieldValues(Account.CreatedDate);
domain.getFieldByField(Account.Name, Account.AccountNumber);
domain.getFieldsByField(Account.Id, Account.AccountNumber);
domain.getFieldByField(Account.Id, Account.Id);
domain.getIdFieldByIdField(Account.Id, Account.Id);
domain.getIdFieldsByIdField(Account.Id, Account.Id);
domain.getIdFieldValues(Account.Id);
domain.getIdFieldValues(Account.Id, criteria);
domain.getIntegerFieldValues(Account.NumberOfEmployees);
domain.getIntegerFieldValues(Account.NumberOfEmployees, criteria);
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.getStringFieldByIdField(Account.Id, Account.Name);
domain.getStringFieldByStringField(Account.Name, Account.AccountNumber);
domain.getStringFieldValues(Account.Name, criteria);
// todo - add assertions
}
@IsTest @IsTest
static void itShouldReturnRecordByIds() static void itShouldReturnRecordByIds()
{ {
@@ -0,0 +1,49 @@
/**
* File Name: sflib_sflib_OperatorUtilsTest
*
* @author architect ir. Wilhelmus G.J. Velzeboer
*
* Copyright (c), W.G.J. Velzeboer,
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above author notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of the author nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
@IsTest
private class fflib_OperatorUtilsTest
{
@IsTest
static void testBehavior()
{
System.assertEquals('=', fflib_OperatorUtils.operatorToString(fflib_OperatorUtils.EQUALS));
System.assertEquals('!=', fflib_OperatorUtils.operatorToString(fflib_OperatorUtils.NOT_EQUALS));
System.assertEquals('<', fflib_OperatorUtils.operatorToString(fflib_OperatorUtils.LESS_THAN));
System.assertEquals('<=', fflib_OperatorUtils.operatorToString(fflib_OperatorUtils.LESS_THAN_OR_EQUAL_TO));
System.assertEquals('>', fflib_OperatorUtils.operatorToString(fflib_OperatorUtils.GREATER_THAN));
System.assertEquals('>=', fflib_OperatorUtils.operatorToString(fflib_OperatorUtils.GREATER_THAN_OR_EQUAL_TO));
System.assertEquals('like', fflib_OperatorUtils.operatorToString(fflib_OperatorUtils.LIKEx));
System.assertEquals('IN', fflib_OperatorUtils.operatorToString(fflib_OperatorUtils.INx));
System.assertEquals('NOT IN', fflib_OperatorUtils.operatorToString(fflib_OperatorUtils.NOT_IN));
System.assertEquals('INCLUDES', fflib_OperatorUtils.operatorToString(fflib_OperatorUtils.INCLUDES));
System.assertEquals('EXCLUDES', fflib_OperatorUtils.operatorToString(fflib_OperatorUtils.EXCLUDES));
System.assertEquals(null, fflib_OperatorUtils.operatorToString(null));
}
}
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>51.0</apiVersion>
<status>Active</status>
</ApexClass>