Skip to content

Commit

Permalink
Merge branch 'dict_return_types' into apply_live_bonus
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmadden86 committed Oct 19, 2019
2 parents 9121d30 + 7c9ca19 commit 1948173
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 34 deletions.
30 changes: 18 additions & 12 deletions fpl/fpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ async def get_teams(self, team_ids=None, return_json=False):
if return_json:
return teams

return [Team(team_information, self.session)
for team_information in teams]
return {team_information["id"]: Team(team_information, self.session)
for team_information in teams}

async def get_team(self, team_id, return_json=False):
"""Returns the team with the given ``team_id``.
Expand Down Expand Up @@ -186,6 +186,7 @@ async def get_player_summary(self, player_id, return_json=False):

return PlayerSummary(player_summary)

# not used
async def get_player_summaries(self, player_ids, return_json=False):
"""Returns a list of summaries of players whose ID are
in the ``player_ids`` list.
Expand Down Expand Up @@ -282,7 +283,10 @@ async def get_players(self, player_ids=None, include_summary=False,
for player_id in player_ids]
players = await asyncio.gather(*tasks)

return players
if return_json:
return list(filter(lambda p: p["id"] in player_ids, players))

return {player.id: player for player in players}

async def get_fixture(self, fixture_id, return_json=False):
"""Returns the fixture with the given ``fixture_id``.
Expand Down Expand Up @@ -359,7 +363,7 @@ async def get_fixtures_by_id(self, fixture_ids, return_json=False):
if return_json:
return fixtures

return [Fixture(fixture) for fixture in fixtures]
return {fixture["id"]: Fixture(fixture) for fixture in fixtures}

async def get_fixtures_by_gameweek(self, gameweek, return_json=False):
"""Returns a list of all fixtures of the given ``gameweek``.
Expand All @@ -382,7 +386,7 @@ async def get_fixtures_by_gameweek(self, gameweek, return_json=False):
if return_json:
return fixtures

return [Fixture(fixture) for fixture in fixtures]
return {fixture["id"]: Fixture(fixture) for fixture in fixtures}

