Skip to content

Commit

Permalink
test: Add tests for all date options
Browse files Browse the repository at this point in the history
  • Loading branch information
jordan-gillard authored Jul 16, 2024
1 parent 0391bf6 commit bb850ec
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
21 changes: 20 additions & 1 deletion edgar_tool/url_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ def __init__(self, **query_args: SearchQueryKwargs):
"You must provide both a start and end date if searching a custom date range."
)
)
elif date_range_select and date_range_select not in {"all", "10y", "1y", "30d"}:
elif date_range_select and date_range_select not in {
"all",
"10y",
"1y",
"30d",
"custom",
}:
raise ValueError(
(
"Invalid date_range_select. "
Expand Down Expand Up @@ -68,6 +74,19 @@ def generate_search_url_for_kwargs(search_kwargs: SearchQueryKwargs) -> str:
query_params = {
"q": validated_params.keywords,
}
if date_range_select := validated_params.date_range_select:
query_params.update(
{
"dateRange": date_range_select,
}
)
if date_range_select == "custom":
query_params.update(
{
"startdt": validated_params.start_date.strftime("%Y-%m-%d"),
"enddt": validated_params.end_date.strftime("%Y-%m-%d"),
}
)
encoded_params = parse.urlencode(
query_params, doseq=True, encoding="utf-8", quote_via=parse.quote
)
Expand Down
31 changes: 31 additions & 0 deletions tests/test_url_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,34 @@ def test_should_raise_if_date_range_select_invalid():
# WHEN / THEN
with pytest.raises(ValueError, match=expected_error_msg):
url_generator.generate_search_url_for_kwargs(test_kwargs)


@pytest.mark.parametrize(
"date_kwargs,url_ending",
[
(
{
"date_range_select": "custom",
"start_date": datetime.date.fromisoformat("2024-07-10"),
"end_date": datetime.date.fromisoformat("2024-07-15"),
},
"&dateRange=custom&startdt=2024-07-10&enddt=2024-07-15",
),
({"date_range_select": "all"}, "&dateRange=all"),
({"date_range_select": "10y"}, "&dateRange=10y"),
({"date_range_select": "1y"}, "&dateRange=1y"),
({"date_range_select": "30d"}, "&dateRange=30d"),
],
)
def test_generates_correct_url_for_date(date_kwargs, url_ending):
# GIVEN
expected_url = (
f"https://www.sec.gov/edgar/search/#/q=%22Ford%20Motor%20Co%22{url_ending}"
)
test_kwargs = {**{"keywords": ["Ford Motor Co"]}, **date_kwargs}

# WHEN
actual_url = url_generator.generate_search_url_for_kwargs(test_kwargs)

# THEN
assert actual_url == expected_url

0 comments on commit bb850ec

Please sign in to comment.