In the below unit test my debug line
System.debug('+++ Number of Queries used in this apex code 2.1 so far: ' + Limits.getQueries());
shows that the number of queries after my single query, in a new method, is 100. My understanding was that each method is has it's own set of limits (although I can't find the documentation to confirm this), in which case shouldn't the number of queries should be 1?
This is causing me to hit the error
System.LimitException: Too many SOQL queries: 101
so I'm unable to retrieve the updated records & assert the results of my test.
@isTest
public class WE_MRFv3Test {
@testSetup
static void dataSetup() {
User u = WE_TestDataUtility.createStandardUser();
System.runas(u) {
VApexC__c acCS = WE_TestDataUtility.createApexControllerCustSett(
'Opportunity MRF', // fieldRef
false); // fcstDisabledStatus
acCS.BudgetManagerDisabled__c = true;
insert acCS;
VProfileId__c profCS = WE_TestDataUtility.createProfileIdCustSett(
'User', // locationRef
'System Admin User', // fieldRef
true); // settingStatus
profCS.Team_Name__c = 'Virtual Admin';
insert profCS;
VRTN__c rtCS = WE_TestDataUtility.createRecTypeNameCustSett(
'CP Virtual Prepaid MC', // rtName
'NAEU Opps'); // csField
rtCS.Product_Name__c = 'Virtual Prepaid MasterCard';
insert rtCS;
List<Target__c> targets = WE_TestDataUtility.createTargets(
2, // noMonths
u.Id, // salespersonId
date.today().addMonths(1).year(), // firstYear
date.today().addMonths(1).month(), // firstMonth
'Virtual Sales EU', // team
'Close Date', // dateType
100000); // target
insert targets;
List<Budget__c> budgets = new List<Budget__c>();
for(integer i = 0; i < 24; i++) {
budgets.add(new Budget__c(
Name = 'Test Budget ' + i,
Date__c = date.today().addMonths(i),
Team__c = 'Virtual Admin',
Product__c = 'Virtual Prepaid MasterCard',
Budget__c = 1));
}
insert budgets;
List<Opportunity> oppsToInsert = new List<Opportunity>();
List<Opportunity> opportunities0 = WE_TestDataUtility.createOpportunities(
'EU Opps', // oppRef
1, // noOpps
'CP Virtual Prepaid MC', // recordTypeName
'id0', // identifier
'1) Suspect', // stageName
date.today().addmonths(1), // closeDate
'Standard', // rampProfile
100.00); // settlement
insert opportunities0;
acCS.BudgetManagerDisabled__c = false;
update acCS;
List<Opportunity> opportunities1 = WE_TestDataUtility.createOpportunities(
'EU Opps', // oppRef
1, // noOpps
'CP Virtual Prepaid MC', // recordTypeName
'id1', // identifier
'1) Suspect', // stageName
date.today().addmonths(1), // closeDate
'Standard', // rampProfile
100.00); // settlement
oppsToInsert.addAll(opportunities1);
List<Opportunity> cappedFcstDurOpps = WE_TestDataUtility.createOpportunities(
'EU Opps', // oppRef
1, // noOpps
'CP Virtual Prepaid MC', // recordTypeName
'id2', // identifier
'1) Suspect', // stageName
date.today().addMonths(1), // closeDate
'Standard', // rampProfile
100.00); // settlement
for(Opportunity o : cappedFcstDurOpps) {
o.FSR_End_Date__c = date.today().addMonths(9);
oppsToInsert.add(o);
}
insert oppsToInsert;
}
}
static testMethod void testMrfUpdates2() {
List<Opportunity> opportunities1 = [SELECT Id, CloseDate FROM Opportunity WHERE Name LIKE '%Id1%'];
System.debug('+++ Number of Queries used in this apex code 2.1 so far: ' + Limits.getQueries());
test.startTest();
for(Opportunity upOppsFcstAdj : opportunities1) {
upOppsFcstAdj.Forecast_Adjustment__c = 10;
}
update opportunities1;
for(Opportunity upOppsRampProfile : opportunities1) {
upOppsRampProfile.FSR_End_Date__c = upOppsRampProfile.CloseDate.addMonths(6);
}
update opportunities1;
test.stopTest();
System.debug('+++ Number of Queries used in this apex code 2.2 so far: ' + Limits.getQueries());
// too many queries error on next line
List<Monthly_Revenue_Forecast__c > mrfRecs1 = [SELECT Id, Budget__c
FROM Monthly_Revenue_Forecast__c
WHERE Opportunity__c IN :opportunities1];
// 1 is noOpps
system.assertEquals(1*3, mrfRecs1.size(), 'there should be 3 MRF records for each Opportunity, as the forecast duration is capped');
system.assert(mrfRecs1[0].Budget__c != null, 'budget should have been matched with MRF record');
}
}