Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions tests/test_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,25 @@ def test_parse_and_format_date():
"2020Q1:2021Q4",
id="two strings",
),
pytest.param(
["2020M01", "2021M12"],
"Q",
"2020Q1:2021Q4",
id="two strings in a list",
),
],
)
def test_format_dates(dates_, freq, expected):
assert dates.format_dates(dates_, freq) == expected


@pytest.mark.parametrize(
["dates_"],
(
(1234,),
(["Why", "Hello", "There"],),
),
)
def test_bad_dates(dates_):
with pytest.raises(ValueError, match="dates argument"):
dates.format_dates(dates_, "Y")
9 changes: 7 additions & 2 deletions wbdata/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,14 @@ def format_dates(dates: Dates, freq: str) -> str:
A string representing a date or date range according to the specified
frequency in the form the World Bank API expects.
"""
if isinstance(dates, tuple):
if isinstance(dates, (str, dt.datetime)):
return _parse_and_format_date(dates, freq)
if isinstance(dates, Sequence) and len(dates) == 2:
return (
f"{_parse_and_format_date(dates[0], freq)}"
f":{_parse_and_format_date(dates[1], freq)}"
)
return _parse_and_format_date(dates, freq)
raise ValueError(
"dates argument must be a string, datetime object, or 2-tuple of"
" strings or datetime objects."
)