Spaces:
Running on T4
Running on T4
Download AI-coach-agent/tests/test_cache.py from modventures/baseball-engine: direct link, hf CLI and curl.
- Browser
- Download file 3.03 kB
-
https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/AI-coach-agent/tests/test_cache.py
- Command line
-
hf download hf://spaces/modventures/baseball-engine/AI-coach-agent/tests/test_cache.py
-
curl -L -o test_cache.py https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/AI-coach-agent/tests/test_cache.py
3.03 kB
| import sqlite3 | |
| from ai_coach.cache import ResponseCache, cache_key | |
| from .helpers import good_note | |
| def test_cache_key_depends_on_version_model_and_input(): | |
| base = cache_key("v1", "m", "{}") | |
| assert base == cache_key("v1", "m", "{}") | |
| assert len({base, cache_key("v2", "m", "{}"), cache_key("v1", "n", "{}"), cache_key("v1", "m", '{"a":1}')}) == 4 | |
| def test_cache_roundtrip_ttl_and_purge(tmp_path): | |
| cache = ResponseCache(tmp_path / "c.sqlite3", ttl_days=1) | |
| note = good_note() | |
| assert cache.get("k") is None | |
| cache.put("k", note, now=1000.0) | |
| assert cache.get("k", now=1000.0 + 3600) == note | |
| assert cache.get("k", now=1000.0 + 86_401) is None # expired | |
| cache.put("fresh", note, now=2_000_000.0) | |
| assert cache.purge_expired(now=2_000_000.0 + 10) == 1 # only the old one goes | |
| assert cache.get("fresh", now=2_000_000.0 + 10) == note | |
| def test_cache_survives_reopening(tmp_path): | |
| path = tmp_path / "c.sqlite3" | |
| ResponseCache(path, ttl_days=5).put("k", good_note()) | |
| assert ResponseCache(path, ttl_days=5).get("k") == good_note() | |
| def test_a_row_that_no_longer_matches_the_schema_is_a_miss_and_is_dropped(tmp_path): | |
| path = tmp_path / "c.sqlite3" | |
| cache = ResponseCache(path, ttl_days=5) | |
| cache.put("k", good_note()) | |
| with sqlite3.connect(path) as db: | |
| db.execute("UPDATE notes SET note = ? WHERE key = 'k'", ('{"unexpected": true}',)) | |
| assert cache.get("k") is None | |
| with sqlite3.connect(path) as db: | |
| assert db.execute("SELECT COUNT(*) FROM notes").fetchone()[0] == 0 | |
| def test_an_unusable_cache_location_degrades_to_no_cache(tmp_path): | |
| blocker = tmp_path / "file" | |
| blocker.write_text("not a directory") | |
| cache = ResponseCache(blocker / "c.sqlite3", ttl_days=5) # parent is a file: cannot be created | |
| cache.put("k", good_note()) # must not raise | |
| assert cache.get("k") is None and cache.purge_expired() == 0 | |
| cache.delete("k") | |
| def test_failure_cache_remembers_briefly_and_expires(tmp_path): | |
| cache = ResponseCache(tmp_path / "c.sqlite3", ttl_days=1) | |
| assert cache.get_failure("k") is None | |
| cache.put_failure("k", "guardrails_failed", ttl_s=300, now=1000.0) | |
| assert cache.get_failure("k", now=1100.0) == "guardrails_failed" | |
| assert cache.get_failure("k", now=1301.0) is None # expired | |
| cache.put_failure("off", "x", ttl_s=0, now=1000.0) | |
| assert cache.get_failure("off", now=1000.0) is None # a zero TTL stores nothing | |
| assert cache.get("k", now=1100.0) is None # a failure is never served as a note | |
| def test_purge_removes_expired_failures_and_survives_reopening(tmp_path): | |
| path = tmp_path / "c.sqlite3" | |
| cache = ResponseCache(path, ttl_days=1) | |
| cache.put_failure("old", "llm_error", ttl_s=10, now=1000.0) | |
| cache.put_failure("new", "llm_error", ttl_s=10_000, now=1000.0) | |
| cache.purge_expired(now=5000.0) | |
| assert ResponseCache(path, ttl_days=1).get_failure("new", now=5000.0) == "llm_error" | |
| assert ResponseCache(path, ttl_days=1).get_failure("old", now=1005.0) is None | |