diff --git a/scripts/tests/test_backfill_discover.py b/scripts/tests/test_backfill_discover.py new file mode 100644 index 00000000..df70ebb2 --- /dev/null +++ b/scripts/tests/test_backfill_discover.py @@ -0,0 +1,26 @@ +""" +The backfill buckets each search row by its release date as the store search +writes it (asked with l=english, so the month names are English). + +Run from the repository root: python -m pytest scripts/tests +""" +import pytest + +from backfill_discover import parse_released + + +@pytest.mark.parametrize("text, expected", [ + ("21 Aug, 2012", (2012, 8)), + ("Aug 21, 2012", (2012, 8)), + ("Sep 2024", (2024, 9)), + # A year alone sorts with the year's start. + ("2025", (2025, 1)), + # Not dates: kept out of every window. + ("Xyz 2024", None), + ("Q1 2026", None), + ("Coming soon", None), + ("", None), + (None, None), +]) +def test_release_rows_are_bucketed_by_year_and_month(text, expected): + assert parse_released(text) == expected diff --git a/scripts/tests/test_leaderboards.py b/scripts/tests/test_leaderboards.py index c28cc602..4314ed22 100644 --- a/scripts/tests/test_leaderboards.py +++ b/scripts/tests/test_leaderboards.py @@ -74,3 +74,17 @@ def test_update_data_refills_only_incomplete_records(repo): repo.setattr(fetcher, "fetch_full", lambda g, client=None, guard=None: refilled.append(g["link"])) update_data.main() assert len(refilled) == 3 # the fixture's records lack most store fields + + +@pytest.mark.parametrize("module", [top_online, top_offline]) +@pytest.mark.parametrize("reviews, stars", [ + ("96% (Overwhelmingly Positive)", "★★★★★"), + ("72% (Mostly Positive)", "★★★☆☆"), + ("12% (Overwhelmingly Negative)", "★☆☆☆☆"), + # Never read, no reviews, or no field at all: no stars, and no crash. + ("N/A", "—"), + ("No reviews", "—"), + (None, "—"), +]) +def test_stars_come_from_the_percentage_and_nothing_else(module, reviews, stars): + assert module._stars(reviews) == stars diff --git a/scripts/tests/test_mark_dead_games.py b/scripts/tests/test_mark_dead_games.py new file mode 100644 index 00000000..d4309ed6 --- /dev/null +++ b/scripts/tests/test_mark_dead_games.py @@ -0,0 +1,22 @@ +""" +mark_dead_games.py compares player counts, which the dataset stores as +formatted strings ("492,197"), never as numbers (CLAUDE.md). + +Run from the repository root: python -m pytest scripts/tests +""" +import pytest + +from mark_dead_games import parse_players + + +@pytest.mark.parametrize("stored, players", [ + ("492,197", 492197), + ("0", 0), + (12, 12), + (3.0, 3), + (None, 0), + ("N/A", 0), + ("", 0), +]) +def test_players_are_read_from_the_stored_string(stored, players): + assert parse_players(stored) == players diff --git a/scripts/tests/test_steam_client.py b/scripts/tests/test_steam_client.py index 2a39802c..ff75b922 100644 --- a/scripts/tests/test_steam_client.py +++ b/scripts/tests/test_steam_client.py @@ -181,3 +181,37 @@ def test_tag_names_are_fetched_once(): assert client.fetch_tag_names() == {12095: "Sexual Content"} assert len(session.requests) == 1 assert session.requests[0]["params"] == {"language": "english"} + + +APPDETAILS = "https://store.steampowered.com/api/appdetails" + + +class _NotJson(_Resp): + def json(self): + raise ValueError("Expecting value") + + +@pytest.mark.parametrize("answer, expected", [ + # A 403 is no answer at all: _get gives up on it and hands back nothing. + (_Resp(APPDETAILS, status_code=403), ("network_error", None)), + (_Resp(APPDETAILS, status_code=404), ("not_found", None)), + # What purge asks again before it believes it (RECHECK_AFTER). + (_Resp(APPDETAILS, body={"10": {"success": False}}), ("unavailable", None)), + (_NotJson(APPDETAILS), ("network_error", None)), +]) +def test_appdetails_tells_its_failures_apart(answer, expected): + client, _ = _client(answer) + assert client.fetch_app_details_full("10") == expected + + +def test_a_store_page_nobody_answered_is_a_network_error(): + client, _ = _client(_Resp(APP, status_code=403)) + assert client.fetch_store_page_full("2279510") == ("network_error", None) + + +def test_a_store_page_answer_neither_200_nor_404_is_a_network_error(monkeypatch): + # _get hands back only a 200 or a 404/410 today; the page does not lean on that. + client, _ = _client() + # SteamClient has __slots__: the method is replaced on the class, for this test only. + monkeypatch.setattr(SteamClient, "_get", lambda self, *args, **kwargs: _Resp(APP, status_code=302)) + assert client.fetch_store_page_full("2279510") == ("network_error", None)