From 6ba852512dd06de4144e6c5045534dd72f00aaeb Mon Sep 17 00:00:00 2001 From: Renato Date: Mon, 8 Jan 2018 12:30:47 -0200 Subject: [PATCH 1/2] Added text about id generator. Removed invalid ids from the examples. --- readme.md | 44 ++++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/readme.md b/readme.md index b22e1e7..3a0eda3 100644 --- a/readme.md +++ b/readme.md @@ -22,32 +22,32 @@ SObjectFabricator provides the ability to set any field value, including system, ```java sfab_FabricatedSObject fabricatedAccount = new sfab_FabricatedSObject(Account.class); -fabricatedAccount.setField(Account.Id, 'Id-1'); +fabricatedAccount.setField(Account.Id, '001000000000001'); fabricatedAccount.setField(Account.LastModifiedDate, Date.newInstance(2017, 1, 1)); fabricatedAccount.setChildren('Opportunities', new List { - new sfab_FabricatedSObject(Opportunity.class).setField(Opportunity.Id, 'OppId-1'), - new sfab_FabricatedSObject(Opportunity.class).setField(Opportunity.Id, 'OppId-2') + new sfab_FabricatedSObject(Opportunity.class).setField(Opportunity.Id, '001000000000001'), + new sfab_FabricatedSObject(Opportunity.class).setField(Opportunity.Id, '001000000000002') }); -Account acct = (Account)fabricatedAccount.toSObject(); +Account acct = (Account) fabricatedAccount.toSObject(); -// Account:{LastModifiedDate=2017-01-01 00:00:00, Id=Id-1} +// Account:{LastModifiedDate=2017-01-01 00:00:00, Id=001000000000001} System.debug(acct); -// (Opportunity:{Id=OppId-1}, Opportunity:{Id=OppId-2}) +// (Opportunity:{Id=001000000000001}, Opportunity:{Id=001000000000002}) System.debug(acct.Opportunities); sfab_FabricatedSObject fabricatedOpportunity = new sfab_FabricatedSObject(Opportunity.class); -fabricatedOpportunity.setField(Opportunity.Id, 'OppId-3'); +fabricatedOpportunity.setField(Opportunity.Id, '001000000000003'); fabricatedOpportunity.setParent('Account', fabricatedAccount); -Opportunity opp = (Opportunity)fabricatedOpportunity.toSObject(); +Opportunity opp = (Opportunity) fabricatedOpportunity.toSObject(); -// Opportunity:{Id=OppId-3} +// Opportunity:{Id=001000000000003} System.debug(opp); -// Account:{LastModifiedDate=2017-01-01 00:00:00, Id=Id-1} +// Account:{LastModifiedDate=2017-01-01 00:00:00, Id=001000000000003} System.debug(opp.Account); ``` @@ -55,7 +55,7 @@ System.debug(opp.Account); ```java Map accountValues = new Map { - Account.Id => 'Id-1', + Account.Id => '001000000000001', Account.LastModifiedDate => Date.newInstance(2017, 1, 1) }; @@ -68,11 +68,27 @@ The example above is a bit verbose. We can simplify it by leveraging the fluent ```java Account acct = (Account)new sfab_FabricatedSObject(Account.class) - .setField(Account.Id, 'Id-1') + .setField(Account.Id, '001000000000001') .setField(Account.LastModifiedDate, Date.newInstance(2017, 1, 1)) .setChildren('Opportunities', new List { - new sfab_FabricatedSObject(Opportunity.class).setField(Opportunity.Id, 'OppId-1'), - new sfab_FabricatedSObject(Opportunity.class).setField(Opportunity.Id, 'OppId-2') + new sfab_FabricatedSObject(Opportunity.class).setField(Opportunity.Id, '001000000000001'), + new sfab_FabricatedSObject(Opportunity.class).setField(Opportunity.Id, '001000000000002') }).toSObject(); ``` +### You can use Id generators, too + +Since the record's `Id` is passed as a string, it is possible to use a generator class, like the one featured in [Financial Force's Apex Mocks library](https://github.com/financialforcedev/fflib-apex-mocks/blob/master/src/classes/fflib_IDGenerator.cls): + +For example, to create an opportunity with a given number of products, without actually creating the product records: + +```java +Opportunity opportunity_a = (Opportunity) new sfab_FabricatedSObject(Opportunity.class) + .setField(Opportunity.Id, IDGenerator.generate(Opportunity.SObjectType)) + .setField(Opportunity.TotalOpportunityQuantity, 1) + .toSObject(); +// Opportunity:{TotalOpportunityQuantity=1.0, Id=006000000000001} +System.debug(opportunity_a); +``` + +Since the field is generated from the number of `OpportunityLineItem` records related to the opportunity, that field would be 1 or more only if we inserted the opportunity record to the database and after that inserted an line item. From 288c073feb3e23d850321c07221f2781c32fc738 Mon Sep 17 00:00:00 2001 From: Renato Oliveira Date: Thu, 8 Feb 2018 16:58:32 -0200 Subject: [PATCH 2/2] Added method to set parent relationships with field describe. --- src/classes/sfab_FabricatedSObject.cls | 5 +++++ src/classes/sfab_FabricatedSObjectTest.cls | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/src/classes/sfab_FabricatedSObject.cls b/src/classes/sfab_FabricatedSObject.cls index e243207..3003f5e 100644 --- a/src/classes/sfab_FabricatedSObject.cls +++ b/src/classes/sfab_FabricatedSObject.cls @@ -29,6 +29,11 @@ public virtual class sfab_FabricatedSObject { return this; } + public sfab_FabricatedSObject setParent(Schema.DescribeFieldResult fieldDescribe, sfab_FabricatedSObject fabricatedParent) { + nodes.add(new sfab_ParentRelationshipNode(fieldDescribe.getRelationshipName(), fabricatedParent)); + return this; + } + public sfab_FabricatedSObject setChildren(String relationshipName, List fabricatedChildren) { nodes.add(new sfab_ChildRelationshipNode(relationshipName, fabricatedChildren)); return this; diff --git a/src/classes/sfab_FabricatedSObjectTest.cls b/src/classes/sfab_FabricatedSObjectTest.cls index 033cbf4..ddc2e85 100644 --- a/src/classes/sfab_FabricatedSObjectTest.cls +++ b/src/classes/sfab_FabricatedSObjectTest.cls @@ -49,6 +49,13 @@ private class sfab_FabricatedSObjectTest { System.assertEquals(1, fabricatedSObject.nodes.size()); } + @isTest + private static void setParentWithDescribe_expectFieldAddedToNodes() { + sfab_FabricatedSObject fabricatedSObject = new sfab_FabricatedSObject(Opportunity.class); + fabricatedSObject.setParent(Opportunity.AccountId.getDescribe(), new sfab_FabricatedSObject(Account.class)); + System.assertEquals(1, fabricatedSObject.nodes.size()); + } + @isTest private static void setChildren_expectFieldAdded() { sfab_FabricatedSObject fabricatedSObject = new sfab_FabricatedSObject(Account.class);