Skip to content

Commit

Permalink
initial func w unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyagreco committed Sep 17, 2024
1 parent 5e47fae commit 6895966
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
12 changes: 12 additions & 0 deletions sleeper/api/league.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
ROSTERS_ROUTE,
SLEEPER_APP_BASE_URL,
USER_ROUTE,
USERS_ROUTE,
VERSION,
)
from sleeper.api.util import build_route, get
Expand Down Expand Up @@ -37,3 +38,14 @@ def get_rosters(*, league_id: str) -> list[dict]:
ROSTERS_ROUTE,
)
return get(url)


def get_users_in_league(*, league_id: str) -> list[dict]:
url = build_route(
SLEEPER_APP_BASE_URL,
VERSION,
LEAGUE_ROUTE,
league_id,
USERS_ROUTE,
)
return get(url)
2 changes: 1 addition & 1 deletion sleeper/api/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def build_route(base_url: str, version: Optional[str], *args) -> str:
return f"{base_url}/{'/'.join(args)}"


def get(url: str) -> dict | list:
def get(url: str) -> any:
response = requests.get(url)
response.raise_for_status()
return response.json()
Expand Down
20 changes: 19 additions & 1 deletion test/unit/test_api/test_league.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import unittest
from unittest.mock import patch

from sleeper.api.league import get_league, get_rosters, get_user_leagues_for_year
from sleeper.api.league import (
get_league,
get_rosters,
get_user_leagues_for_year,
get_users_in_league,
)
from sleeper.enum import Sport
from test.unit.helper.helper_classes import MockResponse

Expand Down Expand Up @@ -47,3 +52,16 @@ def test_get_rosters(self, mock_requests_get):
mock_requests_get.assert_called_once_with(
"https://api.sleeper.app/v1/league/12345/rosters"
)

@patch("requests.get")
def test_get_users_in_league(self, mock_requests_get):
mock_list = [{"foo": "bar"}]
mock_response = MockResponse(mock_list, 200)
mock_requests_get.return_value = mock_response

response = get_users_in_league(league_id="12345")

self.assertEqual(mock_list, response)
mock_requests_get.assert_called_once_with(
"https://api.sleeper.app/v1/league/12345/users"
)

0 comments on commit 6895966

Please sign in to comment.