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
Somasundaram S 1Somasundaram S 1 

test class covered 82% but...

HI All
       could you please advise me about test case i wrote for the  the below test class and when i run it shows it covered 82% but am not convinced, the trigger code has after update,after insert, after delete and after undelte , in all i just getting the id of child and parent then i am collectiing child record then getting sum and then updateing the parent field. 
in below test class i just inserted what are the record need for the code but it shows 82% code coverage , for prod move this is fine but my query is how come it covered 82& i didnt do anything for after update after delete undelete and also didnt get sum and update 
could you please advise me
 
trigger quantityleft on Sample_Transaction__c ( after INSERT, after UPDATE, after DELETE, after UNDELETE ) {
   List<Sample_Lot__c> list_lot= new List<Sample_Lot__c>();
     Set<Id> set_transaction = new Set<Id>();
     Decimal Sum = 0;
      if( Trigger.isInsert || Trigger.isUndelete ){
        for( Sample_Transaction__c objOpp: Trigger.new ) {
            if( objOpp.Completed_Transaction__c == TRUE ){
                set_transaction.add( objOpp.Parent_id__c );
            }
        }
    }
 

    if( Trigger.isUpdate ) {

        for( Sample_Transaction__c objOpp: Trigger.new ) {

            if( objOpp.Completed_Transaction__c == TRUE ) {

                set_transaction.add( objOpp.Parent_id__c );

            }

        }

    }
 

    if( Trigger.isdelete ) {

       for( Sample_Transaction__c objOpp: Trigger.old ) {

            if( objOpp.Completed_Transaction__c == TRUE ) {

                set_transaction.add( objOpp.Parent_id__c );
            }
          }
    }
    List<Sample_Transaction__c> sampleTransactionRecords = new List<Sample_Transaction__c>();
    sampleTransactionRecords = [
        SELECT  Id
                ,Sample_Lot_ID__c

                ,Transaction_Quantity__c,
                Completed_Transaction__c

        FROM    Sample_Transaction__c

        WHERE   Parent_id__c = :set_transaction

    ];
    List<String> sampleLotIds = new List<String>();
    for( Sample_Transaction__c sampleTransaction : sampleTransactionRecords ) {
        sampleLotIds.add( sampleTransaction.Sample_Lot_ID__c );
    }
    List<Sample_Lot__c> sampleLotRecords = new List<Sample_Lot__c>();
    sampleLotRecords = [
        SELECT  Id
                ,Name
                ,Quantity_Left__c
        FROM    Sample_Lot__c
        WHERE   Id IN :set_transaction

    ];


    Map<String,Sample_Lot__c> mapOfSampleLots = new Map<String,Sample_Lot__c>();

    for( Sample_Lot__c sampleLot : sampleLotRecords ) {

        mapOfSampleLots.put( sampleLot.Id, sampleLot );

    }
    

    if( !set_transaction.isEmpty() ) {

        List<Sample_Lot__c> sampleLotsToUpdate = new List<Sample_Lot__c>();

        
        for( Sample_Transaction__c sample_transaction : sampleTransactionRecords ) {
         if( sample_transaction.Completed_Transaction__c == TRUE ) {

            Sum += sample_transaction.Transaction_Quantity__c;

 
}
        }
}
 for( Sample_Lot__c samplelots : sampleLotRecords ) {

         samplelots.Quantity_left__c=Sum;


    }


        UPDATE sampleLotRecords;

    

}

test class

 
@isTest 
public class testquantityleft{
static testmethod void triggerTest(){
// create account
Account accountOb= new Account();
accountOb.Firstname='name';
accountOb.LastName='1';
accountOb.Credentials__c='MD';
accountOb.SLN__c='123';
accountOb.Specialty_1__c='heart';
Map <String,Schema.RecordTypeInfo> recordTypeMap = Account.sObjectType.getDescribe().getRecordTypeInfosByName();
//Account accountObj = new Account();
//accountObj.name = 'Test Record Type';
if(recordTypeMap.containsKey('Reckitt Person Account')) {
 accountOb.RecordTypeId= recordTypeMap.get('Reckitt Person Account').getRecordTypeId();
}
insert accountOb;
// create address
Address__c address = new Address__c();
address.Account__c = accountOb.id;
//Map <String,Schema.RecordTypeInfo> recordTypeMap1 = Address__c.sObjectType.getDescribe().getRecordTypeInfosByName();
//if(recordTypeMap1.containsKey('Reckitt Address')) {
 //address .RecordTypeId= recordTypeMap.get('Reckitt Address').getRecordTypeId();
//}
address.Name='somaaddress';
insert address;

// create product
Product__c prod = new Product__c();
prod.name ='test';
insert prod;
Sample_Lot__c lot=new Sample_Lot__c();
lot.Name='test lot';
lot.Product_ID__c=prod.id;
lot.Confirmed_Quantity__c=600;
lot.Quantity_Left__c=0;
insert lot;

// create sample transaction

Sample_Transaction__c stransac = new Sample_Transaction__c();
stransac.Account_ID__c = accountob.id;
stransac .Address_ID__c=address.id;
stransac .Quantity__c =100;
stransac .Product__c=prod.id;
stransac .Sample_Lot_ID__c=lot.id;
insert stransac ;
Sample_Transaction__c stransac1 = new Sample_Transaction__c();
stransac1.Account_ID__c = accountob.id;
stransac1 .Address_ID__c=address.id;
stransac1 .Quantity__c =100;
stransac1 .Product__c=prod.id;
stransac1 .Sample_Lot_ID__c=lot.id;
Map <String,Schema.RecordTypeInfo> recordTypeMap2 = Sample_Transaction__c.sObjectType.getDescribe().getRecordTypeInfosByName();
if(recordTypeMap2.containsKey('Reckitt Receipt')) {
 stransac1 .RecordTypeId= recordTypeMap2.get('Reckitt Receipt').getRecordTypeId();
}





insert stransac1 ;





}
}

Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Somasundaram, I hope it will be helpful.

Please mark it as best answer if the information is informative.

Best Regards
Rahul Kumar