Code Monkey home page Code Monkey logo

apex-domainbuilder's Introduction

My GitHub stats

apex-domainbuilder's People

Contributors

anmolgkv avatar anmollogicline avatar imjohnmdaniel avatar manjot0074 avatar rsoesemann avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

apex-domainbuilder's Issues

How to handle circular relationship?

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.

Failing test

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

[Question] remove uow dependency?

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

Getting issue while deploy into Sandbox

I have new refresh sandbox and getting below mentioend error while deploy package.
Uploading GitHub-Salesforce-Deploy-Tool Error.png…

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.

[Question] Is the framework causing performance issues in large-scale tests with multiple test records?

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.
Screenshot 2024-09-06 at 12 06 22

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.