Increase unit-test coverage to 85%

This commit is contained in:
Wim Velzeboer
2021-06-25 11:30:27 +01:00
parent a5482f0fb4
commit fee08c99b9
8 changed files with 360 additions and 37 deletions
@@ -150,24 +150,24 @@ public with sharing class fflib_Comparator
return false;
else if (object2 == null)
return false;
else if (object1 instanceof set<Date> && object2 instanceof Date)
return ((set<Date>) object1).contains((date) object2);
else if (object1 instanceof set<Datetime> && object2 instanceof Datetime)
return ((set<Datetime>) object1).contains((Datetime) object2);
else if (object1 instanceof set<Double> && object2 instanceof Double)
return ((set<Double>) object1).contains((Double) object2);
else if (object1 instanceof set<Id> && object2 instanceof Id)
return ((set<Id>) object1).contains((Id) object2);
else if (object1 instanceof set<Integer> && object2 instanceof Integer)
return ((set<Integer>) object1).contains((Integer) object2);
else if (object1 instanceof set<Long> && object2 instanceof Long)
return ((set<Long>) object1).contains((Long) object2);
else if (object1 instanceof set<Object>)
return ((set<Object>) object1).contains((Object) object2);
else if (object1 instanceof set<Time> && object2 instanceof Time)
return ((set<Time>) object1).contains((Time) object2);
else if (object1 instanceof set<String> && object2 instanceof String)
return ((set<String>) object1).contains((String) object2);
else if (object1 instanceof Set<Date> && object2 instanceof Date)
return ((Set<Date>) object1).contains((Date) object2);
else if (object1 instanceof Set<Datetime> && object2 instanceof Datetime)
return ((Set<Datetime>) object1).contains((Datetime) object2);
else if (object1 instanceof Set<Double> && object2 instanceof Double)
return ((Set<Double>) object1).contains((Double) object2);
else if (object1 instanceof Set<Id> && object2 instanceof Id)
return ((Set<Id>) object1).contains((Id) object2);
else if (object1 instanceof Set<Integer> && object2 instanceof Integer)
return ((Set<Integer>) object1).contains((Integer) object2);
else if (object1 instanceof Set<Long> && object2 instanceof Long)
return ((Set<Long>) object1).contains((Long) object2);
else if (object1 instanceof Set<Object>)
return ((Set<Object>) object1).contains((Object) object2);
else if (object1 instanceof Set<Time> && object2 instanceof Time)
return ((Set<Time>) object1).contains((Time) object2);
else if (object1 instanceof Set<String> && object2 instanceof String)
return ((Set<String>) object1).contains((String) object2);
else
throw new IllegalArgumentException(
'Both arguments must be type Date, Datetime, Decimal, Double, ID, Integer, Long, Object, Time, Object or String');
@@ -320,14 +320,14 @@ public virtual with sharing class fflib_Criteria
*/
public fflib_Criteria inSet(Schema.SObjectField field, Set<Object> values)
{
return inSet(field, new Objects(values));
return inSet(field, new fflib_Objects(new List<Object>(values)));
}
// Method overrides
public fflib_Criteria inSet(Schema.SObjectField field, Set<Date> values)
{
return inSet(field, new Dates(values));
}
public fflib_Criteria inSet(Schema.SObjectField field, Set<DateTime> values)
public fflib_Criteria inSet(Schema.SObjectField field, Set<Datetime> values)
{
return inSet(field, new DateTimes(values));
}
@@ -370,7 +370,7 @@ public virtual with sharing class fflib_Criteria
* Evaluates:
* Account.Type IN ('Customer','Competitor','Partner')
*/
public fflib_Criteria inSet(Schema.SObjectField field, Objects values)
public fflib_Criteria inSet(Schema.SObjectField field, fflib_Objects values)
{
evaluators.add(new FieldSetEvaluator(field, fflib_Operator.INx, values));
return this;
@@ -395,7 +395,7 @@ public virtual with sharing class fflib_Criteria
{
return notInSet(field, new Dates(values));
}
public fflib_Criteria notInSet(Schema.SObjectField field, Set<DateTime> values)
public fflib_Criteria notInSet(Schema.SObjectField field, Set<Datetime> values)
{
return notInSet(field, new DateTimes(values));
}
@@ -425,7 +425,7 @@ public virtual with sharing class fflib_Criteria
}
public fflib_Criteria notInSet(Schema.SObjectField field, Set<Object> values)
{
return notInSet(field, new Objects(values));
return notInSet(field, new fflib_Objects(new List<Object>(values)));
}
/**
@@ -443,7 +443,7 @@ public virtual with sharing class fflib_Criteria
* Evaluates:
* Account.Type NOT IN ('Customer','Competitor','Partner')
*/
public fflib_Criteria notInSet(Schema.SObjectField field, Objects values)
public fflib_Criteria notInSet(Schema.SObjectField field, fflib_Objects values)
{
evaluators.add(new FieldSetEvaluator(field, fflib_Operator.NOT_IN, values));
return this;
@@ -552,7 +552,7 @@ public virtual with sharing class fflib_Criteria
*
* @return Returns the given values converted to literal string separated by a comma
*/
private static String toLiteral(final Objects values)
private static String toLiteral(final fflib_Objects values)
{
if (values.isEmpty()) return '';
@@ -574,10 +574,10 @@ public virtual with sharing class fflib_Criteria
private class FieldSetEvaluator implements Evaluator
{
private Schema.SObjectField sObjectField;
private Objects values;
private fflib_Objects values;
private fflib_Operator operator;
public FieldSetEvaluator(Schema.SObjectField sObjectField, fflib_Operator operator, Objects values)
public FieldSetEvaluator(Schema.SObjectField sObjectField, fflib_Operator operator, fflib_Objects values)
{
this.sObjectField = sObjectField;
this.values = values;
@@ -46,7 +46,7 @@ public virtual with sharing class Ids
super(elements);
}
public Ids(Objects objects)
public Ids(fflib_Objects objects)
{
super(getIdList(objects));
}
@@ -61,7 +61,7 @@ public virtual with sharing class Ids
return (List<Id>) getObjects();
}
private static List<Id> getIdList(Objects objects)
private static List<Id> getIdList(fflib_Objects objects)
{
List<Id> result = new List<Id>();
for (Object obj : objects.getObjects())
@@ -67,6 +67,18 @@ public virtual class SObjects
SObjectDescribe = sObjectType.getDescribe();
}
/**
* @param id Domain containing primary key (Id field) values
*
* @return Returns only the SObjects from the domain matching the given Ids.
*/
public virtual SObject getRecord(Id id)
{
List<SObject> result = getRecords(new Set<Id> {id});
return (result.size() == 0) ? null : result.get(0);
}
/**
* @return Returns the contents of the Domain by their primary Key ('Id' field)
*/
@@ -83,7 +95,6 @@ public virtual class SObjects
public virtual List<SObject> getRecords(Ids ids)
{
return getRecords(ids.getIds());
}
/**
@@ -28,7 +28,7 @@
* 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
@IsTest(IsParallel=true)
private with sharing class fflib_ComparatorTest
{
@@ -0,0 +1,184 @@
/**
* File Name: DatesTest
* Description: Unit test class for Primitive domains
*
* @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(IsParallel=true)
private class PrimitiveDomainsTest
{
@IsTest
static void testDates()
{
final Set<Date> dateSet = new Set<Date>
{
Date.newInstance(2020, 02, 02),
Date.newInstance(2021, 02, 01),
Date.newInstance(2022, 02, 02)
};
final List<Date> dateList = new List<Date>
{
Date.newInstance(2020, 02, 02),
Date.newInstance(2021, 02, 01),
Date.newInstance(2022, 02, 02)
};
Dates datesA = new Dates(dateSet);
Dates DatesB = new Dates(dateList);
Dates DatesC = new Dates((List<Object>) dateList);
System.assert(datesA.getDateSet().containsAll(DatesB.getDates()));
System.assert(datesA.getDateSet().containsAll(DatesC.getDates()));
}
@IsTest
static void testDateTimes()
{
final Set<Datetime> dateSet = new Set<Datetime>
{
Datetime.newInstance(2020, 02, 02, 01, 00, 00),
Datetime.newInstance(2021, 02, 01, 01, 00, 00),
Datetime.newInstance(2022, 02, 02, 01, 00, 00)
};
final List<Datetime> dateList = new List<Datetime>
{
Datetime.newInstance(2020, 02, 02, 01, 00, 00),
Datetime.newInstance(2021, 02, 01, 01, 00, 00),
Datetime.newInstance(2022, 02, 02, 01, 00, 00)
};
DateTimes datetimeA = new DateTimes(dateSet);
DateTimes datetimeB = new DateTimes(dateList);
DateTimes datetimeC = new DateTimes((List<Object>) dateList);
System.assert(datetimeA.getDateTimeSet().containsAll(datetimeB.getDateTimes()));
System.assert(datetimeA.getDateTimeSet().containsAll(datetimeC.getDateTimes()));
}
@IsTest
static void testDecimal()
{
final Set<Decimal> dataSet = new Set<Decimal> {5.5, 1.1, -1.1, -2.7};
final List<Decimal> dataList = new List<Decimal> {5.5, 1.1, -1.1, -2.7};
Decimals domainA = new Decimals(dataSet);
Decimals domainB = new Decimals(dataList);
Decimals domainC = new Decimals((List<Object>) dataList);
System.assert(domainA.getDecimalSet().containsAll(domainB.getDecimals()));
System.assert(domainA.getDecimalSet().containsAll(domainC.getDecimals()));
}
@IsTest
static void testDoubles()
{
final Set<Double> dataSet = new Set<Double> {1261992, 3.14159265};
final List<Double> dataList = new List<Double> {1261992, 3.14159265};
Doubles domainA = new Doubles(dataSet);
Doubles domainB = new Doubles(dataList);
Doubles domainC = new Doubles((List<Object>) dataList);
System.assert(domainA.getDoubleSet().containsAll(domainB.getDoubles()));
System.assert(domainA.getDoubleSet().containsAll(domainC.getDoubles()));
}
@IsTest
static void testIds()
{
final Set<Id> dataSet = new Set<Id>
{
fflib_IDGenerator.generate(Schema.Account.SObjectType),
fflib_IDGenerator.generate(Schema.Account.SObjectType),
fflib_IDGenerator.generate(Schema.Account.SObjectType)
};
Ids domainA = new Ids(dataSet);
Ids domainB = new Ids(new List<Id>(dataSet));
Ids domainC = new Ids((List<Object>) new List<Id>(dataSet));
System.assert(domainA.getIds().containsAll(domainB.getIdList()));
System.assert(domainA.getIds().containsAll(domainC.getIdList()));
}
@IsTest
static void testIds_Objects()
{
final fflib_Objects data = new fflib_Objects(new List<Id>(
new Set<Id>
{
fflib_IDGenerator.generate(Schema.Account.SObjectType),
fflib_IDGenerator.generate(Schema.Account.SObjectType),
fflib_IDGenerator.generate(Schema.Account.SObjectType)
}));
Ids domainA = new Ids(data);
System.assert(domainA.getIds().containsAll((List<Id>) data.getObjects()));
}
@IsTest
static void testIntegers()
{
final Set<Integer> dataSet = new Set<Integer> {1261, 31415};
Integers domainA = new Integers(dataSet);
Integers domainB = new Integers(new List<Integer>(dataSet));
Integers domainC = new Integers((List<Object>) new List<Integer>(dataSet));
System.assert(domainA.getIntegerSet().containsAll(domainB.getIntegers()));
System.assert(domainA.getIntegerSet().containsAll(domainC.getIntegers()));
}
@IsTest
static void testLongs()
{
final Set<Long> dataSet = new Set<Long> {1261, 31415};
Longs domainA = new Longs(dataSet);
Longs domainB = new Longs(new List<Long>(dataSet));
Longs domainC = new Longs((List<Object>) new List<Long>(dataSet));
System.assert(domainA.getLongSet().containsAll(domainB.getLongs()));
System.assert(domainA.getLongSet().containsAll(domainC.getLongs()));
}
@IsTest
static void testStrings()
{
final Set<String> dataSet = new Set<String> {'Hello', 'World'};
Strings domainA = new Strings(dataSet);
Strings domainB = new Strings(new List<String>(dataSet));
Strings domainC = new Strings((List<Object>) new List<String>(dataSet));
System.assert(domainA.getStringSet().containsAll(domainB.getStrings()));
System.assert(domainA.getStringSet().containsAll(domainC.getStrings()));
}
}
@@ -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>
@@ -28,9 +28,33 @@
* 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
@IsTest(IsParallel=true)
private class SObjectsTest
{
@IsTest
static void itShouldAddErrorMessage()
{
SObjectType accountSObjectType = Schema.Account.SObjectType;
final Id idA = fflib_IDGenerator.generate(accountSObjectType);
final Id idB = fflib_IDGenerator.generate(accountSObjectType);
DomainAccounts domain = new DomainAccounts(
new List<SObject>
{
new Account(Id = idA),
new Account(Id = idB)
}
);
domain.addError('Oops');
domain.addError(Schema.Account.Name,'Oops');
System.assertEquals(
4,
fflib_SObjects.Errors.getAll().size()
);
}
@IsTest
static void itShouldClearTheField()
{
@@ -43,7 +67,7 @@ private class SObjectsTest
System.assert(domain.selectByShippingCountry('Holland').isEmpty(), 'Field should have been nullified');
}
@IsTest
@IsTest
static void itShouldReturnTheDomainsType()
{
System.assertEquals(
@@ -60,6 +84,31 @@ private class SObjectsTest
);
}
@IsTest
static void itShouldReturnRecordByIds()
{
SObjectType accountSObjectType = Schema.Account.SObjectType;
final Id idA = fflib_IDGenerator.generate(accountSObjectType);
final Id idB = fflib_IDGenerator.generate(accountSObjectType);
final Id idC = fflib_IDGenerator.generate(accountSObjectType);
DomainAccounts domain = new DomainAccounts(
new List<SObject>
{
new Account(Id = idA),
new Account(Id = idB),
new Account(Id = idC)
}
);
Set<Id> recordIds = new Set<Id> {idA, idB, idC};
System.assert(
domain.getSObjectById().keySet().containsAll(recordIds),
'The domain should return all the record Ids'
);
}
@IsTest
static void itShouldReturnRecordsIds()
{
@@ -84,6 +133,79 @@ private class SObjectsTest
);
}
@IsTest
static void itShouldReturnRecords()
{
DomainAccounts domain = generateDomain();
System.assert(domain.getRecords().size() == 6);
}
@IsTest
static void itShouldReturnRecordByCriteria()
{
final Id idA = fflib_IDGenerator.generate(Schema.Account.SObjectType);
DomainAccounts domain = new DomainAccounts(
new List<Account>
{
new Account(Name = 'A', Id = idA),
new Account(Name = 'B', Id = fflib_IDGenerator.generate(Schema.Account.SObjectType))
});
System.assertEquals(
'A',
domain.getRecords(
new fflib_Criteria()
.equalTo(Schema.Account.Id, idA)
)
.get(0)
.get('Name')
);
}
@IsTest
static void itShouldReturnRecordById()
{
final Id idA = fflib_IDGenerator.generate(Schema.Account.SObjectType);
DomainAccounts domain = new DomainAccounts(
new List<Account>
{
new Account(Name = 'A', Id = idA),
new Account(Name = 'B', Id = fflib_IDGenerator.generate(Schema.Account.SObjectType))
});
System.assertEquals('A', domain.getRecord(idA).get('Name'));
}
@IsTest
static void itShouldReturnRecordsById()
{
final Id idA = fflib_IDGenerator.generate(Schema.Account.SObjectType);
final Ids ids = new Ids(new Set<Id> {idA});
DomainAccounts domain = new DomainAccounts(
new List<Account>
{
new Account(Name = 'A', Id = idA),
new Account(Name = 'B', Id = fflib_IDGenerator.generate(Schema.Account.SObjectType))
});
System.assertEquals('A', domain.getRecords(ids).get(0).get('Name'));
}
@IsTest
static void itShouldReturnRecordsByIds()
{
final Id idA = fflib_IDGenerator.generate(Schema.Account.SObjectType);
DomainAccounts domain = new DomainAccounts(
new List<Account>
{
new Account(Name = 'A', Id = idA),
new Account(Name = 'B', Id = fflib_IDGenerator.generate(Schema.Account.SObjectType))
});
System.assertEquals('A', domain.getRecords(new Set<Id> {idA}).get(0).get('Name'));
}
@IsTest
static void itShouldReturnRecordsWithFieldValues()
{
@@ -132,7 +254,8 @@ private class SObjectsTest
System.assert(
domain.getStringFieldValues(Schema.Account.ShippingCountry)
.containsAll(
new Set<Object>{
new Set<Object>
{
'USA',
'Ireland',
'UK',
@@ -157,7 +280,7 @@ private class SObjectsTest
{
DomainAccounts domain = generateDomain();
domain.setRatingByShippingCountry(
new Map<Object,Object>
new Map<Object, Object>
{
'USA' => 'Hot'
}
@@ -166,7 +289,7 @@ private class SObjectsTest
System.assert(domain.selectByRating('Hot').size() == 1);
}
private static DomainAccounts generateDomain()
private static DomainAccounts generateDomain()
{
DomainAccounts domain = new DomainAccounts(
new List<Account>
@@ -194,7 +317,7 @@ private class SObjectsTest
{
return (List<Account>) getRecordsByFieldValues(
Schema.Account.ShippingCountry,
new Set<Object>{ country }
new Set<Object> {country}
);
}