Skip to content

Commit

Permalink
Added ability to find if a DmlDefiner will delete other records
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-baillie-ortoo committed Mar 23, 2022
1 parent f1b0c09 commit 63c0a18
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,10 @@ public inherited sharing class DmlChildContext
uow.registerRelationship( child, relatedByField, parent );
}
}

@testVisible
private SobjectField getRelatedByField()
{
return relatedByField;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,16 @@ public inherited sharing class DmlDefiner
return sobjects;
}

/**
* States if this definer is configured to ensure that other records are deleted
*
* @return Boolean Will other records be deleted
*/
public Boolean getWillDeleteOtherRecords() {
Contract.assert( options != null, 'getWillDeleteOtherRecords was called when options was null' );
return options.getWillDeleteOtherRecords();
}

@testVisible
private List<DmlRecord> getDmlRecords() {
return recordsToDml;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,14 @@ public inherited sharing class DmlDefinerOptions
return new DmlDefinerOptions()
.setOtherRecordsMode( OtherRecordsOption.IGNORE_RECORDS );
}

/**
* States if this definer will instruct to delete other records
*
* @return Boolean Should other records be deleted?
*/
public Boolean getWillDeleteOtherRecords()
{
return getOtherRecordsMode() == OtherRecordsOption.DELETE_RECORDS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,10 @@ public virtual inherited sharing class DmlRecord
checkTypeIsValid( childRecordType );
return childContextsByType.get( childRecordType );
}

@testVisible
private Boolean getWillDeleteOtherRecords( String childRecordType )
{
return getChildDefiner( childRecordType )?.getWillDeleteOtherRecords();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,30 @@ private without sharing class DmlDefinerOptionsTest

ortoo_Asserts.assertContains( 'setOtherRecordsMode called with a null otherRecordsMode', exceptionMessage, 'setOtherRecordsMode, when called with null, will throw an exception' );
}

@isTest
private static void getWillDeleteOtherRecords_whenRecordModeIsDelete_returnsTrue() // NOPMD: Test method name format
{
DmlDefinerOptions options = new DmlDefinerOptions();

Test.startTest();
options.setOtherRecordsMode( DmlDefinerOptions.OtherRecordsOption.DELETE_RECORDS );
Boolean got = options.getWillDeleteOtherRecords();
Test.stopTest();

System.assertEquals( true, got, 'getWillDeleteOtherRecords, when the record mode is DELETE_RECORDS, will return true' );
}

@isTest
private static void getWillDeleteOtherRecords_whenRecordModeIsIgnore_returnsFalse() // NOPMD: Test method name format
{
DmlDefinerOptions options = new DmlDefinerOptions();

Test.startTest();
options.setOtherRecordsMode( DmlDefinerOptions.OtherRecordsOption.IGNORE_RECORDS );
Boolean got = options.getWillDeleteOtherRecords();
Test.stopTest();

System.assertEquals( false, got, 'getWillDeleteOtherRecords, when the record mode is IGNORE_RECORDS, will return false' );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,24 @@ private without sharing class DmlDefinerTest

ortoo_Asserts.assertContains( 'addPreSaveAction called with a null action', exceptionMessage, 'addPreSaveAction, when given null, will throw an exception' );
}

@isTest
private static void getWillDeleteOtherRecords_willReturnTheValueFromTheDefiner() // NOPMD: Test method name format
{
DmlDefiner dmlDefiner = new DmlDefiner();

Amoss_Instance optionsController = new Amoss_Instance( DmlDefinerOptions.class );
optionsController
.expects( 'getWillDeleteOtherRecords' )
.returning( true );

Test.startTest();
dmlDefiner.setOptions( (DmlDefinerOptions)optionsController.generateDouble() );
Boolean got = dmlDefiner.getWillDeleteOtherRecords();
Test.stopTest();

optionsController.verify();

System.assertEquals( true, got, 'getWillDeleteOtherRecords, will call getWillDeleteOtherRecords on the options and return the result' );
}
}

0 comments on commit 63c0a18

Please sign in to comment.