69 lines
2.3 KiB
Python
69 lines
2.3 KiB
Python
|
|
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"]
|