diff --git a/requirements.txt b/requirements.txt index e087fd8..1688c7c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,6 @@ argparse==1.2.1 click==5.0 requests==2.7.0 wsgiref==0.1.2 +betamax==0.6.0 +betamax-serializers==0.2.0 +pytest==2.9.1 diff --git a/soccer/main.py b/soccer/main.py index 376db52..7c1ffc6 100644 --- a/soccer/main.py +++ b/soccer/main.py @@ -14,6 +14,10 @@ LEAGUE_IDS = leagueids.LEAGUE_IDS TEAM_NAMES = teamnames.team_names +session = requests.Session() +headers = { + 'X-Auth-Token': '' +} def get_input_key(): """Input API key and validate""" @@ -41,6 +45,7 @@ def load_config_key(): try: api_token = os.environ['SOCCER_CLI_API_TOKEN'] except KeyError: + home = os.path.expanduser("~") config = os.path.join(home, "soccer-cli.ini") if not os.path.exists(config): @@ -65,7 +70,7 @@ def load_config_key(): def _get(url): """Handles api.football-data.org requests""" - req = requests.get(BASE_URL+url, headers=headers) + req = session.get(BASE_URL+url, headers=headers) if req.status_code == requests.codes.ok: return req @@ -85,7 +90,7 @@ def _get(url): def get_live_scores(writer, use_12_hour_format): """Gets the live scores""" - req = requests.get(LIVE_URL) + req = session.get(LIVE_URL) if req.status_code == requests.codes.ok: scores = req.json() if len(scores["games"]) == 0: @@ -237,11 +242,9 @@ def list_team_codes(): help="Save output to a file (only if csv or json option is provided)") def main(league, time, standings, team, live, use12hour, players, output_format, output_file, upcoming, lookup, listcodes, apikey): """A CLI for live and past football scores from various football leagues""" - global headers - headers = { - 'X-Auth-Token': apikey - } try: + headers['X-Auth-Token'] = apikey + if output_format == 'stdout' and output_file: raise IncorrectParametersException('Printing output to stdout and ' 'saving to a file are mutually exclusive') diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/cassettes/League_standings.json b/tests/cassettes/League_standings.json new file mode 100644 index 0000000..d729e49 --- /dev/null +++ b/tests/cassettes/League_standings.json @@ -0,0 +1 @@ +{"http_interactions": [{"request": {"body": {"string": "", "encoding": "utf-8"}, "headers": {"Connection": ["keep-alive"], "User-Agent": ["python-requests/2.7.0 CPython/2.7.6 Linux/3.4.0+"], "Accept-Encoding": ["gzip, deflate"], "Accept": ["*/*"], "X-Auth-Token": ["f13b85a10d704cbe99e1aeac5e36703e"]}, "method": "GET", "uri": "http://api.football-data.org/alpha/soccerseasons/398/leagueTable"}, "response": {"body": {"string": "{\"_links\":{\"self\":{\"href\":\"http://api.football-data.org/alpha/soccerseasons/398/leagueTable/?matchday=34\"},\"soccerseason\":{\"href\":\"http://api.football-data.org/alpha/soccerseasons/398\"}},\"leagueCaption\":\"Premier League 2015/16\",\"matchday\":34,\"standing\":[{\"_links\":{\"team\":{\"href\":\"http://api.football-data.org/alpha/teams/338\"}},\"position\":1,\"teamName\":\"Leicester City FC\",\"playedGames\":33,\"points\":72,\"goals\":57,\"goalsAgainst\":31,\"goalDifference\":26,\"wins\":21,\"draws\":9,\"losses\":3,\"home\":{\"goals\":26,\"goalsAgainst\":15,\"wins\":10,\"draws\":5,\"losses\":1},\"away\":{\"goals\":31,\"goalsAgainst\":16,\"wins\":11,\"draws\":4,\"losses\":2}},{\"_links\":{\"team\":{\"href\":\"http://api.football-data.org/alpha/teams/73\"}},\"position\":2,\"teamName\":\"Tottenham Hotspur FC\",\"playedGames\":33,\"points\":65,\"goals\":60,\"goalsAgainst\":25,\"goalDifference\":35,\"wins\":18,\"draws\":11,\"losses\":4,\"home\":{\"goals\":33,\"goalsAgainst\":12,\"wins\":10,\"draws\":5,\"losses\":2},\"away\":{\"goals\":27,\"goalsAgainst\":13,\"wins\":8,\"draws\":6,\"losses\":2}},{\"_links\":{\"team\":{\"href\":\"http://api.football-data.org/alpha/teams/57\"}},\"position\":3,\"teamName\":\"Arsenal FC\",\"playedGames\":32,\"points\":59,\"goals\":55,\"goalsAgainst\":33,\"goalDifference\":22,\"wins\":17,\"draws\":8,\"losses\":7,\"home\":{\"goals\":23,\"goalsAgainst\":10,\"wins\":9,\"draws\":3,\"losses\":3},\"away\":{\"goals\":32,\"goalsAgainst\":23,\"wins\":8,\"draws\":5,\"losses\":4}},{\"_links\":{\"team\":{\"href\":\"http://api.football-data.org/alpha/teams/65\"}},\"position\":4,\"teamName\":\"Manchester City FC\",\"playedGames\":32,\"points\":57,\"goals\":58,\"goalsAgainst\":33,\"goalDifference\":25,\"wins\":17,\"draws\":6,\"losses\":9,\"home\":{\"goals\":41,\"goalsAgainst\":19,\"wins\":11,\"draws\":1,\"losses\":5},\"away\":{\"goals\":17,\"goalsAgainst\":14,\"wins\":6,\"draws\":5,\"losses\":4}},{\"_links\":{\"team\":{\"href\":\"http://api.football-data.org/alpha/teams/66\"}},\"position\":5,\"teamName\":\"Manchester United FC\",\"playedGames\":32,\"points\":53,\"goals\":39,\"goalsAgainst\":30,\"goalDifference\":9,\"wins\":15,\"draws\":8,\"losses\":9,\"home\":{\"goals\":20,\"goalsAgainst\":7,\"wins\":9,\"draws\":4,\"losses\":2},\"away\":{\"goals\":19,\"goalsAgainst\":23,\"wins\":6,\"draws\":4,\"losses\":7}},{\"_links\":{\"team\":{\"href\":\"http://api.football-data.org/alpha/teams/563\"}},\"position\":6,\"teamName\":\"West Ham United FC\",\"playedGames\":32,\"points\":52,\"goals\":52,\"goalsAgainst\":40,\"goalDifference\":12,\"wins\":13,\"draws\":13,\"losses\":6,\"home\":{\"goals\":27,\"goalsAgainst\":19,\"wins\":7,\"draws\":7,\"losses\":2},\"away\":{\"goals\":25,\"goalsAgainst\":21,\"wins\":6,\"draws\":6,\"losses\":4}},{\"_links\":{\"team\":{\"href\":\"http://api.football-data.org/alpha/teams/340\"}},\"position\":7,\"teamName\":\"Southampton FC\",\"playedGames\":33,\"points\":50,\"goals\":44,\"goalsAgainst\":34,\"goalDifference\":10,\"wins\":14,\"draws\":8,\"losses\":11,\"home\":{\"goals\":31,\"goalsAgainst\":19,\"wins\":9,\"draws\":3,\"losses\":5},\"away\":{\"goals\":13,\"goalsAgainst\":15,\"wins\":5,\"draws\":5,\"losses\":6}},{\"_links\":{\"team\":{\"href\":\"http://api.football-data.org/alpha/teams/64\"}},\"position\":8,\"teamName\":\"Liverpool FC\",\"playedGames\":31,\"points\":48,\"goals\":50,\"goalsAgainst\":42,\"goalDifference\":8,\"wins\":13,\"draws\":9,\"losses\":9,\"home\":{\"goals\":24,\"goalsAgainst\":19,\"wins\":6,\"draws\":6,\"losses\":3},\"away\":{\"goals\":26,\"goalsAgainst\":23,\"wins\":7,\"draws\":3,\"losses\":6}},{\"_links\":{\"team\":{\"href\":\"http://api.football-data.org/alpha/teams/70\"}},\"position\":9,\"teamName\":\"Stoke City FC\",\"playedGames\":33,\"points\":47,\"goals\":37,\"goalsAgainst\":43,\"goalDifference\":-6,\"wins\":13,\"draws\":8,\"losses\":12,\"home\":{\"goals\":19,\"goalsAgainst\":18,\"wins\":7,\"draws\":3,\"losses\":6},\"away\":{\"goals\":18,\"goalsAgainst\":25,\"wins\":6,\"draws\":5,\"losses\":6}},{\"_links\":{\"team\":{\"href\":\"http://api.football-data.org/alpha/teams/61\"}},\"position\":10,\"teamName\":\"Chelsea FC\",\"playedGames\":32,\"points\":44,\"goals\":49,\"goalsAgainst\":42,\"goalDifference\":7,\"wins\":11,\"draws\":11,\"losses\":10,\"home\":{\"goals\":29,\"goalsAgainst\":24,\"wins\":5,\"draws\":7,\"losses\":4},\"away\":{\"goals\":20,\"goalsAgainst\":18,\"wins\":6,\"draws\":4,\"losses\":6}},{\"_links\":{\"team\":{\"href\":\"http://api.football-data.org/alpha/teams/1044\"}},\"position\":11,\"teamName\":\"AFC Bournemouth\",\"playedGames\":33,\"points\":41,\"goals\":40,\"goalsAgainst\":55,\"goalDifference\":-15,\"wins\":11,\"draws\":8,\"losses\":14,\"home\":{\"goals\":20,\"goalsAgainst\":27,\"wins\":5,\"draws\":4,\"losses\":7},\"away\":{\"goals\":20,\"goalsAgainst\":28,\"wins\":6,\"draws\":4,\"losses\":7}},{\"_links\":{\"team\":{\"href\":\"http://api.football-data.org/alpha/teams/72\"}},\"position\":12,\"teamName\":\"Swansea City FC\",\"playedGames\":33,\"points\":40,\"goals\":34,\"goalsAgainst\":42,\"goalDifference\":-8,\"wins\":10,\"draws\":10,\"losses\":13,\"home\":{\"goals\":16,\"goalsAgainst\":18,\"wins\":7,\"draws\":5,\"losses\":5},\"away\":{\"goals\":18,\"goalsAgainst\":24,\"wins\":3,\"draws\":5,\"losses\":8}},{\"_links\":{\"team\":{\"href\":\"http://api.football-data.org/alpha/teams/74\"}},\"position\":13,\"teamName\":\"West Bromwich Albion FC\",\"playedGames\":32,\"points\":40,\"goals\":31,\"goalsAgainst\":39,\"goalDifference\":-8,\"wins\":10,\"draws\":10,\"losses\":12,\"home\":{\"goals\":19,\"goalsAgainst\":21,\"wins\":6,\"draws\":4,\"losses\":6},\"away\":{\"goals\":12,\"goalsAgainst\":18,\"wins\":4,\"draws\":6,\"losses\":6}},{\"_links\":{\"team\":{\"href\":\"http://api.football-data.org/alpha/teams/62\"}},\"position\":14,\"teamName\":\"Everton FC\",\"playedGames\":31,\"points\":39,\"goals\":52,\"goalsAgainst\":43,\"goalDifference\":9,\"wins\":9,\"draws\":12,\"losses\":10,\"home\":{\"goals\":29,\"goalsAgainst\":28,\"wins\":4,\"draws\":4,\"losses\":8},\"away\":{\"goals\":23,\"goalsAgainst\":15,\"wins\":5,\"draws\":8,\"losses\":2}},{\"_links\":{\"team\":{\"href\":\"http://api.football-data.org/alpha/teams/346\"}},\"position\":15,\"teamName\":\"Watford FC\",\"playedGames\":32,\"points\":38,\"goals\":31,\"goalsAgainst\":37,\"goalDifference\":-6,\"wins\":10,\"draws\":8,\"losses\":14,\"home\":{\"goals\":15,\"goalsAgainst\":15,\"wins\":5,\"draws\":5,\"losses\":7},\"away\":{\"goals\":16,\"goalsAgainst\":22,\"wins\":5,\"draws\":3,\"losses\":7}},{\"_links\":{\"team\":{\"href\":\"http://api.football-data.org/alpha/teams/354\"}},\"position\":16,\"teamName\":\"Crystal Palace FC\",\"playedGames\":32,\"points\":37,\"goals\":35,\"goalsAgainst\":42,\"goalDifference\":-7,\"wins\":10,\"draws\":7,\"losses\":15,\"home\":{\"goals\":17,\"goalsAgainst\":22,\"wins\":5,\"draws\":2,\"losses\":10},\"away\":{\"goals\":18,\"goalsAgainst\":20,\"wins\":5,\"draws\":5,\"losses\":5}},{\"_links\":{\"team\":{\"href\":\"http://api.football-data.org/alpha/teams/68\"}},\"position\":17,\"teamName\":\"Norwich City FC\",\"playedGames\":33,\"points\":31,\"goals\":35,\"goalsAgainst\":57,\"goalDifference\":-22,\"wins\":8,\"draws\":7,\"losses\":18,\"home\":{\"goals\":22,\"goalsAgainst\":24,\"wins\":5,\"draws\":5,\"losses\":6},\"away\":{\"goals\":13,\"goalsAgainst\":33,\"wins\":3,\"draws\":2,\"losses\":12}},{\"_links\":{\"team\":{\"href\":\"http://api.football-data.org/alpha/teams/71\"}},\"position\":18,\"teamName\":\"Sunderland AFC\",\"playedGames\":32,\"points\":27,\"goals\":36,\"goalsAgainst\":57,\"goalDifference\":-21,\"wins\":6,\"draws\":9,\"losses\":17,\"home\":{\"goals\":17,\"goalsAgainst\":18,\"wins\":4,\"draws\":5,\"losses\":7},\"away\":{\"goals\":19,\"goalsAgainst\":39,\"wins\":2,\"draws\":4,\"losses\":10}},{\"_links\":{\"team\":{\"href\":\"http://api.football-data.org/alpha/teams/67\"}},\"position\":19,\"teamName\":\"Newcastle United FC\",\"playedGames\":32,\"points\":25,\"goals\":32,\"goalsAgainst\":61,\"goalDifference\":-29,\"wins\":6,\"draws\":7,\"losses\":19,\"home\":{\"goals\":22,\"goalsAgainst\":22,\"wins\":4,\"draws\":6,\"losses\":5},\"away\":{\"goals\":10,\"goalsAgainst\":39,\"wins\":2,\"draws\":1,\"losses\":14}},{\"_links\":{\"team\":{\"href\":\"http://api.football-data.org/alpha/teams/58\"}},\"position\":20,\"teamName\":\"Aston Villa FC\",\"playedGames\":33,\"points\":16,\"goals\":23,\"goalsAgainst\":64,\"goalDifference\":-41,\"wins\":3,\"draws\":7,\"losses\":23,\"home\":{\"goals\":12,\"goalsAgainst\":31,\"wins\":2,\"draws\":4,\"losses\":11},\"away\":{\"goals\":11,\"goalsAgainst\":33,\"wins\":1,\"draws\":3,\"losses\":12}}]}", "encoding": "UTF-8"}, "headers": {"x-api-version": ["alpha"], "transfer-encoding": ["chunked"], "x-requestcounter-reset": ["60"], "x-response-control": ["full"], "server": ["nginx/1.2.1"], "x-requests-available": ["49"], "connection": ["keep-alive"], "access-control-allow-credentials": ["true"], "date": ["Fri, 15 Apr 2016 05:39:29 GMT"], "access-control-allow-origin": ["*"], "access-control-allow-methods": ["GET"], "content-type": ["application/json;charset=UTF-8"], "x-authenticated-client": ["Carlos Vargas"]}, "status": {"message": "OK", "code": 200}, "url": "http://api.football-data.org/alpha/soccerseasons/398/leagueTable"}, "recorded_at": "2016-04-15T05:39:37"}], "recorded_with": "betamax/0.6.0"} \ No newline at end of file diff --git a/tests/cassettes/Scores_live_games.json b/tests/cassettes/Scores_live_games.json new file mode 100644 index 0000000..e4f3386 --- /dev/null +++ b/tests/cassettes/Scores_live_games.json @@ -0,0 +1 @@ +{"http_interactions": [], "recorded_with": "betamax/0.6.0"} \ No newline at end of file diff --git a/tests/cassettes/Scores_specific_league.json b/tests/cassettes/Scores_specific_league.json new file mode 100644 index 0000000..41ba8d6 --- /dev/null +++ b/tests/cassettes/Scores_specific_league.json @@ -0,0 +1 @@ +{"http_interactions": [{"request": {"body": {"string": "", "encoding": "utf-8"}, "headers": {"Connection": ["keep-alive"], "X-Auth-Token": ["f13b85a10d704cbe99e1aeac5e36703e"], "Accept-Encoding": ["gzip, deflate"], "Accept": ["*/*"], "User-Agent": ["python-requests/2.7.0 CPython/2.7.6 Linux/3.4.0+"]}, "method": "GET", "uri": "http://api.football-data.org/alpha/soccerseasons/398/fixtures?timeFrame=p6"}, "response": {"body": {"string": "{\"_links\":{\"self\":{\"href\":\"http://api.football-data.org/alpha/soccerseasons/398/fixtures\"},\"soccerseason\":{\"href\":\"http://api.football-data.org/alpha/soccerseasons/398\"}},\"count\":11,\"fixtures\":[{\"_links\":{\"self\":{\"href\":\"http://api.football-data.org/alpha/fixtures/146791\"},\"soccerseason\":{\"href\":\"http://api.football-data.org/alpha/soccerseasons/398\"},\"homeTeam\":{\"href\":\"http://api.football-data.org/alpha/teams/563\"},\"awayTeam\":{\"href\":\"http://api.football-data.org/alpha/teams/57\"}},\"date\":\"2016-04-09T11:45:00Z\",\"status\":\"FINISHED\",\"matchday\":33,\"homeTeamName\":\"West Ham United FC\",\"awayTeamName\":\"Arsenal FC\",\"result\":{\"goalsHomeTeam\":3,\"goalsAwayTeam\":3}},{\"_links\":{\"self\":{\"href\":\"http://api.football-data.org/alpha/fixtures/146788\"},\"soccerseason\":{\"href\":\"http://api.football-data.org/alpha/soccerseasons/398\"},\"homeTeam\":{\"href\":\"http://api.football-data.org/alpha/teams/72\"},\"awayTeam\":{\"href\":\"http://api.football-data.org/alpha/teams/61\"}},\"date\":\"2016-04-09T14:00:00Z\",\"status\":\"FINISHED\",\"matchday\":33,\"homeTeamName\":\"Swansea City FC\",\"awayTeamName\":\"Chelsea FC\",\"result\":{\"goalsHomeTeam\":1,\"goalsAwayTeam\":0}},{\"_links\":{\"self\":{\"href\":\"http://api.football-data.org/alpha/fixtures/146786\"},\"soccerseason\":{\"href\":\"http://api.football-data.org/alpha/soccerseasons/398\"},\"homeTeam\":{\"href\":\"http://api.football-data.org/alpha/teams/340\"},\"awayTeam\":{\"href\":\"http://api.football-data.org/alpha/teams/67\"}},\"date\":\"2016-04-09T14:00:00Z\",\"status\":\"FINISHED\",\"matchday\":33,\"homeTeamName\":\"Southampton FC\",\"awayTeamName\":\"Newcastle United FC\",\"result\":{\"goalsHomeTeam\":3,\"goalsAwayTeam\":1}},{\"_links\":{\"self\":{\"href\":\"http://api.football-data.org/alpha/fixtures/146783\"},\"soccerseason\":{\"href\":\"http://api.football-data.org/alpha/soccerseasons/398\"},\"homeTeam\":{\"href\":\"http://api.football-data.org/alpha/teams/354\"},\"awayTeam\":{\"href\":\"http://api.football-data.org/alpha/teams/68\"}},\"date\":\"2016-04-09T14:00:00Z\",\"status\":\"FINISHED\",\"matchday\":33,\"homeTeamName\":\"Crystal Palace FC\",\"awayTeamName\":\"Norwich City FC\",\"result\":{\"goalsHomeTeam\":1,\"goalsAwayTeam\":0}},{\"_links\":{\"self\":{\"href\":\"http://api.football-data.org/alpha/fixtures/146790\"},\"soccerseason\":{\"href\":\"http://api.football-data.org/alpha/soccerseasons/398\"},\"homeTeam\":{\"href\":\"http://api.football-data.org/alpha/teams/346\"},\"awayTeam\":{\"href\":\"http://api.football-data.org/alpha/teams/62\"}},\"date\":\"2016-04-09T14:00:00Z\",\"status\":\"FINISHED\",\"matchday\":33,\"homeTeamName\":\"Watford FC\",\"awayTeamName\":\"Everton FC\",\"result\":{\"goalsHomeTeam\":1,\"goalsAwayTeam\":1}},{\"_links\":{\"self\":{\"href\":\"http://api.football-data.org/alpha/fixtures/147085\"},\"soccerseason\":{\"href\":\"http://api.football-data.org/alpha/soccerseasons/398\"},\"homeTeam\":{\"href\":\"http://api.football-data.org/alpha/teams/58\"},\"awayTeam\":{\"href\":\"http://api.football-data.org/alpha/teams/1044\"}},\"date\":\"2016-04-09T14:00:00Z\",\"status\":\"FINISHED\",\"matchday\":33,\"homeTeamName\":\"Aston Villa FC\",\"awayTeamName\":\"AFC Bournemouth\",\"result\":{\"goalsHomeTeam\":1,\"goalsAwayTeam\":2}},{\"_links\":{\"self\":{\"href\":\"http://api.football-data.org/alpha/fixtures/146785\"},\"soccerseason\":{\"href\":\"http://api.football-data.org/alpha/soccerseasons/398\"},\"homeTeam\":{\"href\":\"http://api.football-data.org/alpha/teams/65\"},\"awayTeam\":{\"href\":\"http://api.football-data.org/alpha/teams/74\"}},\"date\":\"2016-04-09T16:30:00Z\",\"status\":\"FINISHED\",\"matchday\":33,\"homeTeamName\":\"Manchester City FC\",\"awayTeamName\":\"West Bromwich Albion FC\",\"result\":{\"goalsHomeTeam\":2,\"goalsAwayTeam\":1}},{\"_links\":{\"self\":{\"href\":\"http://api.football-data.org/alpha/fixtures/146787\"},\"soccerseason\":{\"href\":\"http://api.football-data.org/alpha/soccerseasons/398\"},\"homeTeam\":{\"href\":\"http://api.football-data.org/alpha/teams/71\"},\"awayTeam\":{\"href\":\"http://api.football-data.org/alpha/teams/338\"}},\"date\":\"2016-04-10T12:30:00Z\",\"status\":\"FINISHED\",\"matchday\":33,\"homeTeamName\":\"Sunderland AFC\",\"awayTeamName\":\"Leicester City FC\",\"result\":{\"goalsHomeTeam\":0,\"goalsAwayTeam\":2}},{\"_links\":{\"self\":{\"href\":\"http://api.football-data.org/alpha/fixtures/146789\"},\"soccerseason\":{\"href\":\"http://api.football-data.org/alpha/soccerseasons/398\"},\"homeTeam\":{\"href\":\"http://api.football-data.org/alpha/teams/73\"},\"awayTeam\":{\"href\":\"http://api.football-data.org/alpha/teams/66\"}},\"date\":\"2016-04-10T15:00:00Z\",\"status\":\"FINISHED\",\"matchday\":33,\"homeTeamName\":\"Tottenham Hotspur FC\",\"awayTeamName\":\"Manchester United FC\",\"result\":{\"goalsHomeTeam\":3,\"goalsAwayTeam\":0}},{\"_links\":{\"self\":{\"href\":\"http://api.football-data.org/alpha/fixtures/146784\"},\"soccerseason\":{\"href\":\"http://api.football-data.org/alpha/soccerseasons/398\"},\"homeTeam\":{\"href\":\"http://api.football-data.org/alpha/teams/64\"},\"awayTeam\":{\"href\":\"http://api.football-data.org/alpha/teams/70\"}},\"date\":\"2016-04-10T15:00:00Z\",\"status\":\"FINISHED\",\"matchday\":33,\"homeTeamName\":\"Liverpool FC\",\"awayTeamName\":\"Stoke City FC\",\"result\":{\"goalsHomeTeam\":4,\"goalsAwayTeam\":1}},{\"_links\":{\"self\":{\"href\":\"http://api.football-data.org/alpha/fixtures/146766\"},\"soccerseason\":{\"href\":\"http://api.football-data.org/alpha/soccerseasons/398\"},\"homeTeam\":{\"href\":\"http://api.football-data.org/alpha/teams/354\"},\"awayTeam\":{\"href\":\"http://api.football-data.org/alpha/teams/62\"}},\"date\":\"2016-04-13T19:00:00Z\",\"status\":\"FINISHED\",\"matchday\":35,\"homeTeamName\":\"Crystal Palace FC\",\"awayTeamName\":\"Everton FC\",\"result\":{\"goalsHomeTeam\":0,\"goalsAwayTeam\":0}}]}", "encoding": "UTF-8"}, "headers": {"x-api-version": ["alpha"], "date": ["Fri, 15 Apr 2016 04:46:28 GMT"], "transfer-encoding": ["chunked"], "x-response-control": ["full"], "server": ["nginx/1.2.1"], "x-requests-available": ["47"], "connection": ["keep-alive"], "access-control-allow-credentials": ["true"], "x-requestcounter-reset": ["38"], "access-control-allow-origin": ["*"], "access-control-allow-methods": ["GET"], "content-type": ["application/json;charset=UTF-8"], "x-authenticated-client": ["Carlos Vargas"]}, "status": {"message": "OK", "code": 200}, "url": "http://api.football-data.org/alpha/soccerseasons/398/fixtures?timeFrame=p6"}, "recorded_at": "2016-04-15T04:46:36"}, {"request": {"body": {"string": "", "encoding": "utf-8"}, "headers": {"Connection": ["keep-alive"], "User-Agent": ["python-requests/2.7.0 CPython/2.7.6 Linux/3.4.0+"], "Accept-Encoding": ["gzip, deflate"], "Accept": ["*/*"], "X-Auth-Token": ["f13b85a10d704cbe99e1aeac5e36703e"]}, "method": "GET", "uri": "http://api.football-data.org/alpha/soccerseasons/398/fixtures?timeFrame=p6"}, "recorded_at": "2016-04-15T04:48:21"}], "recorded_with": "betamax/0.6.0"} \ No newline at end of file diff --git a/tests/cassettes/Team_invalid.json b/tests/cassettes/Team_invalid.json new file mode 100644 index 0000000..e4f3386 --- /dev/null +++ b/tests/cassettes/Team_invalid.json @@ -0,0 +1 @@ +{"http_interactions": [], "recorded_with": "betamax/0.6.0"} \ No newline at end of file diff --git a/tests/cassettes/Team_player_names.json b/tests/cassettes/Team_player_names.json new file mode 100644 index 0000000..8acc253 --- /dev/null +++ b/tests/cassettes/Team_player_names.json @@ -0,0 +1 @@ +{"http_interactions": [{"request": {"body": {"string": "", "encoding": "utf-8"}, "headers": {"Connection": ["keep-alive"], "User-Agent": ["python-requests/2.7.0 CPython/2.7.5 Windows/8"], "Accept-Encoding": ["gzip, deflate"], "Accept": ["*/*"], "X-Auth-Token": ["956ca2b3e1fa48e995376ca735f8e5ce"]}, "method": "GET", "uri": "http://api.football-data.org/alpha/teams/57/players"}, "response": {"body": {"string": "{\"_links\":{\"self\":{\"href\":\"http://api.football-data.org/alpha/teams/57/players\"},\"team\":{\"href\":\"http://api.football-data.org/alpha/teams/57\"}},\"count\":26,\"players\":[{\"id\":1952,\"name\":\"David Ospina\",\"position\":\"Keeper\",\"jerseyNumber\":13,\"dateOfBirth\":\"1988-08-31\",\"nationality\":\"Colombia\",\"contractUntil\":\"2019-06-30\",\"marketValue\":\"8,000,000 \u20ac\"},{\"id\":1954,\"name\":\"Per Mertesacker\",\"position\":\"Centre Back\",\"jerseyNumber\":4,\"dateOfBirth\":\"1984-09-29\",\"nationality\":\"Germany\",\"contractUntil\":\"2017-06-30\",\"marketValue\":\"15,000,000 \u20ac\"},{\"id\":1955,\"name\":\"Laurent Koscielny\",\"position\":\"Centre Back\",\"jerseyNumber\":6,\"dateOfBirth\":\"1985-09-10\",\"nationality\":\"France\",\"contractUntil\":\"2017-06-30\",\"marketValue\":\"20,000,000 \u20ac\"},{\"id\":1956,\"name\":\"Kieran Gibbs\",\"position\":\"Left-Back\",\"jerseyNumber\":3,\"dateOfBirth\":\"1989-09-26\",\"nationality\":\"England\",\"contractUntil\":\"2018-06-30\",\"marketValue\":\"13,000,000 \u20ac\"},{\"id\":1957,\"name\":\"Nacho Monreal\",\"position\":\"Left-Back\",\"jerseyNumber\":18,\"dateOfBirth\":\"1986-02-26\",\"nationality\":\"Spain\",\"contractUntil\":\"2019-06-30\",\"marketValue\":\"12,000,000 \u20ac\"},{\"id\":1959,\"name\":\"Calum Chambers\",\"position\":\"Centre Back\",\"jerseyNumber\":21,\"dateOfBirth\":\"1995-01-20\",\"nationality\":\"England\",\"contractUntil\":\"2020-06-30\",\"marketValue\":\"12,000,000 \u20ac\"},{\"id\":1960,\"name\":\"H\u00e9ctor Beller\u00edn\",\"position\":\"Right-Back\",\"jerseyNumber\":24,\"dateOfBirth\":\"1995-03-19\",\"nationality\":\"Spain\",\"contractUntil\":\"2019-06-30\",\"marketValue\":\"5,000,000 \u20ac\"},{\"id\":1961,\"name\":\"Mathieu Flamini\",\"position\":\"Defensive Midfield\",\"jerseyNumber\":20,\"dateOfBirth\":\"1984-03-07\",\"nationality\":\"France\",\"contractUntil\":\"2016-06-30\",\"marketValue\":\"4,000,000 \u20ac\"},{\"id\":1962,\"name\":\"Mikel Arteta\",\"position\":\"Central Midfield\",\"jerseyNumber\":8,\"dateOfBirth\":\"1982-03-26\",\"nationality\":\"Spain\",\"contractUntil\":\"2016-06-30\",\"marketValue\":\"2,500,000 \u20ac\"},{\"id\":1963,\"name\":\"Francis Coquelin\",\"position\":\"Defensive Midfield\",\"jerseyNumber\":34,\"dateOfBirth\":\"1991-05-13\",\"nationality\":\"France\",\"contractUntil\":\"2019-06-30\",\"marketValue\":\"7,000,000 \u20ac\"},{\"id\":1964,\"name\":\"Jack Wilshere\",\"position\":\"Central Midfield\",\"jerseyNumber\":10,\"dateOfBirth\":\"1992-01-01\",\"nationality\":\"England\",\"contractUntil\":\"2018-06-30\",\"marketValue\":\"25,000,000 \u20ac\"},{\"id\":1965,\"name\":\"Aaron Ramsey\",\"position\":\"Central Midfield\",\"jerseyNumber\":16,\"dateOfBirth\":\"1990-12-26\",\"nationality\":\"Wales\",\"contractUntil\":\"2018-06-30\",\"marketValue\":\"28,000,000 \u20ac\"},{\"id\":1967,\"name\":\"Mesut \u00d6zil\",\"position\":\"Attacking Midfield\",\"jerseyNumber\":11,\"dateOfBirth\":\"1988-10-15\",\"nationality\":\"Germany\",\"contractUntil\":\"2018-06-30\",\"marketValue\":\"40,000,000 \u20ac\"},{\"id\":1968,\"name\":\"Tom\u00e1s Rosicky\",\"position\":\"Attacking Midfield\",\"jerseyNumber\":7,\"dateOfBirth\":\"1980-10-04\",\"nationality\":\"Czech Republic\",\"contractUntil\":\"2016-06-30\",\"marketValue\":\"1,500,000 \u20ac\"},{\"id\":1969,\"name\":\"Santi Cazorla\",\"position\":\"Central Midfield\",\"jerseyNumber\":19,\"dateOfBirth\":\"1984-12-13\",\"nationality\":\"Spain\",\"contractUntil\":\"2017-06-30\",\"marketValue\":\"26,000,000 \u20ac\"},{\"id\":1970,\"name\":\"Alex Oxlade-Chamberlain\",\"position\":\"Right Wing\",\"jerseyNumber\":15,\"dateOfBirth\":\"1993-08-15\",\"nationality\":\"England\",\"contractUntil\":\"2018-06-30\",\"marketValue\":\"20,000,000 \u20ac\"},{\"id\":1972,\"name\":\"Alexis S\u00e1nchez\",\"position\":\"Left Wing\",\"jerseyNumber\":17,\"dateOfBirth\":\"1988-12-19\",\"nationality\":\"Chile\",\"contractUntil\":\"2018-06-30\",\"marketValue\":\"55,000,000 \u20ac\"},{\"id\":1973,\"name\":\"Theo Walcott\",\"position\":\"Right Wing\",\"jerseyNumber\":14,\"dateOfBirth\":\"1989-03-16\",\"nationality\":\"England\",\"contractUntil\":\"2019-06-30\",\"marketValue\":\"25,000,000 \u20ac\"},{\"id\":1974,\"name\":\"Joel Campbell\",\"position\":\"Right Wing\",\"jerseyNumber\":28,\"dateOfBirth\":\"1992-06-26\",\"nationality\":\"Costa Rica\",\"contractUntil\":\"2018-06-30\",\"marketValue\":\"5,000,000 \u20ac\"},{\"id\":1975,\"name\":\"Olivier Giroud\",\"position\":\"Centre Forward\",\"jerseyNumber\":12,\"dateOfBirth\":\"1986-09-30\",\"nationality\":\"France\",\"contractUntil\":\"2018-06-30\",\"marketValue\":\"24,000,000 \u20ac\"},{\"id\":1976,\"name\":\"Danny Welbeck\",\"position\":\"Centre Forward\",\"jerseyNumber\":23,\"dateOfBirth\":\"1990-11-26\",\"nationality\":\"England\",\"contractUntil\":\"2019-06-30\",\"marketValue\":\"17,000,000 \u20ac\"},{\"id\":2139,\"name\":\"Petr Cech\",\"position\":\"Keeper\",\"jerseyNumber\":33,\"dateOfBirth\":\"1982-05-20\",\"nationality\":\"Czech Republic\",\"contractUntil\":\"2019-06-30\",\"marketValue\":\"12,000,000 \u20ac\"},{\"id\":2343,\"name\":\"Mohamed Elneny\",\"position\":\"Defensive Midfield\",\"jerseyNumber\":35,\"dateOfBirth\":\"1992-07-11\",\"nationality\":\"Egypt\",\"contractUntil\":\"2020-06-30\",\"marketValue\":\"4,000,000 \u20ac\"},{\"id\":3244,\"name\":\"Gabriel Paulista\",\"position\":\"Centre Back\",\"jerseyNumber\":5,\"dateOfBirth\":\"1990-11-26\",\"nationality\":\"Brazil\",\"contractUntil\":\"2019-06-30\",\"marketValue\":\"12,000,000 \u20ac\"},{\"id\":6502,\"name\":\"Matt Macey\",\"position\":\"Keeper\",\"jerseyNumber\":49,\"dateOfBirth\":\"1994-09-09\",\"nationality\":\"England\",\"contractUntil\":null,\"marketValue\":null},{\"id\":6503,\"name\":\"Alex Iwobi\",\"position\":\"Left Wing\",\"jerseyNumber\":45,\"dateOfBirth\":\"1996-05-03\",\"nationality\":\"Nigeria\",\"contractUntil\":\"2020-06-30\",\"marketValue\":null}]}", "encoding": "UTF-8"}, "headers": {"x-api-version": ["alpha"], "transfer-encoding": ["chunked"], "x-requestcounter-reset": ["55"], "x-response-control": ["full"], "server": ["nginx/1.2.1"], "x-requests-available": ["46"], "connection": ["keep-alive"], "access-control-allow-credentials": ["true"], "date": ["Thu, 14 Apr 2016 23:31:23 GMT"], "access-control-allow-origin": ["*"], "access-control-allow-methods": ["GET"], "content-type": ["application/json;charset=UTF-8"], "x-authenticated-client": ["Archit Verma"]}, "status": {"message": "OK", "code": 200}, "url": "http://api.football-data.org/alpha/teams/57/players"}, "recorded_at": "2016-04-14T23:31:23"}], "recorded_with": "betamax/0.6.0"} \ No newline at end of file diff --git a/tests/cassettes/Team_scores.json b/tests/cassettes/Team_scores.json new file mode 100644 index 0000000..ee09fc8 --- /dev/null +++ b/tests/cassettes/Team_scores.json @@ -0,0 +1 @@ +{"http_interactions": [{"request": {"body": {"string": "", "encoding": "utf-8"}, "headers": {"Connection": ["keep-alive"], "User-Agent": ["python-requests/2.7.0 CPython/2.7.5 Windows/8"], "Accept-Encoding": ["gzip, deflate"], "Accept": ["*/*"], "X-Auth-Token": ["956ca2b3e1fa48e995376ca735f8e5ce"]}, "method": "GET", "uri": "http://api.football-data.org/alpha/teams/57/fixtures?timeFrame=p6"}, "response": {"body": {"string": "{\"_links\":{\"self\":{\"href\":\"http://api.football-data.org/alpha/teams/57/fixtures\"},\"team\":{\"href\":\"http://api.football-data.org/alpha/teams/57\"}},\"timeFrameStart\":\"2016-04-09\",\"timeFrameEnd\":\"2016-04-15\",\"count\":1,\"fixtures\":[{\"_links\":{\"self\":{\"href\":\"http://api.football-data.org/alpha/fixtures/146791\"},\"soccerseason\":{\"href\":\"http://api.football-data.org/alpha/soccerseasons/398\"},\"homeTeam\":{\"href\":\"http://api.football-data.org/alpha/teams/563\"},\"awayTeam\":{\"href\":\"http://api.football-data.org/alpha/teams/57\"}},\"date\":\"2016-04-09T11:45:00Z\",\"status\":\"FINISHED\",\"matchday\":33,\"homeTeamName\":\"West Ham United FC\",\"awayTeamName\":\"Arsenal FC\",\"result\":{\"goalsHomeTeam\":3,\"goalsAwayTeam\":3}}]}", "encoding": "UTF-8"}, "headers": {"x-api-version": ["alpha"], "transfer-encoding": ["chunked"], "x-requestcounter-reset": ["55"], "x-response-control": ["full"], "server": ["nginx/1.2.1"], "x-requests-available": ["45"], "connection": ["keep-alive"], "access-control-allow-credentials": ["true"], "date": ["Thu, 14 Apr 2016 23:31:23 GMT"], "access-control-allow-origin": ["*"], "access-control-allow-methods": ["GET"], "content-type": ["application/json;charset=UTF-8"], "x-authenticated-client": ["Archit Verma"]}, "status": {"message": "OK", "code": 200}, "url": "http://api.football-data.org/alpha/teams/57/fixtures?timeFrame=p6"}, "recorded_at": "2016-04-14T23:31:23"}], "recorded_with": "betamax/0.6.0"} \ No newline at end of file diff --git a/tests/cassettes/Team_upcoming_games.json b/tests/cassettes/Team_upcoming_games.json new file mode 100644 index 0000000..d7437f9 --- /dev/null +++ b/tests/cassettes/Team_upcoming_games.json @@ -0,0 +1 @@ +{"http_interactions": [{"request": {"body": {"string": "", "encoding": "utf-8"}, "headers": {"Connection": ["keep-alive"], "User-Agent": ["python-requests/2.7.0 CPython/2.7.5 Windows/8"], "Accept-Encoding": ["gzip, deflate"], "Accept": ["*/*"], "X-Auth-Token": ["956ca2b3e1fa48e995376ca735f8e5ce"]}, "method": "GET", "uri": "http://api.football-data.org/alpha/teams/57/fixtures?timeFrame=n6"}, "response": {"body": {"string": "{\"_links\":{\"self\":{\"href\":\"http://api.football-data.org/alpha/teams/57/fixtures\"},\"team\":{\"href\":\"http://api.football-data.org/alpha/teams/57\"}},\"timeFrameStart\":\"2016-04-15\",\"timeFrameEnd\":\"2016-04-20\",\"count\":1,\"fixtures\":[{\"_links\":{\"self\":{\"href\":\"http://api.football-data.org/alpha/fixtures/146774\"},\"soccerseason\":{\"href\":\"http://api.football-data.org/alpha/soccerseasons/398\"},\"homeTeam\":{\"href\":\"http://api.football-data.org/alpha/teams/57\"},\"awayTeam\":{\"href\":\"http://api.football-data.org/alpha/teams/354\"}},\"date\":\"2016-04-17T15:00:00Z\",\"status\":\"TIMED\",\"matchday\":34,\"homeTeamName\":\"Arsenal FC\",\"awayTeamName\":\"Crystal Palace FC\",\"result\":{\"goalsHomeTeam\":-1,\"goalsAwayTeam\":-1}}]}", "encoding": "UTF-8"}, "headers": {"x-api-version": ["alpha"], "transfer-encoding": ["chunked"], "x-requestcounter-reset": ["55"], "x-response-control": ["full"], "server": ["nginx/1.2.1"], "x-requests-available": ["43"], "connection": ["keep-alive"], "access-control-allow-credentials": ["true"], "date": ["Thu, 14 Apr 2016 23:31:23 GMT"], "access-control-allow-origin": ["*"], "access-control-allow-methods": ["GET"], "content-type": ["application/json;charset=UTF-8"], "x-authenticated-client": ["Archit Verma"]}, "status": {"message": "OK", "code": 200}, "url": "http://api.football-data.org/alpha/teams/57/fixtures?timeFrame=n6"}, "recorded_at": "2016-04-14T23:31:24"}], "recorded_with": "betamax/0.6.0"} \ No newline at end of file diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..4fcf2bf --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,23 @@ +import base64 +import betamax +import os +import pytest +from betamax_serializers import pretty_json + +betamax.Betamax.register_serializer(pretty_json.PrettyJSONSerializer) + +with betamax.Betamax.configure() as config: + record_mode = 'once' + + config.cassette_library_dir = 'tests/cassettes' + config.default_cassette_options['record_mode'] = record_mode + #config.default_cassette_options['serialize_with'] = 'prettyjson' + + +@pytest.fixture +def betamax_simple_body(request): + """Return configuration to match cassette on uri, method and body.""" + request.cls.betamax_simple_body = { + 'match_requests_on': ['uri', 'method', 'body'] + } + diff --git a/tests/helper.py b/tests/helper.py new file mode 100644 index 0000000..1d9f616 --- /dev/null +++ b/tests/helper.py @@ -0,0 +1,32 @@ +import betamax +import os +import sys +import pytest +import unittest +import soccer.main +import soccer.writers +from click.testing import CliRunner + + +@pytest.mark.usefixtures('betamax_simple_body') +class IntegrationHelper(unittest.TestCase): + def setUp(self): + self.session = soccer.main.session + self.recorder = betamax.Betamax(self.session) + + self.cli_runner = CliRunner() + self.writer = soccer.writers.get_writer('stdout') + soccer.main.headers['X-Auth-Token'] = soccer.main.load_config_key() + + def cassette_name(self, method, cls=None): + class_name = cls or self.described_class + return '_'.join([class_name, method]) + + def get_output(self, out): + sys.stdout.flush() + return out.getvalue() + + @property + def described_class(self): + class_name = self.__class__.__name__ + return class_name[4:] diff --git a/tests/integration/__init__.py b/tests/integration/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/integration/test_league.py b/tests/integration/test_league.py new file mode 100644 index 0000000..c75e005 --- /dev/null +++ b/tests/integration/test_league.py @@ -0,0 +1,16 @@ +import unittest +import soccer.main +import soccer.writers +from ..helper import IntegrationHelper + +class TestLeague(IntegrationHelper): + def test_team_scores(self): + cassette_name = self.cassette_name('standings') + with self.recorder.use_cassette(cassette_name): + with self.cli_runner.isolation() as runner: + league = 'EPL' + soccer.main.get_standings(league, self.writer) + header, first, second, _ = self.get_output(runner).split('\n', 3) + assert 'POS CLUB PLAYED GOAL DIFF POINTS ' == header + assert '1 Leicester City FC 33 26 72' == first + assert '2 Tottenham Hotspur FC 33 35 65' == second diff --git a/tests/integration/test_scores.py b/tests/integration/test_scores.py new file mode 100644 index 0000000..43fc090 --- /dev/null +++ b/tests/integration/test_scores.py @@ -0,0 +1,26 @@ +import unittest +import soccer.main +import soccer.writers +from ..helper import IntegrationHelper + +class TestScores(IntegrationHelper): + def test_live(self): + return + # TODO: When there are live games + cassette_name = self.cassette_name('live_games') + with self.recorder.use_cassette(cassette_name): + with self.cli_runner.isolation() as out: + soccer.main.get_live_scores(self.writer, False) + + def test_specific_league(self): + cassette_name = self.cassette_name('specific_league') + with self.recorder.use_cassette(cassette_name): + with self.cli_runner.isolation() as runner: + league = 'EPL' + time = 6 + show_upcoming = False + use_12_hour = False + soccer.main.get_league_scores(league, time, self.writer, show_upcoming, use_12_hour) + output = self.get_output(runner) + assert '============================ EPL =============================' == output.split("\n", 1)[0] + assert 'West Ham United FC 3 vs 3 Arsenal FC' in output diff --git a/tests/integration/test_team.py b/tests/integration/test_team.py new file mode 100644 index 0000000..ad3104e --- /dev/null +++ b/tests/integration/test_team.py @@ -0,0 +1,62 @@ +# -*- coding: utf-8 -*- +import unittest +import soccer.main +import soccer.writers +from ..helper import IntegrationHelper + +class TestTeam(IntegrationHelper): + def test_team_scores(self): + cassette_name = self.cassette_name('scores') + with self.recorder.use_cassette(cassette_name): + with self.cli_runner.isolation() as runner: + team = 'AFC' + time = 6 + use12hour = True + upcoming = False + soccer.main.get_team_scores(team, time, self.writer, upcoming, use12hour) + assert self.get_output(runner) == '\n2016-04-09\tWest Ham United FC 3 vs 3 Arsenal FC\n' + + def test_incorrect_team(self): + cassette_name = self.cassette_name('invalid') + with self.recorder.use_cassette(cassette_name): + with self.cli_runner.isolation() as runner: + team = 'INVALIDTEAM' + time = 6 + use12hour = True + upcoming = False + soccer.main.get_team_scores(team, time, self.writer, upcoming, use12hour) + assert self.get_output(runner) == 'Team code is not correct.\n' + + def test_team_upcoming_games_with_utc_time(self): + cassette_name = self.cassette_name('upcoming_games') + with self.recorder.use_cassette(cassette_name): + with self.cli_runner.isolation() as runner: + team = 'AFC' + time = 6 + use12hour = False + upcoming = True + soccer.main.get_team_scores(team, time, self.writer, upcoming, use12hour) + assert self.get_output(runner) == '\nArsenal FC - vs - Crystal Palace FC Sun 17, 08:00\n' + + def test_team_upcoming_games_with_local_time(self): + cassette_name = self.cassette_name('upcoming_games') + with self.recorder.use_cassette(cassette_name): + with self.cli_runner.isolation() as runner: + team = 'AFC' + time = 6 + use12hour = True + upcoming = True + soccer.main.get_team_scores(team, time, self.writer, upcoming, use12hour) + assert self.get_output(runner) == '\nArsenal FC - vs - Crystal Palace FC Sun 17, 08:00 AM\n' + + def test_team_player_names(self): + cassette_name = self.cassette_name('player_names') + with self.recorder.use_cassette(cassette_name): + with self.cli_runner.isolation() as runner: + team = 'AFC' + soccer.main.get_team_players(team, self.writer) + output = self.get_output(runner) + assert 'N. NAME POSITION NATIONALITY BIRTHDAY MARKET VALUE' in output + assert u'11 Mesut Özil Attacking Midfield Germany 1988-10-15 40,000,000' in unicode(output, 'utf-8') + + diff --git a/tests/test.py b/tests/test.py deleted file mode 100644 index 10e6159..0000000 --- a/tests/test.py +++ /dev/null @@ -1 +0,0 @@ -import unittest