+ Start a Discussion
Phuc Nguyen 18Phuc Nguyen 18 

Test class with Aggregate and mapping

Hello All,
Wil someone assist me on how to get test coverage for my class?  No coverage at all for 
if( Trigger.isInsert){
        List<String> zips = new List<String>();

        for (Site__c site :(List<Site__c >) trigger.New)
        {
            zips.add(site.Zip_Code__c);
        }
              List<AggregateResult> zipcodes = [SELECT count(Id) zipids,Zip_Code__c zips FROM Site__c where Zip_Code__c in : zips
              GROUP BY Zip_Code__c];
            
                for (Site__c newsite : (List<Site__c> ) trigger.New){

                string fieldtype = newsite.Site_Type__c == 'Field' ? 'F' : 'R';
                
                Map<String,Integer> mapzips = new Map<String,Integer>();

                for(AggregateResult aggResult : zipcodes)
                    {
                        mapzips.put((String)aggResult.get('zips'), (Integer)aggResult.get('zipids'));
                    }
                
                if(mapzips.size()>0){ 
                    for(AggregateResult result :zipcodes)
                        {
                            string sitename = fieldtype;
                            string postal  = newsite.Zip_Code__c;
                            integer sitenum = ((integer)result.get('zipids')) +1;
                            newsite.Name = fieldtype +'_'+ newsite.Zip_Code__c +'_'+ sitenum;  
                        }
                    }else
                        {
                            string sitename = fieldtype;
                            string postal  = newsite.Zip_Code__c;
                            integer sitenum = 1;
                            newsite.Name = fieldtype +'_'+ newsite.Zip_Code__c +'_'+ sitenum; 
                        }
                }
        }
 
static testMethod void TestBasicTriggerFunctions(){
       Site__c site = new Site__c(
                Name = 'Test Site',
               Type__c = 'COW',
               Site_Status__c = 'Archived'

        );
        insert site;

       Site__c site2 = new Site__c(
            Name = 'Test Site2',
           Site_Type__c = 'COW',
           Site_Status__c = 'Select One'

            );
        insert site2;

        List<Site__c> sites = [ SELECT Id,Site_Type__c,Name FROM Site__c ];
        System.assertEquals( 2, sites.size() );

        site.Site_Status__c = 'Backlog';
        update site;

        System.assertEquals( 'Backlog', [ SELECT Id, Site_Status__c FROM Site__c WHERE Id =: site.Id LIMIT 1 ].Site_Status__c );

        site.Site_Status__c = 'Active';
        update site;
        
        System.assertEquals( 'Active', [ SELECT Id, Site_Status__c FROM Site__c WHERE Id =: site.Id LIMIT 1 ].Site_Status__c );

        delete site;
        
        System.assertEquals( 0, [ SELECT COUNT() FROM Site__c WHERE Id =: site.Id ] );
    }
}

Thank you,
P
Best Answer chosen by Phuc Nguyen 18
AnudeepAnudeep (Salesforce Developers) 
I suggest populating the map in your test class

See this post for sample code