37 lines
1.2 KiB
Python
37 lines
1.2 KiB
Python
from pathlib import Path
|
|
|
|
|
|
def test_competitor_intel_cache_reuses_memory_and_shared_file(tmp_path, monkeypatch):
|
|
from services import competitor_intel_repository as repo
|
|
|
|
monkeypatch.setattr(repo, "_CACHE_FILE", Path(tmp_path) / "competitor_intel_cache.pkl")
|
|
repo._MEM_CACHE.clear()
|
|
|
|
calls = {"count": 0}
|
|
|
|
def producer():
|
|
calls["count"] += 1
|
|
return {"valid_matches": 7, "match_rate": 0.1}
|
|
|
|
first = repo._cached_payload("coverage:test", producer, ttl_seconds=60)
|
|
second = repo._cached_payload("coverage:test", producer, ttl_seconds=60)
|
|
repo._MEM_CACHE.clear()
|
|
third = repo._cached_payload("coverage:test", producer, ttl_seconds=60)
|
|
|
|
assert first == second == third == {"valid_matches": 7, "match_rate": 0.1}
|
|
assert calls["count"] == 1
|
|
|
|
|
|
def test_clear_competitor_intel_cache_removes_shared_file(tmp_path, monkeypatch):
|
|
from services import competitor_intel_repository as repo
|
|
|
|
cache_file = Path(tmp_path) / "competitor_intel_cache.pkl"
|
|
monkeypatch.setattr(repo, "_CACHE_FILE", cache_file)
|
|
repo._MEM_CACHE["x"] = {"time": 1, "value": {"ok": True}}
|
|
cache_file.write_bytes(b"stale")
|
|
|
|
repo.clear_competitor_intel_cache()
|
|
|
|
assert repo._MEM_CACHE == {}
|
|
assert not cache_file.exists()
|