`is True` strict identity fails if the NHL API returns an integer 1 instead of a JSON boolean. A truthy check is safe here since the Intermission field is always False/0 for non-intermission live games. Also adds a test that verifies intermission games are separated from live games in the /scoreboard response. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
89 lines
3.2 KiB
Python
89 lines
3.2 KiB
Python
import json
|
|
|
|
from tests.conftest import make_game
|
|
|
|
|
|
class TestIndexRoute:
|
|
def test_returns_200(self, flask_client):
|
|
response = flask_client.get("/")
|
|
assert response.status_code == 200
|
|
|
|
def test_returns_html(self, flask_client):
|
|
response = flask_client.get("/")
|
|
assert b"NHL Scoreboard" in response.data
|
|
|
|
|
|
class TestScoreboardRoute:
|
|
def test_returns_200(self, flask_client):
|
|
response = flask_client.get("/scoreboard")
|
|
assert response.status_code == 200
|
|
|
|
def test_returns_json_with_expected_keys(self, flask_client):
|
|
response = flask_client.get("/scoreboard")
|
|
data = json.loads(response.data)
|
|
assert "live_games" in data
|
|
assert "intermission_games" in data
|
|
assert "pre_games" in data
|
|
assert "final_games" in data
|
|
|
|
def test_intermission_games_separated_from_live(
|
|
self, flask_client, monkeypatch, tmp_path
|
|
):
|
|
import json as _json
|
|
import app.routes as routes
|
|
|
|
intermission_game = make_game(in_intermission=True)
|
|
live_game = make_game(home_name="Oilers", away_name="Flames")
|
|
scoreboard = {"games": [intermission_game, live_game]}
|
|
|
|
f = tmp_path / "scoreboard_data.json"
|
|
f.write_text(_json.dumps(scoreboard))
|
|
monkeypatch.setattr(routes, "SCOREBOARD_DATA_FILE", str(f))
|
|
|
|
data = _json.loads(flask_client.get("/scoreboard").data)
|
|
|
|
assert len(data["intermission_games"]) == 1
|
|
assert data["intermission_games"][0]["Intermission"] is True
|
|
assert len(data["live_games"]) == 1
|
|
assert data["live_games"][0]["Intermission"] is False
|
|
|
|
def test_live_games_have_required_fields(self, flask_client):
|
|
response = flask_client.get("/scoreboard")
|
|
data = json.loads(response.data)
|
|
if data["live_games"]:
|
|
game = data["live_games"][0]
|
|
assert "Home Team" in game
|
|
assert "Away Team" in game
|
|
assert "Home Score" in game
|
|
assert "Away Score" in game
|
|
assert "Game State" in game
|
|
assert game["Game State"] == "LIVE"
|
|
|
|
def test_missing_file_returns_error(self, flask_client, monkeypatch):
|
|
import app.routes as routes
|
|
|
|
monkeypatch.setattr(routes, "SCOREBOARD_DATA_FILE", "/nonexistent/path.json")
|
|
response = flask_client.get("/scoreboard")
|
|
data = json.loads(response.data)
|
|
assert "error" in data
|
|
|
|
def test_invalid_json_returns_error(self, flask_client, monkeypatch, tmp_path):
|
|
import app.routes as routes
|
|
|
|
bad_file = tmp_path / "bad.json"
|
|
bad_file.write_text("not valid json {{{")
|
|
monkeypatch.setattr(routes, "SCOREBOARD_DATA_FILE", str(bad_file))
|
|
response = flask_client.get("/scoreboard")
|
|
data = json.loads(response.data)
|
|
assert "error" in data
|
|
|
|
def test_null_json_returns_error(self, flask_client, monkeypatch, tmp_path):
|
|
import app.routes as routes
|
|
|
|
null_file = tmp_path / "null.json"
|
|
null_file.write_text("null")
|
|
monkeypatch.setattr(routes, "SCOREBOARD_DATA_FILE", str(null_file))
|
|
response = flask_client.get("/scoreboard")
|
|
data = json.loads(response.data)
|
|
assert "error" in data
|