Skip to content

Commit

Permalink
fix(utils): evaluate date parser multiple holiday results correctly (a…
Browse files Browse the repository at this point in the history
  • Loading branch information
arkid15r authored and sebastianliebscher committed Apr 28, 2023
1 parent 77a22ff commit 3c2e3e3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
3 changes: 1 addition & 2 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ hashids==1.3.1
# via apache-superset
hijri-converter==2.2.4
# via holidays
holidays==0.17.2
holidays==0.23
# via apache-superset
humanize==3.11.0
# via apache-superset
Expand Down Expand Up @@ -298,7 +298,6 @@ typing-extensions==4.4.0
# apache-superset
# flask-limiter
# limits
# rich
urllib3==1.26.6
# via selenium
vine==5.0.0
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def get_git_sha() -> str:
"graphlib-backport",
"gunicorn>=20.1.0; sys_platform != 'win32'",
"hashids>=1.3.1, <2",
"holidays>=0.17.2, <0.18",
"holidays>=0.23, <0.24",
"humanize",
"isodate",
"markdown>=3.0",
Expand Down
4 changes: 2 additions & 2 deletions superset/utils/date_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,8 @@ def eval(self) -> datetime:
country = country.eval() if country else "US"

holiday_lookup = country_holidays(country, years=[holiday_year], observed=False)
searched_result = holiday_lookup.get_named(holiday)
if len(searched_result) == 1:
searched_result = holiday_lookup.get_named(holiday, lookup="istartswith")
if len(searched_result) > 0:
return dttm_from_timetuple(searched_result[0].timetuple())
raise ValueError(
_("Unable to find such a holiday: [%(holiday)s]", holiday=holiday)
Expand Down
22 changes: 22 additions & 0 deletions tests/unit_tests/utils/date_parser_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@ def mock_parse_human_datetime(s: str) -> Optional[datetime]:
return datetime(2017, 4, 7)
elif s in ["5 days", "5 days ago"]:
return datetime(2016, 11, 2)
elif s == "2000-01-01T00:00:00":
return datetime(2000, 1, 1)
elif s == "2018-01-01T00:00:00":
return datetime(2018, 1, 1)
elif s == "2018-12-31T23:59:59":
return datetime(2018, 12, 31, 23, 59, 59)
elif s == "2022-01-01T00:00:00":
return datetime(2022, 1, 1)
else:
return None

Expand Down Expand Up @@ -260,12 +264,30 @@ def test_datetime_eval() -> None:
expected = datetime(2018, 9, 3, 0, 0, 0)
assert result == expected

result = datetime_eval(
"holiday('Eid al-Fitr', datetime('2000-01-01T00:00:00'), 'SA')"
)
expected = datetime(2000, 1, 8, 0, 0, 0)
assert result == expected

result = datetime_eval(
"holiday('Boxing day', datetime('2018-01-01T00:00:00'), 'UK')"
)
expected = datetime(2018, 12, 26, 0, 0, 0)
assert result == expected

result = datetime_eval(
"holiday('Juneteenth', datetime('2022-01-01T00:00:00'), 'US')"
)
expected = datetime(2022, 6, 19, 0, 0, 0)
assert result == expected

result = datetime_eval(
"holiday('Independence Day', datetime('2022-01-01T00:00:00'), 'US')"
)
expected = datetime(2022, 7, 4, 0, 0, 0)
assert result == expected

result = datetime_eval(
"lastday(dateadd(datetime('2018-01-01T00:00:00'), 1, month), month)"
)
Expand Down

0 comments on commit 3c2e3e3

Please sign in to comment.