I have been creating a Business Hours Calculation engine in the past few weeks and have gotten some great help on a few questions I've already asked (BH in Apex, BH and Holiday Affiliation).
I have my code working exaclty how I want it and now have moved on to test code. As Bob Buzzard Pointed out, although it's not well documented, Business Hour Data as well as Holidays are available in Test Code Context.
@isTest
private class BusinessHoursMathTest {
static testmethod void SimpleTest(){
List<BusinessHours> bhs = [select id from BusinessHours where IsDefault=true];
System.assert(bhs.size() == 1);
List<Holiday> holidays = [Select Id From Holiday];
system.assert(!holidays.isEmpty());
List<Account> accs = [select id from Account];
System.assert(accs.isEmpty());
}
}
The issue is there is no connection between the two. I cannot tell if a Holiday I query for is affiliated with a set of Business Hours. Ideally, in a perfect world I would be able to create a set of business hours, insert them, create a holiday, affiliated with those business hours and insert it. Easy, except DML is not allowed on business hours. I can insert holidays, but there is no way to affiliate it with the Business Hours I have queried for.
So I would think that it would be as simple as Querying for Default business Hours, creating a new Holiday, and associating that Holiday with the Business Hours. Something like this
BusinessHours defaultBH = [Select Id From BusinessHours Where IsDefault = True];
Holiday hol = new Holiday();
hol.Name='Test holiday';
hol.activitydate = System.Today();
insert hol;
The holiday is inserted, but there is no affiliation of that Holiday with the Business Hours. I have found that there is no way to affiliate the Holiday with the business hours with Apex. There are 2 major problems here.
- I am 100% reliant on existing business hours in the org if I cannot create or update Business Hours. This goes against all testing best practices I know. Makes it close to impossible to properly test my code with assertions.
- Even though I can create holidays, the fact that I can't affiliate them with business hours makes it fairly useless.
There was already an Idea on the Idea exchange around exposing the affiliation between Holidays and Business Hours in Apex seen here. I also created an Idea asking to allow DML on Business Hours seen here
MY QUESTION
Does anyone know of a workaround for this. How can I properly test business hour logic without being able to create holidays or create/edit Business Hours in the test class.
As it is now I am 100% reliant on existing data in the org to test my business hour calculations, which prevents me from fully testing all possibilities and edge cases.
Has anyone come up with a way to do this or can you let me know if I have missed anything or not going about this the proper way. I'm pulling my hair out with this one.