function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Cody DrennonCody Drennon 

How to write a test class for a batch class that has child and parent object

Hello, I am trying to write a test class for the following batch class but cant seem to get past the first select statement in the soql query.  Below class is my current test class.  Any help would be great.  Thanks!

BATCH CLASS
********************************************************************************************************************************/

global class ATS_CCCAPAuthRollUpBatch implements Database.Batchable<sObject>,Database.stateful,Schedulable{
    
    public integer counter = 0;   
    public Boolean validFiscAgree = false;
    
    global Database.QueryLocator start(Database.BatchableContext BC) {
        return Database.getQueryLocator('SELECT Id, (SELECT Id, Fiscal_Agreement_Valid__c, CI_Effective_Begin_Date__c, CI_Effective_End_Date__c  FROM CCCAP_Authorization__r WHERE LastModifiedDate >= YESTERDAY OR CreatedDate >= YESTERDAY), ' + 
                                        '(Select Id, Status__c From Fiscal_Agreement_Details__r WHERE CreatedDate >= YESTERDAY OR LastModifiedDate >= YESTERDAY OR CI_Effective_Begin_Date__c = TODAY OR CI_Effective_End_Date__c < TODAY) ' +
                                        'FROM County__c WHERE Id IN (SELECT Provider_County__c FROM Fiscal_Agreement_Details__c WHERE CreatedDate >= YESTERDAY ' + 
                                        'OR LastModifiedDate >= YESTERDAY OR CI_Effective_Begin_Date__c = TODAY OR CI_Effective_End_Date__c < TODAY)');   
        
       
    }
    
    global void execute(Database.BatchableContext BC, List<County__c> scope){
        List<CCCAP_Authorization__c> authList = new List<CCCAP_Authorization__c>();
        
         system.debug('********************* THIS IS THE Execute METHOD');

        system.debug('THIS IS THE SCOPE OBJECT.........COUNTY_C ' + scope);        
        
        for(County__c county : scope){ 
            for(CCCAP_Authorization__c auth : county.CCCAP_Authorization__r) {
                for(Fiscal_Agreement_Details__c fiscal : county.Fiscal_Agreement_Details__r) {
                    if(fiscal.Status__c == 'Active' && !auth.Fiscal_Agreement_Valid__c && auth.CI_Effective_Begin_Date__c <= date.TODAY() && auth.CI_Effective_End_Date__c > date.TODAY() )
                    {  
                        auth.Fiscal_Agreement_Valid__c = true;
                        authList.add(auth);
                        counter++;
                    }
                    else if(auth.Fiscal_Agreement_Valid__c && (fiscal.Status__c != 'Active' || auth.CI_Effective_Begin_Date__c > date.TODAY() || auth.CI_Effective_End_Date__c < date.TODAY())) 
                    {
                        auth.Fiscal_Agreement_Valid__c = false;
                        authList.add(auth);
                        counter++;
                    }   
                }
            }   
        }
        if(!authList.isEmpty()){
            system.debug('AuthList' + authList);
            update authList;
        }
    } 
    
    global void finish(Database.BatchableContext BC) {
        system.debug('The batch has successfully updated ' + counter + ' records');
    }
    
    global void execute(SchedulableContext sc){
        ATS_CCCAPAuthRollUpBatch arb = new ATS_CCCAPAuthRollUpBatch();
        database.executeBatch(arb,100);
    }
    
}

________________________________________________________________
TEST CLASS

@isTest
public class ATS_CCCAPAuthRollUpBatch_Test {
    
   @isTest
    static void testAuthBatch() {
        
        County__c c = new County__c();
         CCCAP_Authorization__c auth = new CCCAP_Authorization__c();
        
        auth.County__c = c.id;
        
        auth.Fiscal_Agreement_Valid__c = true;
        auth.CI_Effective_Begin_Date__c = date.valueOf('2017-04-12');
        auth.CI_Effective_End_Date__c = date.valueOf('2019-09-12');
        auth.CreatedDate= date.valueOf('2018-04-18');
        auth.LastModifiedDate = date.valueOf('2018-04-18');
        
        Fiscal_Agreement_Details__c fad = new Fiscal_Agreement_Details__c();
        fad.Provider_County__c = c.id;
        
        fad.CI_Effective_Begin_Date__c = date.valueOf('2017-04-12');
        fad.CI_Effective_End_Date__c = date.valueOf('2019-09-12');
        fad.CreatedDate= date.valueOf('2018-04-18');
        
        List<CCCAP_Authorization__c> authList = new List<CCCAP_Authorization__c>();
       
        
        
        Test.startTest();
        ATS_CCCAPAuthRollUpBatch arb = new ATS_CCCAPAuthRollUpBatch();
        Database.executeBatch(arb);
        Test.stopTest();
        
        
    }

}