From 7f82f4751717529b8a3566c794cf6cd9fb6f1fbf Mon Sep 17 00:00:00 2001 From: malgorzatagwinner Date: Thu, 31 Oct 2024 12:26:04 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Add=20tests=20for=20the=20DynamicDa?= =?UTF-8?q?teHandler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../unit/orchestration/prefect/test_utils.py | 136 ++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 tests/unit/orchestration/prefect/test_utils.py diff --git a/tests/unit/orchestration/prefect/test_utils.py b/tests/unit/orchestration/prefect/test_utils.py new file mode 100644 index 000000000..b67fd5295 --- /dev/null +++ b/tests/unit/orchestration/prefect/test_utils.py @@ -0,0 +1,136 @@ +import pendulum +import pytest + +from viadot.utils import skip_test_on_missing_extra + + +try: + from viadot.orchestration.prefect.utils import DynamicDateHandler +except ImportError: + skip_test_on_missing_extra( + source_name="DynamicDateHandler", extra="dynamicDateHandler" + ) + +ddh1 = DynamicDateHandler( + ["<<", ">>"], dynamic_date_format="%Y%m%d", dynamic_date_timezone="Europe/Warsaw" +) + +ddh2 = DynamicDateHandler( + ["[[", "]]"], dynamic_date_format="%Y%m%d", dynamic_date_timezone="Europe/Warsaw" +) + + +@pytest.fixture +def setup_dates(): + """Fixture to provide the current dates for comparison in tests.""" + today = pendulum.today("Europe/Warsaw") + yesterday = pendulum.yesterday("Europe/Warsaw") + last_month = today.subtract(months=1).month + last_year = today.subtract(years=1) + last_day_prev_month = today.subtract(months=1).end_of("month") + now_time = pendulum.now("Europe/Warsaw") + return { + "today": today.strftime("%Y%m%d"), + "yesterday": yesterday.strftime("%Y%m%d"), + "current_month": today.strftime("%m"), + "last_month": f"{last_month:02d}", + "current_year": today.strftime("%Y"), + "last_year": last_year.strftime("%Y"), + "last_day_previous_month": last_day_prev_month.strftime("%Y%m%d"), + "now_time": now_time.strftime("%H%M%S"), + } + + +def test_create_dates(setup_dates): + """Test if create_dates function returns correct dictionary values.""" + keys_to_compare = [ + "today", + "yesterday", + "current_month", + "last_month", + "current_year", + "last_year", + "last_day_previous_month", + ] + assert all(setup_dates[key] == ddh1.replacements[key] for key in keys_to_compare) + + +def test_process_dates_single(setup_dates): + """Test if process_dates function replaces a single date placeholder.""" + text = "Today's date is <>." + replaced_text = ddh1.process_dates(text) + expected_text = f"Today's date is {setup_dates['today']}." + assert replaced_text == expected_text + + +def test_process_dates_multiple(setup_dates): + """Test if process_dates function replaces multiple placeholders.""" + text = "Today is <>, yesterday was <>" + replaced_text = ddh1.process_dates(text) + expected_text = ( + f"Today is {setup_dates['today']}, yesterday was {setup_dates['yesterday']}" + ) + assert replaced_text == expected_text + + +def test_process_dates(setup_dates): + """Test if process_dates function replaces multiple placeholders.""" + text = "The hour and minute are <>" + replaced_text = ddh1.process_dates(text) + expected_text = f"The hour and minute are {setup_dates['now_time'][:-2]}" + replaced_text = replaced_text[:-2] + assert replaced_text == expected_text + + +def test_process_dates_with_custom_symbols(setup_dates): + """Test if process_dates function works with custom start and end symbols.""" + text = "The year is [[current_year]]." + replaced_text = ddh2.process_dates(text) + expected_text = f"The year is {setup_dates['today'][:4]}." + assert replaced_text == expected_text + + +def test_process_dates_with_malformed_keys(): + """Test if process_dates function leaves malformed placeholders unchanged.""" + text = "This is a malformed placeholder: <>" + processed_range = ddh1.process_dates(text) + x_years_ago = pendulum.today().year - (x - 1) + expected_result = [str(x_years_ago + i) for i in range(x)] + assert processed_range == expected_result + + +def test_process_last_x_months(): + """Test if process_dates function returns a date range of last x years.""" + x = 18 + text = f"<>" + processed_range = ddh1.process_dates(text) + start_date = pendulum.today().subtract(months=(x - 1)) + expected_result = [ + start_date.add(months=i).start_of("month").format("YMM") for i in range(x) + ] + assert processed_range == expected_result + + +def test_Y_years_from_x(): + x = 2019 + y = 4 + text = f"<<{y}_years_from_{x}>>" + processed_range = ddh1.process_dates(text) + expected_result = [str(x + i) for i in range(y)] + assert processed_range == expected_result