make caching opt-in
Some checks failed
ci / deploy-coolify (push) Has been cancelled
ci / push-image (push) Has been cancelled
ci / test (push) Successful in 7s

This commit is contained in:
Space-Banane
2026-05-29 19:45:02 +02:00
parent 4f0ef035cc
commit d2f0dc646a
6 changed files with 103 additions and 58 deletions

View File

@@ -1,28 +1,58 @@
import time
import tempfile
from pathlib import Path
from app.cache import FileCache
from app.main import get_cache
from app.settings import Settings
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})
def test_cache_returns_fresh_entry() -> None:
with tempfile.TemporaryDirectory(dir=Path.cwd()) as temp_dir:
cache = FileCache(cache_dir=temp_dir, default_ttl_seconds=10)
cache.set_json("sample", {"x": 1})
entry = cache.get_json("sample")
entry = cache.get_json("sample")
assert entry is not None
assert entry.stale is False
assert entry.value == {"x": 1}
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)
def test_cache_can_serve_stale_when_requested() -> None:
with tempfile.TemporaryDirectory(dir=Path.cwd()) as temp_dir:
cache = FileCache(cache_dir=temp_dir, 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)
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}
assert fresh is None
assert stale is not None
assert stale.stale is True
assert stale.value == {"x": 1}
def test_cache_is_disabled_when_not_configured() -> None:
settings = Settings(
_env_file=None,
GITHUB_USERNAME="octocat",
GITEA_BASE_URL="https://gitea.example.com",
GITEA_USERNAME="octocat",
)
cache = get_cache(settings)
assert cache is None
def test_cache_works_in_workspace_tempdir() -> None:
with tempfile.TemporaryDirectory(dir=Path.cwd()) as temp_dir:
cache = FileCache(cache_dir=temp_dir, default_ttl_seconds=10)
cache.set_json("sample", {"x": 1})
entry = cache.get_json("sample")
assert entry is not None
assert entry.value == {"x": 1}

View File

@@ -1,3 +1,5 @@
import tempfile
from pathlib import Path
from datetime import date
from fastapi.testclient import TestClient
@@ -6,30 +8,31 @@ from app.main import ActivityResult, app
from app.settings import get_settings
def test_activity_svg_returns_svg_content_type(monkeypatch, tmp_path) -> None:
def test_activity_svg_returns_svg_content_type(monkeypatch) -> None:
monkeypatch.setenv("GITHUB_USERNAME", "octocat")
monkeypatch.setenv("GITEA_BASE_URL", "https://gitea.example.com")
monkeypatch.setenv("GITEA_USERNAME", "octocat")
monkeypatch.setenv("CACHE_DIR", str(tmp_path))
get_settings.cache_clear()
with tempfile.TemporaryDirectory(dir=Path.cwd()) as temp_dir:
monkeypatch.setenv("CACHE_DIR", temp_dir)
get_settings.cache_clear()
async def fake_collect_merged_activity(settings, cache, options):
return ActivityResult(
merged={
"2026-01-01": {"github": 1, "gitea": 2, "total": 3},
"2026-01-02": {"github": 0, "gitea": 0, "total": 0},
},
stale=False,
from_date=date(2026, 1, 1),
to_date=date(2026, 1, 2),
days_count=2,
)
async def fake_collect_merged_activity(settings, cache, options):
return ActivityResult(
merged={
"2026-01-01": {"github": 1, "gitea": 2, "total": 3},
"2026-01-02": {"github": 0, "gitea": 0, "total": 0},
},
stale=False,
from_date=date(2026, 1, 1),
to_date=date(2026, 1, 2),
days_count=2,
)
monkeypatch.setattr("app.main.collect_merged_activity", fake_collect_merged_activity)
monkeypatch.setattr("app.main.collect_merged_activity", fake_collect_merged_activity)
client = TestClient(app)
response = client.get("/activity.svg")
client = TestClient(app)
response = client.get("/activity.svg")
assert response.status_code == 200
assert response.headers["content-type"].startswith("image/svg+xml")
assert "<svg" in response.text
assert response.status_code == 200
assert response.headers["content-type"].startswith("image/svg+xml")
assert "<svg" in response.text