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

refactor: change validation in method _set_page_size #30

Merged
merged 1 commit into from
Dec 31, 2024
Merged
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
8 changes: 4 additions & 4 deletions pymusixmatch/musixmatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ def _set_page_size(self, page_size: int) -> int:
Returns:
int: The page size.
"""
if page_size > 100:
page_size = 100
elif page_size < 1:
page_size = 1
if page_size > 100 or page_size < 1:
raise ValueError(
f"Invalid page size: {page_size}, please use a page size between 1 and 100."
)
return page_size

def chart_artists(
Expand Down
15 changes: 14 additions & 1 deletion tests/test_musixmatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,24 @@ def test_get_url(self) -> None:

@pytest.mark.parametrize(
"page_size, expected",
[(-1, 1), (0, 1), (1, 1), (10, 10), (100, 100), (101, 100)],
[(1, 1), (10, 10), (100, 100)],
)
def test_set_page_size(self, page_size: int, expected: int) -> None:
assert self.musixmatch._set_page_size(page_size) == expected

@pytest.mark.parametrize("page_size", [0, -1, 101, 200])
def test_set_page_size_with_invalid_page_size(
self,
page_size: int,
) -> None:
with pytest.raises(ValueError) as excinfo:
self.musixmatch._set_page_size(page_size)

assert (
str(excinfo.value)
== f"Invalid page size: {page_size}, please use a page size between 1 and 100."
)

def test_chart_artists(self, requests_mock, chart_artists: dict) -> None:
url = "http://api.musixmatch.com/ws/1.1/chart.artists.get?page=1&page_size=1&country=us&format=json"
requests_mock.get(url=url, json=chart_artists)
Expand Down
Loading