Skip to content

Commit

Permalink
Added tests for the persistence of fabricated SObjects
Browse files Browse the repository at this point in the history
Defend against some invalid values being populated when persisting
  • Loading branch information
rob-baillie-ortoo committed Dec 6, 2021
1 parent 4a244d4 commit 1d0d3c9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
10 changes: 4 additions & 6 deletions TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@ Licenses that are needed with the source code and binary:
* Amoss - https://github.com/bobalicious/amoss/blob/main/LICENSE
* SObject Fabricator - https://github.com/bobalicious/SObjectFabricator/blob/master/LICENSE

TODO:
* Watch recording of the UI for notifications


SObject Fabricator:
* Test opportunity, opportunity line item, product, pricebook, pricebook entry

* Review changes to sfab_FabricatedSObject
* Ensure the registration is complete - there are probably gaps
* see sfab_FabricatedSObject.addParentChild

* Why do we need the null check in the post-process?

* Unit Test
* sfab_FabricatedSObject
* Do an end to end test for the fabricator committing stuff
* Set unsettable fields, and a parent and child
* Add docs to objectdescriber and sfab_FabricatedSObject
* Particularly a reference to the original framework

* Add defaulting mechanism
* Make all the classes @isTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,14 +298,22 @@ public virtual class sfab_FabricatedSObject {
* @return SObject - The built SObject
*/
public SObject toSObject() {
SObject newObject = (SObject)JSON.deserialize(JSON.serialize(serialize()), sType);
postBuildProcess( newObject );
return newObject;
return internalToSobject( false );
}

/**
* Builds the SObject that this sfab_FabricatedSObject represents, in a form that means it can be persisted
*
* @return SObject - The built SObject
*/
public SObject toPersistableSobject()
{
SObject newObject = (SObject)JSON.deserialize(JSON.serialize(serialize(true)), sType);
return internalToSobject( true );
}

private Sobject internalToSobject( Boolean persistable )
{
SObject newObject = (SObject)JSON.deserialize( JSON.serialize( serialize( persistable ) ), sType );
postBuildProcess( newObject );
return newObject;
}
Expand All @@ -316,7 +324,7 @@ public virtual class sfab_FabricatedSObject {
*/
public void postBuildProcess( SObject objectToProcess ) {
for ( String nodeName : nodes.keySet() ) {
nodes.get( nodeName ).postBuildProcess( objectToProcess );
nodes.get( nodeName )?.postBuildProcess( objectToProcess );
}
}

Expand Down Expand Up @@ -689,8 +697,15 @@ public virtual class sfab_FabricatedSObject {
}

public Map<String,Object> serialize( Boolean persistable ) {

Map<String,Object> noNodes = new Map<String,Object>();

if ( persistable && getName().equals( 'Id' ) ) {
System.debug( 'Id with the value "' + value + '" stripped from object' );
return noNodes; // Ids cannot be set when persisting an object - everything is an insert
}
if ( fieldIsBlob() ) {
return new Map<String,Object>(); // is handled by the post process instead as deserializing a blob field does not work (API 50)
return noNodes; // is handled by the post process instead as deserializing a blob field does not work (API 50)
} else {
return new Map<String,Object> { getName() => value };
}
Expand Down Expand Up @@ -766,9 +781,14 @@ public virtual class sfab_FabricatedSObject {
// child objects in same order as the input map, otherwise the Blob field values
// will be applied to the wrong objects. As far as we can tell, this is reliable
// at the time of writing. If it becomes not the case, this will need re-writing
if ( objectToProcess == null )
{
return; // TODO: why is this needed - why is this sometimes null?
}

List<Sobject> childSobjects = ((Sobject)objectToProcess).getSObjects( fieldName );
for ( Integer i=0; i<childSobjects.size(); i++ ) {
children[i].postBuildProcess( childSobjects[i] );
children[i]?.postBuildProcess( childSobjects[i] );
}
}
}
Expand Down

0 comments on commit 1d0d3c9

Please sign in to comment.