diff --git a/uk_bin_collection/tests/test_common_functions.py b/uk_bin_collection/tests/test_common_functions.py index bba11819a7..2e009e97e6 100644 --- a/uk_bin_collection/tests/test_common_functions.py +++ b/uk_bin_collection/tests/test_common_functions.py @@ -134,6 +134,32 @@ def test_is_holiday_different_region(mock_holidays_func): assert is_holiday(datetime(2023, 11, 30), Region.ENG) is False +def test_is_weekend_when_true(): + weekend_date = datetime(2024, 12, 7) + assert is_weekend(weekend_date) is True + + +def test_is_weekend_when_false(): + weekend_date = datetime(2024, 12, 6) + assert is_weekend(weekend_date) is False + + +def test_is_working_day_when_true(): + working_day_date = datetime(2024, 12, 6) + assert is_working_day(working_day_date) is True + + +def test_is_working_day_when_false(): + working_day_date = datetime(2024, 12, 7) + assert is_working_day(working_day_date) is False + + +def test_get_next_working_day(): + sample_date = datetime(2024, 12, 7) + next_working_day = get_next_working_day(sample_date) + assert next_working_day == datetime(2024, 12, 9) + + def test_remove_alpha_characters(): test_string = "12345abc12345" result = remove_alpha_characters(test_string) diff --git a/uk_bin_collection/uk_bin_collection/common.py b/uk_bin_collection/uk_bin_collection/common.py index 37c5092abc..024ae9655a 100644 --- a/uk_bin_collection/uk_bin_collection/common.py +++ b/uk_bin_collection/uk_bin_collection/common.py @@ -10,7 +10,6 @@ import requests from dateutil.parser import parse from selenium import webdriver -from selenium.common.exceptions import WebDriverException from selenium.webdriver.chrome.service import Service as ChromeService from urllib3.exceptions import MaxRetryError from webdriver_manager.chrome import ChromeDriverManager @@ -162,6 +161,31 @@ def is_holiday(date_to_check: datetime, region: Region = Region.ENG) -> bool: return False +def is_weekend(date_to_check: datetime) -> bool: + """ + Checks if a given date is a weekend + :param date_to_check: Date to check if it falls on a weekend + :return: Bool - true if a weekend day, false if not + """ + return True if date_to_check.date().weekday() >= 5 else False + + +def is_working_day(date_to_check: datetime, region: Region = Region.ENG) -> bool: + """ + Wraps is_holiday() and is_weekend() into one function + :param date_to_check: Date to check if holiday + :param region: The UK nation to check. Defaults to ENG. + :return: Bool - true if a working day (non-holiday, Mon-Fri). + """ + return False if is_holiday(date_to_check, region) or is_weekend(date_to_check) else True + + +def get_next_working_day(date: datetime, region: Region = Region.ENG) -> datetime: + while not is_working_day(date, region): + date += timedelta(days=1) + return date + + def get_weekday_dates_in_period(start: datetime, day_of_week: int, amount=8) -> list: """ Returns a list of dates of a given weekday from a start date for the given amount of weeks @@ -208,7 +232,7 @@ def get_next_occurrence_from_day_month(date: datetime) -> datetime: # Check if the target date has already occurred this year if (target_month < current_month) or ( - target_month == current_month and target_day < current_day + target_month == current_month and target_day < current_day ): date = pd.to_datetime(date) + pd.DateOffset(years=1) @@ -291,10 +315,10 @@ def contains_date(string, fuzzy=False) -> bool: def create_webdriver( - web_driver: str = None, - headless: bool = True, - user_agent: str = None, - session_name: str = None, + web_driver: str = None, + headless: bool = True, + user_agent: str = None, + session_name: str = None, ) -> webdriver.Chrome: """ Create and return a Chrome WebDriver configured for optional headless operation.