Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added text about id generator. Removed invalid ids from the examples. #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 30 additions & 14 deletions readme.md
Original file line number Diff line number Diff line change
@@ -22,40 +22,40 @@ 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<sfab_FabricatedSObject> {
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);
```

### Set non-relationship field values in bulk

```java
Map<SObjectField, Object> accountValues = new Map<SObjectField, Object> {
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<sfab_FabricatedSObject> {
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.
5 changes: 5 additions & 0 deletions src/classes/sfab_FabricatedSObject.cls
Original file line number Diff line number Diff line change
@@ -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<sfab_FabricatedSObject> fabricatedChildren) {
nodes.add(new sfab_ChildRelationshipNode(relationshipName, fabricatedChildren));
return this;
7 changes: 7 additions & 0 deletions src/classes/sfab_FabricatedSObjectTest.cls
Original file line number Diff line number Diff line change
@@ -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);