Harden multi-user activity fetching
ci / deploy-coolify (push) Successful in 7s
ci / test (push) Successful in 13s
ci / push-image (push) Successful in 55s

This commit is contained in:
Space-Banane
2026-06-04 22:51:48 +02:00
parent 4444174365
commit c622420e38
2 changed files with 97 additions and 6 deletions
+67 -1
View File
@@ -1,9 +1,13 @@
import tempfile
from datetime import date
from pathlib import Path
import pytest
from app.main import QueryOptions, collect_merged_activity
from app.main import QueryOptions, collect_merged_activity, _fetch_source_usernames
from app.cache import FileCache
from app.settings import Settings
from app.sources import github as github_source
@pytest.mark.asyncio
@@ -38,3 +42,65 @@ async def test_collect_merged_activity_combines_multiple_usernames(monkeypatch)
assert sorted(github_calls) == ["hubot", "octocat"]
assert sorted(gitea_calls) == ["alice", "bob"]
assert result.merged["2026-01-01"] == {"github": 3, "gitea": 7, "total": 10}
@pytest.mark.asyncio
async def test_fetch_source_usernames_skips_failed_accounts() -> None:
async def ok_fetch():
return {"2026-01-01": 2}
async def bad_fetch():
raise github_source.GitHubSourceError("boom")
result, stale = await _fetch_source_usernames(
None,
"github",
["octocat", "hubot"],
date(2026, 1, 1),
date(2026, 1, 1),
lambda username: ok_fetch if username == "octocat" else bad_fetch,
)
assert result == {"2026-01-01": 2}
assert stale is False
@pytest.mark.asyncio
async def test_fetch_source_usernames_keeps_cache_after_username_list_changes() -> None:
with tempfile.TemporaryDirectory(dir=Path.cwd()) as temp_dir:
cache = FileCache(cache_dir=temp_dir, default_ttl_seconds=0)
def prime_fetcher(username):
async def fetch():
return {"2026-01-01": 1}
return fetch
await _fetch_source_usernames(
cache,
"github",
["octocat"],
date(2026, 1, 1),
date(2026, 1, 1),
prime_fetcher,
)
def fetch_after_change(username):
async def fetch():
if username == "octocat":
raise github_source.GitHubSourceError("down")
return {"2026-01-01": 2}
return fetch
result, stale = await _fetch_source_usernames(
cache,
"github",
["octocat", "hubot"],
date(2026, 1, 1),
date(2026, 1, 1),
fetch_after_change,
)
assert result == {"2026-01-01": 3}
assert stale is True