-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rewrite to make Players class testable. get_team is extracted from Pl…
…ayers and is a variable now
- Loading branch information
Showing
4 changed files
with
277 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import asyncio | ||
import aiohttp | ||
from fpl import FPL | ||
|
||
async def get_teams(): | ||
async with aiohttp.ClientSession() as session: | ||
fpl = FPL(session) | ||
teams = await fpl.get_teams(return_json = True) | ||
return teams | ||
|
||
def builder(): | ||
teams = {} | ||
for team in asyncio.run(get_teams()): | ||
teams[team["id"]] = {"short_name": team["short_name"]} | ||
return teams | ||
|
||
teams = builder() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,96 @@ | ||
from custom.constant import Gameweek | ||
import pytest | ||
|
||
from custom.players import Players | ||
from custom.constant import Gameweek | ||
|
||
from custom.teams import teams | ||
|
||
|
||
def test_init_with_mock(monkeypatch): | ||
monkeypatch.setattr(Gameweek, "PREV_N_GWS", [5, 6]) | ||
monkeypatch.setattr(Gameweek, "NEXT_N_GWS", [8, 9]) | ||
monkeypatch.setitem( | ||
teams, 7, {"short_name": "CHE"} | ||
) # TODO: this works but only patch certain key value pair in the dict, patch the whole var? | ||
|
||
def test_players(monkeypatch): | ||
monkeypatch.setattr(Gameweek, "CURRENT_GW", 7) | ||
monkeypatch.setattr(Gameweek, "NO_OF_PREV_GWS", 5) | ||
monkeypatch.setattr(Gameweek, "PREV_N_GWS", [2, 3, 4, 5, 6]) | ||
monkeypatch.setattr(Gameweek, "NO_OF_NEXT_GWS", 5) | ||
async def mock_get_players(self): | ||
return [ | ||
{ | ||
"element_type": 3, | ||
"ep_next": "3.2", | ||
"ep_this": "2.7", | ||
"form": "2.7", | ||
"id": 202, | ||
"now_cost": 54, | ||
"status": "a", | ||
"team": 7, | ||
"web_name": "Gallagher", | ||
"expected_goals": "0.52", | ||
"expected_assists": "0.76", | ||
"fixtures": [ | ||
{ | ||
"team_h": 6, | ||
"team_a": 7, | ||
"event_name": "Gameweek 8", | ||
"is_home": False, | ||
"difficulty": 2, | ||
}, | ||
{ | ||
"team_h": 7, | ||
"team_a": 1, | ||
"event_name": "Gameweek 9", | ||
"is_home": True, | ||
"difficulty": 4, | ||
}, | ||
], | ||
"history": [ | ||
{ | ||
"round": 1, | ||
}, | ||
{ | ||
"round": 2, | ||
}, | ||
{ | ||
"round": 3, | ||
}, | ||
{ | ||
"round": 4, | ||
}, | ||
{ | ||
"total_points": 3, | ||
"round": 5, | ||
}, | ||
{ | ||
"total_points": 2, | ||
"round": 6, | ||
}, | ||
{ | ||
"total_points": 3, | ||
"round": 7, | ||
"value": 54, | ||
}, | ||
], | ||
} | ||
] | ||
|
||
fpl_id = 427 | ||
players = Players(fpl_ids=[fpl_id]) | ||
monkeypatch.setattr(Players, "get_players", mock_get_players) | ||
|
||
assert players.stats[fpl_id]["web_name"] == "Kane" | ||
assert players.stats[fpl_id]["team_short_name"] == "TOT" | ||
assert players.stats[fpl_id]["pos"] == "FOR" | ||
assert players.stats[fpl_id]["understat_id"] == 647 | ||
assert players.stats[fpl_id]["pts_prev_n_gw"] == {2: 8, 3: 6, 4: 10, 5: 5, 6: 9} | ||
assert players.stats[fpl_id]["total_pts_prev_n_gw"] == 38 | ||
players = Players(fpl_ids=[202]) | ||
assert players.stats == { | ||
202: { | ||
"web_name": "Gallagher", | ||
"team_short_name": "CHE", | ||
"pos": "MID", | ||
"latest_price": "£5.4", | ||
"price_change": "£0.0", | ||
"xg": "0.52", | ||
"xa": "0.76", | ||
"sum_xg_xa": 1.28, | ||
"pts_prev_n_gw": {5: 3, 6: 2}, | ||
"total_pts_prev_n_gw": 5, | ||
"ep_this": 2.7, | ||
"ep_next": 3.2, | ||
"fixtures": {8: ["BUR (A) (2)"], 9: ["ARS (H) (4)"]}, | ||
"fixture_difficulty_avg": 3.0, | ||
} | ||
} |
Oops, something went wrong.