I am trying to test a sub query using SOQL Lib
In the docs it says will ignore all filters and relations so how is this possible if I cannot create a relationship?
My example is to get all opportunities for an Account who's amount value is between 500 and 2,000
Account Selector
public AccountSelector getOpportunitiesByAmount(Id account_id){
with(SOQL.SubQuery.of('Opportunities')
.with(Opportunity.Id, Opportunity.Amount)
.whereAre(SOQL.FilterGroup
.add(SOQL.Filter.with(Opportunity.Amount).greaterThan(500))
.add(SOQL.Filter.with(Opportunity.Amount).lessThan(2000))
.conditionLogic('1 AND 2')
)
)
.whereAre(Filter.with(Account.Id).equal(account_id));
return this;
}
Controller The debug is to make sure that the expected result works which is does
public static List<Account> getOpportunitiesInAccount(String account_id) {
// making sure it works
List<Account> accList = AccountSelector.query()
.getOpportunitiesByAmount(account_id)
.mockId('AccountController.getOpportunitiesInAccount')
.toList();
Opportunity opps = accList[0].Opportunities;
System.debug(opps.Amount);
return AccountSelector.query()
.getOpportunitiesByAmount(account_id)
.toList();
}
Test
At the moment I am getting the error - System.QueryException: List has no rows for assignment to SObject
@IsTest
static void opportunityForAccountInRange(){
List<Opportunity> opportunity_list = new List<Opportunity>{
new Opportunity(AccountId = '0012o00003ORxRcAAL', Amount = 500),
new Opportunity(AccountId = '0012o00003ORxRcAAL', Amount = 1500),
new Opportunity(AccountId = '0012o00003ORxRcAAL', Amount = 2000)
};
SOQL.setMock('AccountController.getOpportunitiesInAccount', opportunity_list);
Account account_for_test = new Account(Id = '0012o00003ORxRcAAL', Name = 'Test');
SOQL.setMock('AccountController.getOpportunitiesInAccount', account_for_test);
List<Account> result = AccountController.getOpportunitiesInAccount('0012o00003ORxRcAAL');
// Opportunity opps = result[0].Opportunities;
// System.debug(opps.Amount);
Assert.areEqual(1, result.size());
}
I was hoping that if anyone has experience using SOQL Lib if they could help shed some light around testing sub queries.