Skip to content

Commit

Permalink
Cleaning up UoW
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarParra committed Mar 24, 2022
1 parent c11b18e commit e41610d
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 661 deletions.
8 changes: 4 additions & 4 deletions example/tests/IntegrationTests.cls
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private class IntegrationTests {
private static void canGetTheChildrenCreatedThroughABuilder() {
Account anyAccount = new AccountTestDataBuilder().withChildContact('Parra').insertNew();
SObjectTestDataBuilder.commitRecords();
List<Contact> accountChildren = SObjectTestDataBuilder.getChildrenOfByType(anyAccount.Id, Contact.SObjectType);
List<Contact> accountChildren = SObjectTestDataBuilder.getChildrenOfTypeById(anyAccount.Id, Contact.SObjectType);
System.assertEquals(1, accountChildren.size(), 'Expected a child returned.');
System.assertEquals(anyAccount.Id, accountChildren[0].AccountId, 'Expected the parent correctly set.');
}
Expand All @@ -88,7 +88,7 @@ private class IntegrationTests {
System.assertNotEquals(null, grandParentAccount.Id, 'Expected the grand parent to have been inserted.');
System.assertEquals('Grandparent', grandParentAccount.Name, 'Expected the data to have been correctly set.');

Account parentAccount = (Account)SObjectTestDataBuilder.getChildrenOfByType(grandParentAccount.Id, Account.SObjectType)[0];
Account parentAccount = (Account)SObjectTestDataBuilder.getChildrenOfTypeById(grandParentAccount.Id, Account.SObjectType)[0];
System.assertNotEquals(null, parentAccount.Id, 'Expected the parent to have been created.');
System.assertEquals('Parent', parentAccount.Name, 'Expected the data to have been correctly set.');
System.assertEquals(grandParentAccount.Id, parentAccount.ParentId, 'Expected the relationship to have been correctly created.');
Expand Down Expand Up @@ -160,12 +160,12 @@ private class IntegrationTests {
System.assertNotEquals(null, grandParentAccount.Id, 'Expected the grand parent to have been inserted.');
System.assertEquals('Grandparent', grandParentAccount.Name, 'Expected the data to have been correctly set.');

Account parentAccount = (Account)SObjectTestDataBuilder.getChildrenOfByType(grandParentAccount.Id, Account.SObjectType)[0];
Account parentAccount = (Account)SObjectTestDataBuilder.getChildrenOfTypeById(grandParentAccount.Id, Account.SObjectType)[0];
System.assertNotEquals(null, parentAccount.Id, 'Expected the parent to have been created.');
System.assertEquals('Parent', parentAccount.Name, 'Expected the data to have been correctly set.');
System.assertEquals(grandParentAccount.Id, parentAccount.ParentId, 'Expected the relationship to have been correctly created.');

Account childAccount = (Account)SObjectTestDataBuilder.getChildrenOfByType(parentAccount.Id, Account.SObjectType)[0];
Account childAccount = (Account)SObjectTestDataBuilder.getChildrenOfTypeById(parentAccount.Id, Account.SObjectType)[0];
System.assertNotEquals(null, childAccount.Id, 'Expected child parent to have been created.');
System.assertEquals('Child', childAccount.Name, 'Expected the data to have been correctly set.');
System.assertEquals(parentAccount.Id, childAccount.ParentId, 'Expected the relationship to have been correctly created.');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// TODO: Refactor functionality into the fflib_UoW and get rid of this
@IsTest
public class InsertTestDataUnitOfWork {
private List<UnitOfWorkRegistration> registrations;
Expand Down
69 changes: 28 additions & 41 deletions force-app/main/default/classes/SObjectTestDataBuilder.cls
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// TODO: Now that we are using fflib, let's see what we can do about defaulting parents. For example having a contact having a defualt AccountId
// how can we do that with the least amount of DMLs possible

// TODO: Rename everything that says insert now that we are not directly inserting here
public abstract class SObjectTestDataBuilder implements ITestDataBuilder, ITestDataCallback {
private static TestDataBuilderCache builderCache = new TestDataBuilderCache();
Expand All @@ -24,11 +21,6 @@ public abstract class SObjectTestDataBuilder implements ITestDataBuilder, ITestD
registeredRelationships = new List<ChildRelationship>();
}

// TODO: Maybe we can have an "in memory" overload (using an enum) here that allows us to
// use this same API but just creating everything in memory. We can use sfab style
// of JSON manipulation to create child relationships
// sfab style will also allow us to easily populate non-writable fields like formulas
// In the overload we can even specify if we want "fake Ids" to be created for us when we are in "in memory mode"
public static ITestDataBuilder of(SObjectType objectType) {
ITestDataBuilder builder = builderCache.getFor(objectType);
if (builder == null) {
Expand All @@ -45,7 +37,7 @@ public abstract class SObjectTestDataBuilder implements ITestDataBuilder, ITestD
return of(objectType).insertNew();
}

public static List<SObject> getChildrenOfByType(Id recordId, SObjectType objectType) {
public static List<SObject> getChildrenOfTypeById(Id recordId, SObjectType objectType) {
return testDataHistory.getChildrenOfByType(recordId, objectType);
}

Expand All @@ -63,8 +55,6 @@ public abstract class SObjectTestDataBuilder implements ITestDataBuilder, ITestD
}

List<SObjectType> workTypes = getSObjectTypesToRegisterToUnitOfWorkInOrder(allLateBindingsTemp);
System.debug('Types in order: ' + workTypes);

fflib_SObjectUnitOfWork uOW = new fflib_SObjectUnitOfWork(workTypes,
fflib_SObjectUnitOfWork.UnresolvedRelationshipBehavior.AttemptResolveOutOfOrder
);
Expand Down Expand Up @@ -125,37 +115,13 @@ public abstract class SObjectTestDataBuilder implements ITestDataBuilder, ITestD
return this;
}

public virtual void beforeInsert(List<SObject> records) {}
public virtual void afterInsert(List<SObject> records) {}

protected LateBinding bindTo(ITestDataBuilder builder) {
return new LateBinding(builder);
}

public class LateBinding {
private ITestDataBuilder builder;
private SObjectField relationshipField;
private SObject childRecord;
private SObject parentRecord;

public LateBinding(ITestDataBuilder builder) {
this.builder = builder;
}

public LateBinding setRelationshipField(SObjectField field) {
this.relationshipField = field;
return this;
}

private void setChildRecord(SObject childRecord) {
this.childRecord = childRecord;
}

private void setCreatedParent(SObject parentRecord) {
this.parentRecord = parentRecord;
}
}

public virtual void beforeInsert(List<SObject> records) {}
public virtual void afterInsert(List<SObject> records) {}

protected SObjectTestDataBuilder withData(SObjectField field, Object value) {
this.customValueMap.put(field, value);
return this;
Expand Down Expand Up @@ -275,6 +241,30 @@ public abstract class SObjectTestDataBuilder implements ITestDataBuilder, ITestD
}
}

public class LateBinding {
private ITestDataBuilder builder;
private SObjectField relationshipField;
private SObject childRecord;
private SObject parentRecord;

public LateBinding(ITestDataBuilder builder) {
this.builder = builder;
}

public LateBinding setRelationshipField(SObjectField field) {
this.relationshipField = field;
return this;
}

private void setChildRecord(SObject childRecord) {
this.childRecord = childRecord;
}

private void setCreatedParent(SObject parentRecord) {
this.parentRecord = parentRecord;
}
}

private class ChildRelationship {
public ITestDataBuilder ChildBuilder { get; private set; }
public SObjectField RelationshipField { get; private set; }
Expand Down Expand Up @@ -337,9 +327,6 @@ public abstract class SObjectTestDataBuilder implements ITestDataBuilder, ITestD
return children;
}
for (HistoryItem item : itemsByType) {
// System.debug('Item: ' + item.getRecord().get(Account.Name));
// System.debug('Item Id: ' + item.getRecord().get(Account.Id));
// System.debug('Items Parent: ' + item.getParentRecord());
if (item.getParentRecord()?.Id == recordId) {
children.add(item.getRecord());
}
Expand Down
Loading

0 comments on commit e41610d

Please sign in to comment.