Skip to content

Commit

Permalink
Add in unit test for Truth and reconciliation day, change holiday cod…
Browse files Browse the repository at this point in the history
…e a bit (#1337)
  • Loading branch information
seeker25 authored Dec 1, 2023
1 parent 3353dcd commit 56d2159
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
23 changes: 15 additions & 8 deletions pay-api/src/pay_api/utils/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,23 +198,30 @@ def get_nearest_business_day(date_val: datetime, include_today: bool = True) ->
"""
if not include_today:
date_val = get_next_day(date_val)
if not is_holiday(date_val):
return date_val
# just a recursive call to get the next business day.
return get_nearest_business_day(get_next_day(date_val))
if is_holiday(date_val):
days_to_add = 1
# If it's saturday holiday add 3 days to Tuesday
if date_val.weekday() == 5:
days_to_add = 3
# If it's sunday holiday add 2 days to Tuesday
if date_val.weekday() == 6:
days_to_add = 2
return get_nearest_business_day(date_val + timedelta(days=days_to_add))
# If it's the weekend, get the next day.
if date_val.weekday() >= 5:
return get_nearest_business_day(get_next_day(date_val))
return date_val


def is_holiday(val: datetime) -> bool:
"""Return receipt number for payments.
saturday or sunday check
check the BC holidays
"""
# Even though not officially a BC STAT - Union recognizes Easter Monday and Boxing Day.
# holidays version 0.23 works with Boxing day, but the newer versions don't.
if holidays.CA(subdiv='BC', observed=True).get(val.strftime('%Y-%m-%d')):
return True
if val.weekday() >= 5:
return True
return False


Expand All @@ -232,7 +239,7 @@ def get_outstanding_txns_from_date() -> datetime:
counter: int = 0
while counter < days_interval:
from_date = from_date - timedelta(days=1)
if not is_holiday(from_date):
if not (is_holiday(from_date) and from_date.weekday() >= 5):
counter += 1
return from_date

Expand Down
20 changes: 20 additions & 0 deletions pay-api/tests/unit/utils/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,26 @@ def test_next_business_day(session):
business_date = get_nearest_business_day(d)
assert business_date.date() == datetime(2025, 10, 1).date()

# Truth and reconciliation day: Saturday Sept 30, 2023, Holiday observed on 2nd
d = datetime(2023, 9, 30)
business_date = get_nearest_business_day(d)
assert business_date.date() == datetime(2023, 10, 3).date()

# Weekday check - Friday December 1st, 2023
d = datetime(2023, 12, 1)
business_date = get_nearest_business_day(d)
assert business_date.date() == datetime(2023, 12, 1).date()

# Weekend check - Saturday December 2nd, 2023
d = datetime(2023, 12, 2)
business_date = get_nearest_business_day(d)
assert business_date.date() == datetime(2023, 12, 4).date()

# Weekend check - Sunday December 3rd, 2023
d = datetime(2023, 12, 3)
business_date = get_nearest_business_day(d)
assert business_date.date() == datetime(2023, 12, 4).date()


def test_print_holidays():
"""Print holidays, can be used to take a quick peak at the holidays."""
Expand Down

0 comments on commit 56d2159

Please sign in to comment.