Skip to content

Commit

Permalink
refactor(unittests): changed to using freezegun
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandrusoare committed Jan 20, 2025
1 parent a421455 commit db007a3
Showing 1 changed file with 22 additions and 47 deletions.
69 changes: 22 additions & 47 deletions tests/unit_tests/utils/date_parser_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
# under the License.
import re
from datetime import date, datetime, timedelta
from functools import partial
from typing import Optional
from unittest.mock import Mock, patch

import freezegun
import pytest
from dateutil.relativedelta import relativedelta

Expand Down Expand Up @@ -76,20 +76,6 @@ def mock_parse_human_datetime(s: str) -> Optional[datetime]: # noqa: C901
return None


def mock_today(s: str, today: datetime) -> datetime:
"""
Mock implementation of parse_human_datetime, only handles 'today'.
:param s: The human-readable date string (e.g., "today").
:param today: The datetime value to use as "today".
:return: The mocked or actual datetime for 'today'.
"""
if s == "today":
return today.replace(hour=0, minute=0, second=0, microsecond=0)
else:
raise ValueError(f"Unrecognized date string: {s}")


@patch("superset.utils.date_parser.parse_human_datetime", mock_parse_human_datetime)
def test_get_since_until() -> None:
result: tuple[Optional[datetime], Optional[datetime]]
Expand Down Expand Up @@ -291,42 +277,31 @@ def test_get_since_until_instant_time_comparison_enabled() -> None:
assert result == expected


@patch("superset.utils.date_parser.parse_human_datetime")
def test_previous_calendar_quarter(mock_parse):
today = datetime(2023, 1, 15)
mock_parse.side_effect = partial(mock_today, today=today)

result = get_since_until("previous calendar quarter")
expected = (datetime(2022, 10, 1), datetime(2023, 1, 1))
assert result == expected

today = datetime(2023, 4, 15)
mock_parse.side_effect = partial(mock_today, today=today)

result = get_since_until("previous calendar quarter")
expected = (datetime(2023, 1, 1), datetime(2023, 4, 1))
assert result == expected
def test_previous_calendar_quarter():
with freezegun.freeze_time("2023-01-15"):
result = get_since_until("previous calendar quarter")
expected = (datetime(2022, 10, 1), datetime(2023, 1, 1))
assert result == expected

today = datetime(2023, 8, 15)
mock_parse.side_effect = partial(mock_today, today=today)
with freezegun.freeze_time("2023, 4, 15"):
result = get_since_until("previous calendar quarter")
expected = (datetime(2023, 1, 1), datetime(2023, 4, 1))
assert result == expected

result = get_since_until("previous calendar quarter")
expected = (datetime(2023, 4, 1), datetime(2023, 7, 1))
assert result == expected
with freezegun.freeze_time("2023, 8, 15"):
result = get_since_until("previous calendar quarter")
expected = (datetime(2023, 4, 1), datetime(2023, 7, 1))
assert result == expected

today = datetime(2023, 10, 15)
mock_parse.side_effect = partial(mock_today, today=today)
with freezegun.freeze_time("2023, 10, 15"):
result = get_since_until("previous calendar quarter")
expected = (datetime(2023, 7, 1), datetime(2023, 10, 1))
assert result == expected

result = get_since_until("previous calendar quarter")
expected = (datetime(2023, 7, 1), datetime(2023, 10, 1))
assert result == expected

today = datetime(2024, 1, 1)
mock_parse.side_effect = partial(mock_today, today=today)

result = get_since_until("previous calendar quarter")
expected = (datetime(2023, 10, 1), datetime(2024, 1, 1))
assert result == expected
with freezegun.freeze_time("2024, 1, 1"):
result = get_since_until("previous calendar quarter")
expected = (datetime(2023, 10, 1), datetime(2024, 1, 1))
assert result == expected


@patch("superset.utils.date_parser.parse_human_datetime", mock_parse_human_datetime)
Expand Down

0 comments on commit db007a3

Please sign in to comment.