This commit is contained in:
@@ -147,6 +147,34 @@ def test_growth_cache_shared_file_roundtrip(tmp_path, monkeypatch):
|
||||
assert shared_cache.exists()
|
||||
|
||||
|
||||
def test_growth_cache_reuses_expired_ttl_when_source_fingerprint_matches(tmp_path, monkeypatch):
|
||||
import pickle
|
||||
from datetime import timedelta
|
||||
from services import cache_service
|
||||
|
||||
shared_cache = tmp_path / "growth_analysis_cache.pkl"
|
||||
monkeypatch.setattr(cache_service, "_GROWTH_SHARED_CACHE_FILE", shared_cache)
|
||||
cache_service.clear_growth_cache()
|
||||
cache_service.set_growth_cache(
|
||||
{"labels": ["2026-05"], "revenue": [1000]},
|
||||
{"ytd_revenue": 1000},
|
||||
source_fingerprint=("2026-05-17", 10),
|
||||
)
|
||||
|
||||
stale_timestamp = (
|
||||
cache_service.datetime.now(cache_service.TAIPEI_TZ)
|
||||
- timedelta(seconds=cache_service._GROWTH_CACHE_TTL + 60)
|
||||
)
|
||||
cache_service._GROWTH_ANALYSIS_CACHE["timestamp"] = stale_timestamp
|
||||
payload = pickle.loads(shared_cache.read_bytes())
|
||||
payload["timestamp"] = stale_timestamp
|
||||
shared_cache.write_bytes(pickle.dumps(payload, protocol=pickle.HIGHEST_PROTOCOL))
|
||||
|
||||
assert cache_service.is_growth_cache_valid(("2026-05-17", 10))
|
||||
assert not cache_service.is_growth_cache_valid(("2026-05-18", 10))
|
||||
assert not cache_service.is_growth_cache_valid()
|
||||
|
||||
|
||||
def test_clear_growth_cache_removes_shared_file(tmp_path, monkeypatch):
|
||||
from services import cache_service
|
||||
|
||||
|
||||
Reference in New Issue
Block a user