eksplicit mapping af envs
Some checks failed
Backend CI / test (push) Has been cancelled
Flutter CI / analyze-and-test (push) Has been cancelled

This commit is contained in:
Henrik Jess Nielsen
2026-05-12 18:21:25 +02:00
parent b7a435f8b9
commit 99e9b509a0
67 changed files with 8060 additions and 9 deletions

View File

@@ -0,0 +1,12 @@
import pytest
from httpx import ASGITransport, AsyncClient
from app.main import app
@pytest.mark.asyncio
async def test_health() -> None:
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
response = await client.get("/health")
assert response.status_code == 200
assert response.json() == {"status": "ok"}

View File

@@ -0,0 +1,61 @@
from unittest.mock import AsyncMock, patch
import pytest
from app.services.match_service import attempt_match, _format_interests
@pytest.mark.asyncio
async def test_no_match_when_sessions_missing() -> None:
with patch("app.services.match_service.get_session", return_value=None):
result = await attempt_match("token_a", "token_b")
assert result["match"] is False
assert result["nudge_sent"] is False
@pytest.mark.asyncio
async def test_no_match_when_no_shared_interests() -> None:
sessions = {
"token_a": {"interests": ["devops"]},
"token_b": {"interests": ["cooking"]},
}
async def fake_get(token: str) -> dict:
return sessions.get(token)
with patch("app.services.match_service.get_session", side_effect=fake_get):
with patch("app.services.match_service.manager") as mock_mgr:
result = await attempt_match("token_a", "token_b")
assert result["match"] is False
mock_mgr.send.assert_not_called()
@pytest.mark.asyncio
async def test_match_with_shared_interests() -> None:
sessions = {
"token_a": {"interests": ["devops", "hardstyle"]},
"token_b": {"interests": ["hardstyle", "philosophy"]},
}
async def fake_get(token: str) -> dict:
return sessions.get(token)
with patch("app.services.match_service.get_session", side_effect=fake_get):
with patch("app.services.match_service.manager") as mock_mgr:
mock_mgr.send = AsyncMock()
result = await attempt_match("token_a", "token_b")
assert result["match"] is True
assert "hardstyle" in result["shared_interests"]
assert result["nudge_sent"] is True
assert mock_mgr.send.call_count == 2
def test_format_interests_single() -> None:
assert _format_interests(["devops"]) == "devops"
def test_format_interests_multiple() -> None:
result = _format_interests(["devops", "hardstyle"])
assert result == "devops and hardstyle"

View File

@@ -0,0 +1,68 @@
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from httpx import ASGITransport, AsyncClient
from app.main import app
@pytest.mark.asyncio
async def test_stats_zero_sessions() -> None:
"""Stats endpoint returns zeroed StatsResponse when no data exists."""
mock_db = AsyncMock()
with (
patch("app.services.stats_service.count_active_sessions", return_value=0),
patch("app.core.database.get_db", return_value=mock_db),
):
# DB returns None for DailyStat row and empty for interest categories
mock_db.scalar = AsyncMock(return_value=None)
mock_db.scalars = AsyncMock(return_value=iter([]))
async with AsyncClient(
transport=ASGITransport(app=app), base_url="http://test"
) as client:
response = await client.get("/stats")
assert response.status_code == 200
data = response.json()
assert "active_sessions_now" in data
assert "total_sessions_today" in data
assert "total_matches_today" in data
assert "top_interest_categories" in data
assert data["active_sessions_now"] == 0
assert data["total_sessions_today"] == 0
assert data["total_matches_today"] == 0
assert data["top_interest_categories"] == []
@pytest.mark.asyncio
async def test_stats_with_data() -> None:
"""Stats endpoint returns correct values when DailyStat row exists."""
mock_stat = MagicMock()
mock_stat.total_sessions = 12
mock_stat.total_matches = 5
mock_category = MagicMock()
mock_category.category = "hiking"
mock_db = AsyncMock()
with (
patch("app.services.stats_service.count_active_sessions", return_value=3),
patch("app.core.database.get_db", return_value=mock_db),
):
mock_db.scalar = AsyncMock(return_value=mock_stat)
mock_db.scalars = AsyncMock(return_value=iter([mock_category]))
async with AsyncClient(
transport=ASGITransport(app=app), base_url="http://test"
) as client:
response = await client.get("/stats")
assert response.status_code == 200
data = response.json()
assert data["active_sessions_now"] == 3
assert data["total_sessions_today"] == 12
assert data["total_matches_today"] == 5
assert data["top_interest_categories"] == ["hiking"]