Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix index out of range error when data not found #438

Merged
merged 14 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 10 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
3 changes: 3 additions & 0 deletions src/pypistats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ def pypi_stats_api(

_save_cache(cache_file, res)

if not res.get("data", []):
return f"The package '{res.get('package', '')}' does not exist"

# Actual first and last dates of the fetched data
first, last = _date_range(res["data"])

Expand Down
19 changes: 19 additions & 0 deletions tests/test_pypistats.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,3 +747,22 @@ def test_format_pandas(self) -> None:
# Assert
assert isinstance(output, pandas.DataFrame)
assert str(output).strip() == expected_output.strip()

@respx.mock
def test_package_not_exist(self) -> None:
# Arrange
package = "a" * 100
mocked_response = f"""{{
"data":[],
"package":"{package}",
"type":"python_major_downloads"
}}"""
mocked_url = f"https://pypistats.org/api/packages/{package}/python_major"
expected_output = f"The package '{package}' does not exist"

# Act
respx.get(mocked_url).respond(content=mocked_response)
output = pypistats.python_major(package)

# Assert
assert output == expected_output
Loading