Skip to content

Commit

Permalink
Removed the ortoo_FabricatedSObject - have to accept that the library…
Browse files Browse the repository at this point in the history
… is bespoke

Added tests for the DirectedGraph and the new ObjectDescriber methods
  • Loading branch information
rob-baillie-ortoo committed Dec 3, 2021
1 parent ca66375 commit 918873c
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public inherited sharing class DirectedGraph
*/
public DirectedGraph addRelationship( Object child, Object parent )
{
Contract.requires( parentsByChildren.containsKey( child ), 'addRelationship called with a child that has not been added as a node (' + child + ')' );
Contract.requires( parentsByChildren.containsKey( parent ), 'addRelationship called with a parent that has not been added as a node (' + parent + ')' );
parentsByChildren.get( child ).add( parent );
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
@isTest
private without sharing class DirectedGraphTest
{
@isTest
private static void generateSorted_whenASimpleGraphIsSpecified_willReturnTheNodesInOrder() // NOPMD: Test method name format
{
DirectedGraph graph = new DirectedGraph()
.addNode( 'Great grandparent' )
.addNode( 'Grandparent' )
.addNode( 'Parent' )
.addNode( 'Child' )
.addRelationship( 'Child', 'Parent' )
.addRelationship( 'Parent', 'Grandparent')
.addRelationship( 'Grandparent', 'Great grandparent' );

List<Object> expectedNodes = new List<Object>
{
'Child',
'Parent',
'Grandparent',
'Great grandparent'
};

List<Object> returnedNodes = graph.generateSorted();

System.assertEquals( expectedNodes, returnedNodes, 'generateSorted, when a simple graph has been built, will return the nodes in child to parent order' );
}

@isTest
private static void generateSorted_whenAComplexGraphIsSpecified_willReturnTheNodesInOrder() // NOPMD: Test method name format
{
DirectedGraph graph = new DirectedGraph()
.addNode( 'Great grandparent' )
.addNode( 'Grandparent of both parents' )
.addNode( 'Parent 1' )
.addNode( 'Parent 2' )
.addNode( 'Child 1 of Parent 1' )
.addNode( 'Child 2 of Parent 1' )
.addNode( 'Child 1 of Parent 2' )
.addNode( 'Child 2 of Parent 2' )
.addNode( 'Child of Parents 1 and 2' )

.addRelationship( 'Grandparent of both parents', 'Great grandparent' )
.addRelationship( 'Parent 1', 'Grandparent of both parents' )
.addRelationship( 'Parent 2', 'Grandparent of both parents' )
.addRelationship( 'Child 1 of Parent 1', 'Parent 1' )
.addRelationship( 'Child 2 of Parent 1', 'Parent 1' )
.addRelationship( 'Child 1 of Parent 2', 'Parent 2' )
.addRelationship( 'Child 2 of Parent 2', 'Parent 2' )
.addRelationship( 'Child of Parents 1 and 2', 'Parent 1')
.addRelationship( 'Child of Parents 1 and 2', 'Parent 2' );

List<Object> expectedNodes = new List<Object>
{
'Child 1 of Parent 1',
'Child 2 of Parent 1',
'Child 1 of Parent 2',
'Child 2 of Parent 2',
'Child of Parents 1 and 2',
'Parent 1',
'Parent 2',
'Grandparent of both parents',
'Great grandparent'
};

List<Object> returnedNodes = graph.generateSorted();

System.assertEquals( expectedNodes, returnedNodes, 'generateSorted, when a complex graph has been built, will return the nodes in child to parent order' );
}

@isTest
private static void generateSorted_whenNoGraphIsSpecified_willReturnAnEmptyList() // NOPMD: Test method name format
{
DirectedGraph graph = new DirectedGraph();
List<Object> expectedNodes = new List<Object>();

List<Object> returnedNodes = graph.generateSorted();

System.assertEquals( expectedNodes, returnedNodes, 'generateSorted, when no graph has been built, will return the an empty list' );
}

@isTest
private static void generateSorted_whenADuplicatedNodesAreSpecified_willReturnTheUniqueNodesInOrder() // NOPMD: Test method name format
{
DirectedGraph graph = new DirectedGraph()
.addNode( 'Great grandparent' )
.addNode( 'Great grandparent' )
.addNode( 'Great grandparent' )
.addNode( 'Grandparent' )
.addNode( 'Grandparent' )
.addNode( 'Grandparent' )
.addNode( 'Parent' )
.addNode( 'Parent' )
.addNode( 'Child' )
.addNode( 'Child' )
.addNode( 'Child' )
.addNode( 'Child' )
.addNode( 'Child' )
.addRelationship( 'Child', 'Parent' )
.addRelationship( 'Child', 'Parent' )
.addRelationship( 'Child', 'Parent' )
.addRelationship( 'Child', 'Parent' )
.addRelationship( 'Parent', 'Grandparent')
.addRelationship( 'Parent', 'Grandparent')
.addRelationship( 'Grandparent', 'Great grandparent' );

List<Object> expectedNodes = new List<Object>
{
'Child',
'Parent',
'Grandparent',
'Great grandparent'
};

List<Object> returnedNodes = graph.generateSorted();

System.assertEquals( expectedNodes, returnedNodes, 'generateSorted, when duplicate nodes and relationships are specified, will return the unique nodes in child to parent order' );
}

@isTest
private static void generateSorted_whenACircularReference_willThrowAnException() // NOPMD: Test method name format
{
DirectedGraph graph = new DirectedGraph()
.addNode( 1 )
.addNode( 2 )
.addNode( 3 )
.addRelationship( 1, 2 )
.addRelationship( 2, 3 )
.addRelationship( 3, 1 );
Test.startTest();
String exceptionMessage;
try
{
graph.generateSorted();
}
catch ( Exception e )
{
exceptionMessage = e.getMessage();
}
Test.stopTest();

Amoss_Asserts.assertContains( 'The graph contains a circular reference and therefore cannot be resolved', exceptionMessage, 'generateSorted, when a circular reference has been defined, will throw an exception' );
}

@isTest
private static void addRelationship_whenGivenAnInvalidChild_willThrowAnException() // NOPMD: Test method name format
{
DirectedGraph graph = new DirectedGraph()
.addNode( 'Parent' );
Test.startTest();
String exceptionMessage;
try
{
graph.addRelationship( 'UnregisteredChild', 'Parent' );
}
catch ( Exception e )
{
exceptionMessage = e.getMessage();
}
Test.stopTest();

Amoss_Asserts.assertContains( 'addRelationship called with a child that has not been added as a node (UnregisteredChild)', exceptionMessage, 'addRelationship, when given a child that has not previously been added, will throw an exception' );
}

@isTest
private static void addRelationship_whenGivenAnInvalidParent_willThrowAnException() // NOPMD: Test method name format
{
DirectedGraph graph = new DirectedGraph()
.addNode( 'Child' );
Test.startTest();
String exceptionMessage;
try
{
graph.addRelationship( 'Child', 'UnregisteredParent' );
}
catch ( Exception e )
{
exceptionMessage = e.getMessage();
}
Test.stopTest();

Amoss_Asserts.assertContains( 'addRelationship called with a parent that has not been added as a node (UnregisteredParent)', exceptionMessage, 'addRelationship, when given a parent that has not previously been added, will throw an exception' );
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>53.0</apiVersion>
<apiVersion>52.0</apiVersion>
<status>Active</status>
</ApexClass>

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ public class ortoo_FabricatedSObjectRegister {
public static void registerObject( sfab_FabricatedSObject objectToRegister )
{
objectRegister.add( objectToRegister );
if ( objectToRegister instanceof ortoo_FabricatedSobject )
{
graph.addNode( ((ortoo_FabricatedSobject)objectToRegister).getSobjectType() );
}
graph.addNode( objectToRegister.getSobjectType() );
}

public static void registerChildOfRelationship( sfab_FabricatedSObject child, String relationship, sfab_FabricatedSObject parent )
Expand Down Expand Up @@ -82,11 +79,7 @@ public class ortoo_FabricatedSObjectRegister {
this.parent = parent;
this.relationship = relationship;
this.child = child;

if ( child instanceOf ortoo_FabricatedSobject && parent instanceOf ortoo_FabricatedSobject )
{
graph.addRelationship( ((ortoo_FabricatedSobject)child).getSobjectType(), ((ortoo_FabricatedSobject)parent).getSobjectType() );
}
graph.addRelationship( child.getSobjectType(), parent.getSobjectType() );
}

public void register( ortoo_SobjectUnitOfWork uow )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public virtual class sfab_FabricatedSObject {
*/
public sfab_FabricatedSObject( Type sType ) {
this.sType = sType;
ortoo_FabricatedSObjectRegister.registerObject( this );
}

/**
Expand Down Expand Up @@ -340,12 +341,19 @@ public virtual class sfab_FabricatedSObject {
return String.valueOf( sType );
}

// TODO: document
// TODO: test
public SobjectType getSobjectType()
{
return SobjectUtils.getSobjectType( getSobjectName() );
}

/**
* Given a field name, will split it into its parent and child references, checking that the parent relatioonship
* exists and defaulting the field to an empty version of the appropriate object.
*
* @param String - The name of the field to split and defaul
* @return Map<String,String> - The
* @param String - The name of the field to split and default
* @return sfab_FieldNameSplitter
*/
private sfab_FieldNameSplitter defaultParentField( String fieldName ) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,53 @@ public with sharing class sfab_ObjectDescriberTest {
.getObjectTypeForChildRelationship( 'Invalid', 'Account' );
System.assertEquals( null, targetObjectType );
}

@isTest
private static void getFieldForChildRelationship_whenGivenValidNames_expectTheField() // NOPMD: Test method name format
{
SObjectField field = new sfab_ObjectDescriber()
.getFieldForChildRelationship( 'Account', 'Contacts' );
System.assertEquals( Contact.AccountId, field );
}

@isTest
private static void getFieldForChildRelationship_whenGivenAnInvalidRelationship_expectNull() // NOPMD: Test method name format
{
SObjectField field = new sfab_ObjectDescriber()
.getFieldForChildRelationship( 'Account', 'ContactLike' );
System.assertEquals( null, field );
}

@isTest
private static void getFieldForChildRelationship_whenGivenAnInvalidObject_expectNull() // NOPMD: Test method name format
{
SObjectField field = new sfab_ObjectDescriber()
.getFieldForChildRelationship( 'Accountant', 'Contacts' );
System.assertEquals( null, field );
}


@isTest
private static void getFieldForParentRelationship_whenGivenValidNames_expectTheField() // NOPMD: Test method name format
{
SObjectField field = new sfab_ObjectDescriber()
.getFieldForParentRelationship( 'Contact', 'Account' );
System.assertEquals( Contact.AccountId, field );
}

@isTest
private static void getFieldForParentRelationship_whenGivenAnInvalidRelationship_expectNull() // NOPMD: Test method name format
{
SObjectField field = new sfab_ObjectDescriber()
.getFieldForParentRelationship( 'Contact', 'Accooooooo' );
System.assertEquals( null, field );
}

@isTest
private static void getFieldForParentRelationship_whenGivenAnInvalidObject_expectNull() // NOPMD: Test method name format
{
SObjectField field = new sfab_ObjectDescriber()
.getFieldForParentRelationship( 'Accountant', 'Account' );
System.assertEquals( null, field );
}
}

0 comments on commit 918873c

Please sign in to comment.