Skip to content

Commit

Permalink
test: add unit tests for fetch_utils.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Desi Pilla committed Oct 7, 2022
1 parent 36346ee commit bd070b7
Show file tree
Hide file tree
Showing 14 changed files with 145 additions and 31 deletions.
7 changes: 6 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,10 @@
"!Split",
"!Split sequence"
],
"python.pythonPath": "C:\\Users\\desid\\anaconda3\\python.exe"
"python.pythonPath": "C:\\Users\\desid\\anaconda3\\python.exe",
"python.testing.pytestArgs": [
"tests"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
2 changes: 2 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
addopts=-n4
Binary file added src/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file removed src/__pycache__/__init__.cpython-38.pyc
Binary file not shown.
Binary file removed src/__pycache__/util_functions.cpython-38.pyc
Binary file not shown.
Binary file modified src/doritostats/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file modified src/doritostats/__pycache__/analytic_utils.cpython-310.pyc
Binary file not shown.
Binary file modified src/doritostats/__pycache__/fetch_utils.cpython-310.pyc
Binary file not shown.
Binary file not shown.
7 changes: 4 additions & 3 deletions src/doritostats/fetch_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import requests
import pandas as pd
import datetime
from espn_api.football import League, Team
from espn_api.football import League, Team, Matchup
from .analytic_utils import (
get_best_trio,
get_lineup_efficiency,
Expand All @@ -13,11 +13,12 @@

''' FETCH LEAGUE '''
def set_league_endpoint(league: League):
# (dt.datetime.now() - dt.timedelta(540)).year): # ESPN API v3
# ESPN API v3
if (league.year >= (datetime.datetime.today() - datetime.timedelta(weeks=12)).year):
league.endpoint = "https://fantasy.espn.com/apis/v3/games/ffl/seasons/" + \
str(league.year) + "/segments/0/leagues/" + \
str(league.league_id) + "?"

else: # ESPN API v2
league.endpoint = "https://fantasy.espn.com/apis/v3/games/ffl/leagueHistory/" + \
str(league.league_id) + "?seasonId=" + str(league.year) + '&'
Expand Down Expand Up @@ -91,7 +92,7 @@ def fetch_league(league_id: int, year: int, swid: str, espn_s2: str):


""" HISTORICAL STATS """
def is_playoff_game(league: League, matchup, week):
def is_playoff_game(league: League, matchup: Matchup, week: int):
''' Accepts a League and Matchup object and determines if the matchup was a playoff game '''

# False if not playoff time yet
Expand Down
21 changes: 0 additions & 21 deletions src/doritostats/tests.py

This file was deleted.

84 changes: 78 additions & 6 deletions src/new espn.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,43 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"year = 2022\n",
"login = pd.read_csv('doritostats/login.csv')\n",
"login = pd.read_csv('../login.csv')\n",
"manager, league_name, league_id, swid, espn_s2 = login.iloc[0]\n",
"# league_id, swid, espn_s2 = 301210, None, None # Jeremy's team"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Make Football Great Again'"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# league = fetch_league(league_id, 2018, swid, espn_s2)\n",
"league.name"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -39,7 +66,7 @@
},
{
"cell_type": "code",
"execution_count": 51,
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -52,7 +79,7 @@
},
{
"cell_type": "code",
"execution_count": 56,
"execution_count": 35,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -272,7 +299,7 @@
"6 tie True True 0.00 0 "
]
},
"execution_count": 56,
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -287,6 +314,51 @@
"df.tail()"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 False\n",
"0 False\n",
"0 False\n",
"0 False\n",
"0 False\n",
" ... \n",
"2 False\n",
"2 False\n",
"2 False\n",
"2 True\n",
"2 False\n",
"Name: is_playoff, Length: 84, dtype: bool"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df[df.year == 2018].is_playoff"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
Binary file not shown.
55 changes: 55 additions & 0 deletions tests/test_pytest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import pytest
import pandas as pd
from espn_api.football import League, Team, Matchup
import src.doritostats.fetch_utils as fetch # The code to test

# Import example creds
login = pd.read_csv('./login.csv')
_, _, league_id, swid, espn_s2 = login.iloc[0]
league_2018 = fetch.fetch_league(league_id, 2018, swid, espn_s2)
league_2022 = fetch.fetch_league(league_id, 2022, swid, espn_s2)

@pytest.mark.parametrize("league, endpoint",
[(league_2018, "https://fantasy.espn.com/apis/v3/games/ffl/leagueHistory/1086064?seasonId=2018&"),
(league_2022, "https://fantasy.espn.com/apis/v3/games/ffl/seasons/2022/segments/0/leagues/1086064?")])
def test_set_league_endpoint(league: League, endpoint: str):
fetch.set_league_endpoint(league)
assert league.endpoint == endpoint


@pytest.mark.parametrize("league_id, year, swid, espn_s2, results",
[
(league_id, 2018, swid, espn_s2,
{
"league_name":"Make Football Great Again",
"cookies":{'swid': swid, 'espn_s2': espn_s2},
}),
(league_id, 2022, swid, espn_s2,
{
"league_name": "La Lega dei Cugini",
"cookies": {'swid': swid, 'espn_s2': espn_s2}
})
])
def test_fetch_league(league_id: int, year: int, swid: str, espn_s2: str, results: dict):
league = fetch.fetch_league(league_id, year, swid, espn_s2)
assert league.name == results['league_name']
assert league.cookies == results['cookies']


@pytest.mark.parametrize("league, matchup, week, result",
[
(league_2018, fetch.PseudoMatchup(
league_2018.teams[0], league_2018.teams[0].schedule[0]), 1, False), # Regular season
(league_2018, fetch.PseudoMatchup(
league_2018.teams[0], league_2018.teams[0].schedule[12]), 13, True), # Known playoff game
(league_2018, fetch.PseudoMatchup(
league_2018.teams[5], league_2018.teams[5].schedule[12]), 13, False), # Known consolation game
(league_2022, league_2022.box_scores(
1)[0], 1, False), # Regular season
(league_2022, league_2022.box_scores(
15)[0], 15, True), # Playoff game
(league_2022, league_2022.box_scores(15)
[-1], 15, False), # Consolation game
])
def test_is_playoff_game(league: League, matchup: Matchup, week: int, result: bool):
assert fetch.is_playoff_game(league, matchup, week) == result

0 comments on commit bd070b7

Please sign in to comment.