Skip to main content
Full Circle Insights

Creating Unit Tests for Attribution Models

Unit tests for attribution models should follow a basic structure.

  1. Install a model using the TestInstallConfigurationTypeClass API
  2. Create an instance of a model 
  3. Create test records of sObjects and/or custom settings
  4. Instantiate collections that will be passed as parameters to the model's (Opportunity/Account)DetermineAttribution function
  5. Populate collections with test records
  6. Call (Opportunity/Account)DetermineAttribution on the model instance
  7. Write assert statements that verify whether attributition was done correctly

Response Management Compatibility Modifications: Change namespace prefix to 'FCRM'. Also, when creating test records, be sure to use the right field names. For instance, on Campaign Attribution, Response Date is 'FCI_Response_Date__c', while in Response Management its 'FCR_Response_Date__c'

Custom Opportunity Model Test Example

Here's a partially implemented unit test for the opportunity model in the examples above.

public static void TestSimpleOpConfigurationModel(
    Map<ID, List<FCCI.FCI_CampaignInfluenceAPI.OpportunityRevenue>> OpenOpportunityRevenue,
    Map<ID, List<FCCI.FCI_CampaignInfluenceAPI.OpportunityRevenue>> ClosedOpportunityRevenue,
    Map<ID, List<FCCI.FCI_CampaignInfluenceAPI.OpportunityRevenue>> LostOpportunityRevenue)
{
    // Create instance of the simple opportunity configuration type class, i.e. model type class
    SimpleOpConfiguration.OpConfigurationType SimpleOpModelType = new SimpleOpConfiguration.OpConfigurationType();                 
     
    // Install model
    FCCI.FCI_InfluenceConfigurationPluginAPI.TestInstallConfigurationTypeClass(SimpleOpModelType.GetUniqueName());              
     
    // Create an instance of a configuration type, i.e. model
    SimpleOpModelType.CreateConfiguredInstance('TestSimpleOpConfiguration', 'Test Description');
    FCCI.FCI_CampaignInfluenceAPI.ICampaignAttributionConfiguration SimpleOppModel = SimpleOpModelType.GetConfiguredInstances()[0];       
         
    // Create test records and custom settings if needed 
     
    // Populate these collections with test records.
    // Make sure the attribution code can reference the fields it will reference from the test records  
    Map<ID, Map<ID,CampaignMember>> CreatingContactResponses = new Map<ID, Map<ID,CampaignMember>> ();
    Map<ID, Map<ID, CampaignMember>> PrimaryContactResponses = new Map<ID, Map<ID,CampaignMember>> ();
    Map<ID, Map<ID, CampaignMember>> OtherContactResponses = new Map<ID, Map<ID,CampaignMember>> ();
    Map<ID, Opportunity> opportunities = new Map<ID, Opportunity>();
 
    String state;
    Test.StartTest();
    state = SimpleOppModel.Start();
     
    // Calculate attribution
    state = SimpleOppModel.OpportunityDetermineAttribution(opportunities, CreatingContactResponses, PrimaryContactResponses, OtherContactResponses,
        OpenOpportunityRevenue, ClosedOpportunityRevenue, LostOpportunityRevenue, State);    
       
    SimpleOppModel.Finish(state);
    Test.StopTest();  
 
    // Write assert statements that verify that the OpportunityRevenue objects were updated correctly   
}

Custom Account Model Test Example

Here's a similar example of an Account model unit test:

public static void TestSimpleAccountConfigurationModel(
    Map<ID, List<FCCI.FCI_CampaignInfluenceAPI.OpportunityRevenue>> OpenOpportunityRevenue,
    Map<ID, List<FCCI.FCI_CampaignInfluenceAPI.OpportunityRevenue>> ClosedOpportunityRevenue,
    Map<ID, List<FCCI.FCI_CampaignInfluenceAPI.OpportunityRevenue>> LostOpportunityRevenue)
{
    // Create instance of the simple account configuration type class, i.e. model type class
    SimpleAccountConfiguration.AccountConfigurationType SimpleAccountModelType = new SimpleAccountConfiguration.AccountConfigurationType();                 
     
    // Install model
    FCCI.FCI_InfluenceConfigurationPluginAPI.TestInstallConfigurationTypeClass(SimpleAccountModelType.GetUniqueName());              
     
    // Create an instance of a configuration type, i.e. model
    SimpleAccountModelType.CreateConfiguredInstance('TestSimpleAccountConfiguration', 'Test Description');
    FCCI.FCI_CampaignInfluenceAPI.ICampaignAttributionConfiguration SimpleAccountModel = SimpleAccountModelType.GetConfiguredInstances()[0];       
         
    // Create test records and custom settings if needed 
     
    // Populate these collections with test records.
    // Make sure the attribution code can reference the fields it will reference from the test records  
    Map<ID, Account> accounts = new Map<ID, Account>();
    Map<ID, Map<ID, Opportunity>> opportunities = new Map<ID, Map<ID, Opportunity>>();
 
    String state;
    Test.StartTest();
    state = SimpleAccountModel.Start();
     
    // Calculate attribution
    state = SimpleAccountModel.AccountDetermineAttribution(accounts, opportunities, OpenOpportunityRevenue, ClosedOpportunityRevenue, LostOpportunityRevenue, State);    
       
    SimpleAccountModel.Finish(state);
    Test.StopTest();;  
 
    // Write assert statements that verify that the OpportunityRevenue objects were updated correctly   
}

 

  • Was this article helpful?