rsoesemann / apex-domainbuilder Goto Github PK
View Code? Open in Web Editor NEWFramework to setup Apex test data in a highly flexible and readable way using the Test Data Builder pattern.
License: MIT License
Framework to setup Apex test data in a highly flexible and readable way using the Test Data Builder pattern.
License: MIT License
I'd like to use Domain Builder with circular relationships.
Object ParentObject__c
with has a lookup to ChildObject__c
Object ChildObject__c
has a M/D relation with ParentObject__c
@IsTest
public class Parent_t extends DomainBuilder {
public Parent_t() {
super(ParentObject__c.SObjectType);
}
}
@IsTest
public class Child_t extends DomainBuilder{
public Child_t(Parent_t p) {
super(ChildObject__c.SObjectType);
setParent(ChildObject__c.Parent__c, p);
}
public Child_t add(Parent_t p) {
return (Child_t) p.setParent(ParentObject__c.Child__c, this);
}
}
@IsTest
private class TestDomainBuilder {
@TestSetup
private static void setup() {
Parent_t p = new Parent_t();
Child_t c = new Child_t(p);
//p.persist(); // this does not help as workaround
c.add(p);
p.persist();
}
@IsTest
private static void testSetup() {
System.assertEquals(1, [SELECT Id FROM ParentObject__c].size());
System.assertEquals(1, [SELECT Id FROM ChildObject__c].size());
System.assertEquals(1, [SELECT Id FROM ParentObject__c WHERE Child__c != NULL].size());
}
}
Test will fail:
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Parent__c]: [Parent__c]
If I persist before adding the circular reference, the error is gone but the assert fails.
When copied the library to my org and deployed it on scratch org for validation, one test is failing
DomainBuilder_Test.noUnneededRecords Fail System.AssertException: Assertion Failed: Expected: 6, Actual: 5
Class.DomainBuilder_Test.noUnneededRecords: line 19, column 1
Good stuff Robert as always!
It is lovely that uow speeds DML actions up, but for many of us, bringing in parts of fflib into production code needs approval.
Also the main goal of this repo is to have a clean testBuilder, while uow is only nice-to-have feature. Wonder if it is better to decouple uow out? My 5cents.
Br,
Xi
I have new refresh sandbox and getting below mentioend error while deploy package.
Unhandled Exception : java.lang.RuntimeException: You acknowledge and agree that the CLI tool may collect usage information, user environment, and crash reports for the purposes of providing services or functions that are relevant to use of the CLI tool and product improvements. Warning: The "--outputdir" flag has been deprecated. Use "--output-dir | -d" instead. Error (1): The path "sfdx-source/apex-domainbuilder-samplecode", specified in sfdx-project.json, does not exist. Be sure this directory is included in your project root.
I've tried using the framework in a test class and indeed the code is cleaner and more readable.
However I noticed some delays in the test execution. Below you will see a test class with two methods.
In the first method, I'm using the framework and the Domain classes, which handle some tasks automatically (such as creating standard pricebook entries for new products).
In the second method, I'm inserting all records manually without the framework. Both methods include two loops that runs 50 times each to create new records.
public class DemandService_Test {
@isTest
public static void testWithDomainBuilder() {
// Given
Account_t acc = new Account_t();
Opportunity_t oppo = new Opportunity_t(acc).add(new Contact_t());
List<Product_t> products = new List<Product_t>();
List<Schema.PicklistEntry> picklistEntries = Product2.Family.getDescribe().getPicklistValues();
for(Integer i=0; i<50; i++) {
products.add(
new Product_t().name('Test Product ' + i).code('TESTP' + i).family(picklistEntries[Math.floor(Math.random() * ((19 - 0) + 1) + 0).intValue()].value)
);
}
for(Product_t p : products) {
new Demand(acc, p);
}
acc.persist();
// When
Test.startTest();
for(Product_t p : products) {
new OpportunityLineItem_t(oppo, p).quantity(new Random().integer(1,100));
}
Test.stopTest();
// Then
List<OpportunityLineItem> oppoItems = [SELECT Id, Product2Id, KS_AchievableDemand__c, KS_AchievableDemand__r.KS_Product__c FROM OpportunityLineItem];
for(OpportunityLineItem oppoItem : oppoItems) {
Assert.areEqual(oppoItem.Product2Id, oppoItem.KS_AchievableDemand__r.KS_Product__c);
}
}
@isTest
public static void testWithoutDomainBuilder() {
// Given
Account acc = new Account(Name = 'Test');
insert acc;
Opportunity oppo = new Opportunity(AccountId = acc.Id, Name = 'test', StageName = 'Qualification', closeDate = Date.today(), Pricebook2Id = Test.getStandardPricebookId());
insert oppo;
List<Product2> products = new List<Product2>();
List<Schema.PicklistEntry> picklistEntries = Product2.Family.getDescribe().getPicklistValues();
for(Integer i=0; i<50; i++) {
products.add(
new Product2(Name = 'Test Product ' + i, ProductCode = 'TESTP' + i, Family = picklistEntries[Math.floor(Math.random() * ((19 - 0) + 1) + 0).intValue()].value)
);
}
insert products;
AchievableDemand__c[] demands = new List<AchievableDemand__c>();
for(Product2 p : products) {
new AchievableDemand__c(Account__c = acc.Id, Product__c = p.Id);
}
insert demands;
PricebookEntry[] entries = new List<PricebookEntry>();
for(Product2 p : products) {
entries.add(
new PricebookEntry(
Product2Id = p.Id,
Pricebook2Id = Test.getStandardPricebookId(),
UnitPrice = 10,
IsActive = true
)
);
}
insert entries;
// When
Test.startTest();
OpportunityLineItem[] oppoItems = new List<OpportunityLineItem>();
for(Product2 p : products) {
oppoItems.add(
new OpportunityLineItem(
OpportunityId = oppo.Id,
Product2Id = p.Id,
Quantity = 10,
TotalPrice = 10
)
);
}
insert oppoItems;
Test.stopTest();
// Then
oppoItems = [SELECT Id, Product2Id, AchievableDemand__c, AchievableDemand__r.Product__c FROM OpportunityLineItem];
for(OpportunityLineItem oppoItem : oppoItems) {
Assert.areEqual(oppoItem.Product2Id, oppoItem.AchievableDemand__r.Product__c);
}
}
}
As you can see in the below log analysis, the first test method, which utilizes the framework, took 8 times more time to run.
I'm not sure if the loop is responsible for the delay, as I read this comment by @rsoesemann suggesting that the framework isn't designed for inserting many records. It just makes me wonder what will happen in a larger test class with multiple different scenarios to test, each one with a separated test method that needs test data. I'm concerned about the potential performance impact of using the framework in those cases.
I'm posting this as a question, in case anyone experienced performance issues with the framework, or can point out if I might be using it incorrectly.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.