+ Start a Discussion
Adam RycroftAdam Rycroft 

Problems lookup fields in test class

Hello,

I'm having trouble figuring out the lookup fields in a test class. There is a custom object called Houshold_Member__c. Another custom object called Income__c has a lookup field to it. Household__c is a master-detail relationship to a person account. 

@isTest
public class testProcessIncome {
    
    public static void createHouseholdAndIncome (integer numOfHousehold, integer numOfIncome){
        
        list<Household_Member__c> HouseholdList = new list<Household_Member__c>();
        
        for(integer i = 1; i <= numOfHousehold; i++){
            Household_member__c newA = new Household_member__c(
            Household__c = ??????
            Household_Member__c = ????
            Status__c = 'Current'
            );
            householdList.add(newA);
        }
    
      insert householdList;
        
        list<income__c> IncomeList = new list<income__c>();
        
        for(household_member__c a : householdList){
            for(integer i = 1; i <= numOfIncome; i++){
                
                if(i<=12){
                    income__c d = new income__c(
                        start_of_pay_period__c = date.newInstance(2017, i, 1),
                        Household_Member__c = a.id
                    );                    
                  IncomeList.add(d);
                    
                } else {
                    income__c d = new income__c(
                        start_of_pay_period__c = date.newInstance(2017, 1, 1),
                        household_member__c = a.id
                    );                
                    IncomeList.add(d);
                }
            }
        }
        
        insert IncomeList;
        
        list<income__c> dCheck = [select id, household_member__c, status__c, start_of_pay_period__c from income__c];
        system.debug(dCheck);
        
  }
    
    public static testMethod void insertincome(){
        
        createHouseholdandIncome(1,2);
        
        household_member__c a = [select id from household_member__c limit 1];
        
        income__c d = new income__c(
          start_of_pay_period__c = date.newInstance(2017, 02, 15),
          household_member__c = a.id
        );
        
        test.startTest();
        
        insert d;
        
        test.stopTest();
        
        income__c dUpdated = [select id, status__c from income__c where start_of_pay_period__c = 2017-02-15];
        
        system.assertEquals('Current',dUpdated.status__c);
        
    }
    
    public static testMethod void insertMultipleIncome(){
        
        test.startTest();
        
        createHouseholdAndIncome(1,2);
        
        test.stopTest();
        
        household_member__c a = [select id, (select id, status__c, start_of_pay_period__c from Incomes__r order by start_of_pay_period__c desc, createddate desc) from household_member__c limit 1];
        
        for(income__c d : a.incomes__r){
            
            if(d.id == a.incomes__r[0].id){
                system.assertEquals('Current',d.status__c);
            } else {
                system.assertEquals('Past',d.status__c);
            }
            
        }
        
    }
    
    public static testMethod void deleteIncome(){
        
        createHouseholdAndIncome(1,2);
        
        household_member__c a = [select id, (select id, status__c, start_of_pay_period__c from incomes__r order by start_of_pay_period__c desc, createddate desc) from household_member__c limit 1];
        
        list<income__c> dListToDelete = new list<income__c>();
        
        income__c dToDelete = [select id from income__c where status__c = 'Current' limit 1];
        
        test.startTest();
        
        delete dToDelete;
        
        for(household_member__c aUpdated : [select id, (select id, status__c, start_of_pay_period__c from incomes__r order by start_of_pay_period__c desc, createddate desc) from household_member__c where id = :a.id]){
            system.assertEquals(1,aUpdated.incomes__r.size());
            for(income__c d : aUpdated.incomes__r){
                system.assertEquals('Current',d.status__c);
            }
        }
        
        income__c dToUndelete = [select id from income__c where isdeleted = true limit 1 ALL ROWS];
        
        undelete dToUndelete;
        
        for(household_member__c aUpdated : [select id, (select id, status__c, start_of_pay_period__c from incomes__r order by start_of_pay_period__c desc, createddate desc) from household_member__c where id = :a.id]){
            system.assertEquals(2,aUpdated.incomes__r.size());
            for(income__c d : aUpdated.incomes__r){
                if(d.id == aUpdated.incomes__r[0].id){
                  system.assertEquals('Current',d.status__c);
                } else {
                    system.assertEquals('Past',d.status__c);
                }
            }
        }
        
        test.stopTest();
        
    }
    
    public static testMethod void updateIncomeDate(){
        
        createHouseholdAndIncome(1,2);
        
        household_member__c a = [select id, (select id, status__c, start_of_pay_period__c from incomes__r order by start_of_pay_period__c desc, createddate desc) from household_member__c limit 1];
        
        list<income__c> dList = new list<income__c>();
        
        for(income__c d : a.incomes__r){
            if(d.id == a.incomes__r[0].id){
                d.start_of_pay_period__c = date.newInstance(2016,12,1);
                dList.add(d);
            }
        }
        
        test.startTest();
        
        checkRecursive.reset();
        update dList;
        
        test.stopTest();
        
        household_member__c aUpdated = [select id, (select id, status__c, start_of_pay_period__c from incomes__r order by start_of_pay_period__c desc, createddate desc) from household_member__c limit 1];
        
        list<income__c> dListUpdated = new list<income__c>();
        
        for(income__c d : aUpdated.incomes__r){
            
            if(d.id == aUpdated.incomes__r[0].id){
                system.assertEquals('Current',d.Status__c);
                system.assertEquals(date.newInstance(2017,1,1),d.start_of_pay_period__c);
            } else {
                system.assertEquals('Past',d.Status__c);
            }
        }
        
    }
    
    public static testMethod void testIncome(){
        
        createHouseholdAndIncome(200,10);
        
        list<income__c> dListToModify = new list<income__c>();
        
        for(income__c d : [select id, start_of_pay_period__c, status__c from income__c where status__c = 'Current']){
            system.assertEquals(date.newInstance(2017,10,1),d.start_of_pay_period__c);
            income__c dToUpdate = new income__c(
                id = d.id,
              start_of_pay_period__c = date.newInstance(2017,8,15)
            );
            dListToModify.add(dToUpdate);
        }
        
        checkRecursive.reset();
        update dListToModify;
        
        list<income__c> dListToDelete = new list<income__c>();
        
        for(income__c d : [select id, start_of_pay_period__c, status__c from income__c where status__c = 'Current']){
            system.assertEquals(date.newInstance(2017,9,1),d.start_of_pay_period__c);
            dListToDelete.add(d);
        }
        
        delete dListToDelete;
        
        for(income__c d : [select id, start_of_pay_period__c, status__c from income__c where status__c = 'Current']){
            system.assertEquals(date.newInstance(2017,8,15),d.start_of_pay_period__c);
        }
        
    }

}