Skip to content

Commit dfce16d

Browse files
committed
feat: Add test to ensure non breaking behaviour when overwriting existing registeredRecord
1 parent 7db6291 commit dfce16d

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

fflib/src/classes/fflib_SObjectUnitOfWork.cls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ public virtual class fflib_SObjectUnitOfWork
259259
if(!m_dirtyMapByType.containsKey(sObjectType))
260260
throw new UnitOfWorkException(String.format('SObject type {0} is not supported by this unit of work', new String[] { sObjectType }));
261261

262-
// If record isn't registered as dirty
262+
// If record isn't registered as dirty, or no dirty fields to drive a merge
263263
if (!m_dirtyMapByType.get(sObjectType).containsKey(record.Id) || dirtyFields.isEmpty())
264264
{
265265
// Register the record as dirty

fflib/src/classes/fflib_SObjectUnitOfWorkTest.cls

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -339,27 +339,27 @@ private with sharing class fflib_SObjectUnitOfWorkTest
339339
}
340340

341341
/**
342-
* Try registering two instances of the same record as dirty.
342+
* Try registering two instances of the same record as dirty. Second register should overwrite first.
343343
*
344344
* Testing:
345345
*
346346
* - Exception is thrown stopping second registration
347347
*/
348348
@isTest
349-
private static void testRegisterDirty_DoubleException() {
350-
Opportunity opp = new Opportunity(Id = '00636000005loIj', Name = 'UpdateName');
351-
Opportunity opp2 = new Opportunity(Id = '00636000005loIj', Amount = 250);
349+
private static void testRegisterDirty_ExpectReplacement() {
350+
final Opportunity insertedOpp = new Opportunity(Name = 'Original', StageName = 'Open', CloseDate = System.today());
351+
insert insertedOpp;
352+
353+
Opportunity opp = new Opportunity(Id = insertedOpp.Id, Name = 'Never');
354+
Opportunity opp2 = new Opportunity(Id = insertedOpp.Id, Name = 'Expected');
355+
352356
fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(MY_SOBJECTS);
353357
uow.registerDirty(opp);
354-
Boolean exceptionThrown = false;
355-
try {
356-
// Second registration would undo updates of first registration
357-
uow.registerDirty(opp2);
358-
} catch (fflib_SObjectUnitOfWork.UnitOfWorkException e) {
359-
System.assert(e.getMessage().contains(opp.Id));
360-
exceptionThrown = true;
361-
}
362-
System.assert(exceptionThrown);
358+
uow.registerDirty(opp2);
359+
uow.commitWork();
360+
361+
Opportunity updated = [select Id, Name from Opportunity where Id = :insertedOpp.Id];
362+
System.assertEquals('Expected', updated.Name);
363363
}
364364

365365
/**
@@ -381,7 +381,7 @@ private with sharing class fflib_SObjectUnitOfWorkTest
381381
uow.registerDirty(amountUpdate, new List<SObjectField> { Opportunity.Amount } );
382382
uow.commitWork();
383383

384-
opp = [SELECT Name, Amount FROM Opportunity WHERE Id = :opp.Id];
384+
opp = [select Name, Amount from Opportunity where Id = :opp.Id];
385385
System.assertEquals(opp.Name, nameUpdate.Name);
386386
System.assertEquals(opp.Amount, amountUpdate.Amount);
387387
}

0 commit comments

Comments
 (0)