"""Tests for the best-effort API cache invalidation signal.""" from __future__ import annotations import sys import opengoods.cache as cache def test_disabled_when_no_url(monkeypatch): monkeypatch.delenv("OPENGOODS_REDIS_URL", raising=False) assert cache.default_redis_url() is None assert cache.bump_cache_epoch() is False def test_bump_fails_open_when_unreachable(monkeypatch): # An unroutable URL must not raise; the run still succeeds. monkeypatch.setenv("OPENGOODS_REDIS_URL", "redis://127.0.0.1:1/0") assert cache.bump_cache_epoch() is False def test_bump_increments_epoch(monkeypatch): calls = {} class FakeClient: def incr(self, key): calls["key"] = key return 7 def close(self): calls["closed"] = True class FakeRedis: @staticmethod def from_url(url, **kwargs): calls["url"] = url return FakeClient() monkeypatch.setitem(sys.modules, "redis", type("M", (), {"Redis": FakeRedis})) assert cache.bump_cache_epoch("redis://example:6379/0") is True assert calls["key"] == "og:cache:epoch" assert calls["url"] == "redis://example:6379/0" assert calls["closed"] is True