async def get_fixtures(self, return_json=False):
"""Returns a list of *all* fixtures.
Expand All @@ -409,10 +413,9 @@ async def get_fixtures(self, return_json=False):
if return_json:
return fixtures

return [Fixture(fixture) for fixture in fixtures]
return {fixture["id"]: Fixture(fixture) for fixture in fixtures}

async def get_gameweek(self, gameweek_id, include_live=False,
return_json=False):
async def get_gameweek(self, gameweek_id, return_json=False):
"""Returns the gameweek with the ID ``gameweek_id``.
Information is taken from e.g.:
Expand Down Expand Up @@ -465,8 +468,7 @@ async def get_gameweek(self, gameweek_id, include_live=False,

return Gameweek(static_gameweek)

async def get_gameweeks(self, gameweek_ids=None, include_live=False,
return_json=False):
async def get_gameweeks(self, gameweek_ids=None, return_json=False):
"""Returns either a list of *all* gamweeks, or a list of gameweeks
whose IDs are in the ``gameweek_ids`` list.
Expand All @@ -486,11 +488,15 @@ async def get_gameweeks(self, gameweek_ids=None, include_live=False,
gameweek_ids = range(1, 39)

tasks = [asyncio.ensure_future(
self.get_gameweek(gameweek_id, include_live, return_json))
self.get_gameweek(gameweek_id, return_json))
for gameweek_id in gameweek_ids]

gameweeks = await asyncio.gather(*tasks)
return gameweeks

if return_json:
return gameweeks

return {gameweek.id: gameweek for gameweek in gameweeks}

async def get_classic_league(self, league_id, return_json=False):
"""Returns the classic league with the given ``league_id``. Requires
Expand Down
49 changes: 27 additions & 22 deletions tests/test_fpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,20 @@ async def test_team(self, loop, fpl):

async def test_teams(self, loop, fpl):
teams = await fpl.get_teams()
assert isinstance(teams, list)
assert len(teams) == 20
assert isinstance(teams[0], Team)
assert isinstance(teams, dict)
assert len(teams.values()) == 20
assert isinstance(teams[10], Team)

teams = await fpl.get_teams(return_json=True)
assert isinstance(teams, list)
assert len(teams) == 20
assert isinstance(teams[0], dict)
assert isinstance(teams[1], dict)

teams = await fpl.get_teams(team_ids=[1, 2, 3])
assert isinstance(teams, list)
assert len(teams) == 3
assert isinstance(teams[0], Team)
assert [team.id for team in teams] == [1, 2, 3]
assert isinstance(teams, dict)
assert len(teams.values()) == 3
assert isinstance(teams[1], Team)
assert [team.id for team in teams.values()] == [1, 2, 3]

async def test_player_summary(self, loop, fpl):
# test non positive id
Expand Down Expand Up @@ -122,19 +122,24 @@ async def test_player(self, loop, fpl):

async def test_players(self, loop, fpl):
players = await fpl.get_players()
assert isinstance(players, list)
assert isinstance(players[0], Player)
assert isinstance(players, dict)
assert isinstance(players[1], Player)

players = await fpl.get_players(return_json=True)
assert isinstance(players, list)
assert isinstance(players[0], dict)

players = await fpl.get_players([1, 2, 3])
assert len(players) == 3
assert len(players.values()) == 3

players = await fpl.get_players([1, 2, 3], True)
players = await fpl.get_players([1, 2, 3], include_summary=True)
assert len(players.values()) == 3
summary_keys = ("history_past", "history", "fixtures")
assert all([isinstance(getattr(players[2], key), list) for key in summary_keys])

players = await fpl.get_players([1, 2, 3], include_summary=True, return_json=True)
assert len(players) == 3
assert isinstance(players[0].fixtures, list)
assert all([isinstance(players[2][key], list) for key in summary_keys])

async def test_fixture(self, loop, fpl):
# test fixture with unknown id
Expand All @@ -154,8 +159,8 @@ async def test_fixtures_by_id(self, loop, fpl):
assert len(fixtures) == 0

fixtures = await fpl.get_fixtures_by_id([100, 200, 300])
assert isinstance(fixtures, list)
assert isinstance(fixtures[0], Fixture)
assert isinstance(fixtures, dict)
assert isinstance(fixtures[100], Fixture)

fixtures = await fpl.get_fixtures_by_id(
[100, 200, 300], return_json=True)
Expand All @@ -168,26 +173,26 @@ async def test_fixtures_by_id(self, loop, fpl):
async def test_fixtures_by_gameweek(self, loop, fpl):
for gameweek in range(1, 39):
fixtures = await fpl.get_fixtures_by_gameweek(gameweek)
assert isinstance(fixtures, list)
assert isinstance(fixtures[0], Fixture)
assert isinstance(fixtures, dict)
assert all([isinstance(fixtures[fixture_id], Fixture) for fixture_id in fixtures.keys()])

fixtures = await fpl.get_fixtures_by_gameweek(
gameweek, return_json=True)
assert isinstance(fixtures[0], dict)

async def test_fixtures(self, loop, fpl):
fixtures = await fpl.get_fixtures()
assert isinstance(fixtures, list)
assert isinstance(fixtures[0], Fixture)
assert isinstance(fixtures, dict)
assert isinstance(fixtures[10], Fixture)

fixtures = await fpl.get_fixtures(return_json=True)
assert isinstance(fixtures[0], dict)

async def test_gameweeks(self, loop, fpl):
gameweeks = await fpl.get_gameweeks()
assert isinstance(gameweeks, list)
assert len(gameweeks) == 38
assert isinstance(gameweeks[0], Gameweek)
assert isinstance(gameweeks, dict)
assert len(gameweeks.values()) == 38
assert isinstance(gameweeks[10], Gameweek)

gameweeks = await fpl.get_gameweeks([1, 2, 3], return_json=True)
assert isinstance(gameweeks, list)
Expand Down

0 comments on commit 1948173

Please sign in to comment.