Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix for Issue 221 #449

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,7 @@ public virtual class fflib_SObjectUnitOfWork
{
for (Schema.SObjectType sObjectType : m_sObjectTypes)
{
m_relationships.get(sObjectType.getDescribe().getName()).resolve();
m_dml.dmlUpdate(m_dirtyMapByType.get(sObjectType.getDescribe().getName()).values());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,63 @@ private with sharing class fflib_SObjectUnitOfWorkTest
);
}

@IsTest
private static void testRegisterDirtyWithRelationship() {
// GIVEN an existing opportunity
Opportunity existingOpp = new Opportunity(
Id = fflib_IDGenerator.generate(Schema.Opportunity.SObjectType),
Name = 'Existing Opportunity',
StageName = 'Closed',
CloseDate = System.today()
);
// AND an existing Account to which the existing opportunity will be related to
Account newAccount = new Account(
Name = 'New Account'
);

// WHEN
Test.startTest();
MockDMLWithInsertIds mockDML = new MockDMLWithInsertIds();
List<Schema.SObjectType> mySobjects = new List<Schema.SObjectType>{
Opportunity.SObjectType,
Account.SObjectType
};
fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(mySobjects, mockDML);
uow.registerNew(newAccount);
uow.registerDirty(existingOpp, Opportunity.AccountId, newAccount);
uow.commitWork();
Test.stopTest();

// THEN
System.Assert.isTrue(
new fflib_MatcherDefinitions.SObjectsWith(
new List<Map<SObjectField, Object>>{
new Map<SObjectField, Object>
{
Account.Id => newAccount.Id,
Account.Name => 'New Account'
}
}
).matches(mockDML.recordsForInsert),
'The new account record does not match'
);

// AND
System.Assert.isTrue(
new fflib_MatcherDefinitions.SObjectsWith(
new List<Map<SObjectField, Object>>{
new Map<SObjectField, Object>
{
Opportunity.Id => existingOpp.Id,
Opportunity.Name => 'Existing Opportunity',
Opportunity.StageName => 'Closed',
Opportunity.AccountId => newAccount.Id
}
}
).matches(mockDML.recordsForUpdate),
'The opportunity record should be related to the new Account'
);
}
@IsTest
private static void testRegisterUpsert() {
Opportunity existingOpp = new Opportunity(
Expand Down Expand Up @@ -848,6 +905,43 @@ private with sharing class fflib_SObjectUnitOfWorkTest
}
}

private class MockDMLWithInsertIds implements fflib_SObjectUnitOfWork.IDML
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ClayChipps and @jprichter -- Any reason why you did not just simply modify the existing MockDML??

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to use the existing MockDML.

{
public List<SObject> recordsForInsert = new List<SObject>();
public List<SObject> recordsForUpdate = new List<SObject>();
public List<SObject> recordsForDelete = new List<SObject>();
public List<SObject> recordsForRecycleBin = new List<SObject>();
public List<SObject> recordsForEventPublish = new List<SObject>();

public void dmlInsert(List<SObject> objList)
{
for (SObject obj : objList) {
obj.Id = fflib_IDGenerator.generate(obj.getSObjectType());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ClayChipps and @jprichter -- Please add a null check on obj before trying to generate the mock Id.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}
this.recordsForInsert.addAll(objList);
}

public void dmlUpdate(List<SObject> objList)
{
this.recordsForUpdate.addAll(objList);
}

public void dmlDelete(List<SObject> objList)
{
this.recordsForDelete.addAll(objList);
}

public void eventPublish(List<SObject> objList)
{
this.recordsForEventPublish.addAll(objList);
}

public void emptyRecycleBin(List<SObject> objList)
{
this.recordsForRecycleBin.addAll(objList);
}
}

public class DerivedUnitOfWorkException extends Exception {}
public class FailDoingWorkException extends Exception {}
}
Loading