File size: 844 Bytes
8a8cb44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from tutor.services.cache import FileCache


def test_key_is_stable_and_separator_safe() -> None:
    assert FileCache.key("a", "b") == FileCache.key("a", "b")
    assert FileCache.key("a", "b") != FileCache.key("ab")
    assert FileCache.key("a", "b") != FileCache.key("a:b")


def test_roundtrip_and_miss(tmp_path) -> None:
    cache = FileCache(tmp_path / "nested" / "dir")  # created on init
    key = FileCache.key("text", "1", "model", "B1", "the ocean")
    assert cache.get(key) is None
    cache.set(key, {"text": "héllo", "attempts": 2})
    assert cache.get(key) == {"text": "héllo", "attempts": 2}


def test_corrupt_entry_behaves_like_a_miss(tmp_path) -> None:
    cache = FileCache(tmp_path)
    key = FileCache.key("x")
    (tmp_path / f"{key}.json").write_text("{not json", encoding="utf-8")
    assert cache.get(key) is None