"""Cache-through behaviour: the second read of the day must not hit the network.""" from __future__ import annotations import pytest from wodify import service, store @pytest.fixture() def conn(tmp_path, monkeypatch): monkeypatch.setenv("WODIFY_DB", str(tmp_path / "t.db")) c = store.connect() yield c c.close() SCHEDULE = {"Response": {"ResponseClassList": {"Class": {"List": [ {"Id": "190917816", "Name": "CrossFit: 5:30 PM", "StartTime": "17:30:00", "EndTime": "18:30:00", "StartDate": "2026-07-20", "ProgramId": "133059", "ClassLimit": 10, "ReservationCount": 9, "WaitlistCount": 0, "Coaches": {"List": [{"Name": "Pat Doyle"}]}}, ]}}}} def test_first_call_hits_network_second_uses_cache(conn, monkeypatch): calls = [] def fake_call(key, overrides=None, **kw): calls.append(key) return SCHEDULE monkeypatch.setattr(service.client, "call", fake_call) rows, first = service.day_classes(conn, "2026-07-20") assert first.source == "network" and len(rows) == 1 assert calls == ["schedule"] rows2, second = service.day_classes(conn, "2026-07-20") assert second.source == "cache" and second.cached assert calls == ["schedule"], "second read must not touch the network" assert rows2 == rows def test_refresh_forces_a_network_read(conn, monkeypatch): calls = [] monkeypatch.setattr(service.client, "call", lambda k, o=None, **kw: calls.append(k) or SCHEDULE) service.day_classes(conn, "2026-07-20") service.day_classes(conn, "2026-07-20", refresh=True) assert len(calls) == 2 def test_day_classes_indexes_ids_for_later_lookup(conn, monkeypatch): monkeypatch.setattr(service.client, "call", lambda k, o=None, **kw: SCHEDULE) service.day_classes(conn, "2026-07-20") klass = service.resolve_class(conn, "190917816") assert klass["date"] == "2026-07-20" assert klass["program_id"] == "133059" def test_cache_key_separates_dates(conn, monkeypatch): seen = [] monkeypatch.setattr(service.client, "call", lambda k, o=None, **kw: seen.append(dict(o or {})) or SCHEDULE) service.day_classes(conn, "2026-07-20") service.day_classes(conn, "2026-07-21") assert [s["FromDate"] for s in seen] == ["2026-07-20", "2026-07-21"] def test_workout_cache_key_separates_programs(conn, monkeypatch): seen = [] monkeypatch.setattr(service.client, "call", lambda k, o=None, **kw: seen.append(dict(o or {})) or {"x": 1}) service.workout(conn, "2026-07-20", "133059") service.workout(conn, "2026-07-20", "131311") service.workout(conn, "2026-07-20", "133059") # cached assert [s.get("GymProgramId") for s in seen] == ["133059", "131311"] def test_attendees_raises_maxrecords_for_large_classes(conn, monkeypatch): seen = {} monkeypatch.setattr(service.client, "call", lambda k, o=None, **kw: seen.update(o or {}) or {"x": 1}) service.attendees(conn, "190918189", 30) assert seen["MaxRecords"] == 30, "must not silently truncate a large class" def test_attendees_cached_per_class(conn, monkeypatch): calls = [] monkeypatch.setattr(service.client, "call", lambda k, o=None, **kw: calls.append(o) or {"x": 1}) service.attendees(conn, "1", 10) service.attendees(conn, "1", 10) service.attendees(conn, "2", 10) assert len(calls) == 2 def test_resolve_unknown_class_returns_none(conn): assert service.resolve_class(conn, "999") is None