While analyzing the faild test cases, I started to analyze the code implementation. So i spotted some interesting facts:
- The key used for the
Availability
Object, is the day of week, which will make imposible to query availability for more than 7 days (in current implementation). Because if we query the 8th day, then it will set the availability on the 1st day slot.- The function that is itterationg through events and poopulates the
Availability
Object works in a wrong way. When an event of kindappointment
is queryed then it filters already insertedopening hours
, so if anappointment
event is inserted beforeopening
event (like in the test case 2) then it will try to filter an empty array ofopening hours
, and then will process theopening
event, that will populate the availability with allopening hours
.
First of all I decided that I need to create an Object that will include all the dates with events, including reccuring events for the entire required period. The key will be the entire date string of format
D-MM-YYYY
this will allow us to fetch availability for years ahead.weekly_reccuring
means that event is repeating each 7 days. Each day is an Object will contain multiple keys that represents the kind of event, in our case there are only 2 kindsopening
andappointment
. Now after we already havedaysWithEvents
Object, we can set theavailability
for the requested time frame. I am setting availbaility by subtracting allappointment
hours fromopening
hours for each day that has events.
First I choose to test if function returns
availabiliy
for any ammount off days required. Afterwords I choose to test ifweekly_recurring
events are working fine. Third itteration of ftest was for checking how the function will behave with mixed events. There is an use case when I added an event with opening hours13 - 18
, and then added one more event, on the same date, but with opening hours8 - 11
. This test failed! - because the order of hours didn't match in theavailability
Object. In order to fix that I decided toorder
the query from database bystarts_at
field. This solution fixed thegetAvailability
function.
Please checkout https://vladutzik.github.io/doctolib-api-test
- Write tests to check if reccuring is working correct for
appointment
events.