29 lines
757 B
Python
29 lines
757 B
Python
import time
|
|
|
|
from app.cache import FileCache
|
|
|
|
|
|
def test_cache_returns_fresh_entry(tmp_path) -> None:
|
|
cache = FileCache(cache_dir=str(tmp_path), default_ttl_seconds=10)
|
|
cache.set_json("sample", {"x": 1})
|
|
|
|
entry = cache.get_json("sample")
|
|
|
|
assert entry is not None
|
|
assert entry.stale is False
|
|
assert entry.value == {"x": 1}
|
|
|
|
|
|
def test_cache_can_serve_stale_when_requested(tmp_path) -> None:
|
|
cache = FileCache(cache_dir=str(tmp_path), default_ttl_seconds=0)
|
|
cache.set_json("sample", {"x": 1})
|
|
time.sleep(0.01)
|
|
|
|
fresh = cache.get_json("sample")
|
|
stale = cache.get_json("sample", allow_stale=True)
|
|
|
|
assert fresh is None
|
|
assert stale is not None
|
|
assert stale.stale is True
|
|
assert stale.value == {"x": 1}
|