first commit
This commit is contained in:
28
tests/test_cache.py
Normal file
28
tests/test_cache.py
Normal file
@@ -0,0 +1,28 @@
|
||||
import time
|
||||
|
||||
from app.cache import FileCache
|
||||
|
||||
|
||||
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})
|
||||
|
||||
entry = cache.get_json("sample")
|
||||
|
||||
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)
|
||||
|
||||
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}
|
||||
22
tests/test_merge.py
Normal file
22
tests/test_merge.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from app.merge import filter_activity_source, merge_activity
|
||||
|
||||
|
||||
def test_merge_activity_combines_totals() -> None:
|
||||
github = {"2026-01-01": 2, "2026-01-02": 1}
|
||||
gitea = {"2026-01-02": 3, "2026-01-03": 4}
|
||||
|
||||
merged = merge_activity(github, gitea)
|
||||
|
||||
assert merged["2026-01-01"] == {"github": 2, "gitea": 0, "total": 2}
|
||||
assert merged["2026-01-02"] == {"github": 1, "gitea": 3, "total": 4}
|
||||
assert merged["2026-01-03"] == {"github": 0, "gitea": 4, "total": 4}
|
||||
|
||||
|
||||
def test_filter_activity_source_github_only() -> None:
|
||||
merged = {
|
||||
"2026-01-01": {"github": 2, "gitea": 7, "total": 9},
|
||||
}
|
||||
|
||||
filtered = filter_activity_source(merged, "github")
|
||||
|
||||
assert filtered["2026-01-01"] == {"github": 2, "gitea": 0, "total": 2}
|
||||
35
tests/test_routes.py
Normal file
35
tests/test_routes.py
Normal file
@@ -0,0 +1,35 @@
|
||||
from datetime import date
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from app.main import ActivityResult, app
|
||||
from app.settings import get_settings
|
||||
|
||||
|
||||
def test_activity_svg_returns_svg_content_type(monkeypatch, tmp_path) -> 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()
|
||||
|
||||
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)
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user