from unittest.mock import patch import pytest from app.core import cache from app.models.issue import IssueReport from app.models.report import EngineeringReport from app.models.repository import RepositoryMetadata from app.models.review import GeneratedTests, ReviewSuggestions class FakeRedis: """Minimal in-memory stand-in for redis.Redis(decode_responses=True).""" def __init__(self) -> None: self._store: dict[str, str] = {} def get(self, key: str) -> str | None: return self._store.get(key) def set(self, key: str, value: str, ex: int | None = None) -> None: self._store[key] = value @pytest.fixture(autouse=True) def clear_cache_client() -> None: cache._clear_redis_client_cache() yield cache._clear_redis_client_cache() def _fake_report() -> EngineeringReport: return EngineeringReport( repository=RepositoryMetadata( url="https://github.com/owner/repo", name="repo", local_path="/tmp/fake", language="Python", frameworks=[], architecture="", entry_points=[], summary="", ), issues=IssueReport(), review=ReviewSuggestions(summary="ok", overall_score=8.0), tests=GeneratedTests(), full_report="# Report", generated_at="2026-06-15T00:00:00Z", ) def test_cache_round_trip() -> None: fake = FakeRedis() with patch("app.core.cache.get_redis_client", return_value=fake): cache.set_cached_report("https://github.com/owner/repo", "abc123", _fake_report()) result = cache.get_cached_report("https://github.com/owner/repo", "abc123") assert result is not None assert result.full_report == "# Report" def test_cache_miss_returns_none() -> None: fake = FakeRedis() with patch("app.core.cache.get_redis_client", return_value=fake): result = cache.get_cached_report("https://github.com/owner/repo", "missing-sha") assert result is None def test_cache_read_failure_returns_none() -> None: with patch("app.core.cache.get_redis_client", side_effect=ConnectionError("redis down")): result = cache.get_cached_report("https://github.com/owner/repo", "abc123") assert result is None def test_cache_write_failure_is_silent() -> None: with patch("app.core.cache.get_redis_client", side_effect=ConnectionError("redis down")): cache.set_cached_report("https://github.com/owner/repo", "abc123", _fake_report())