Update stats
This commit is contained in:
@@ -69,5 +69,40 @@ async def test_timetable(setup_db):
|
||||
f"/api/stats/timetable?from_date={from_date}&to_date={to_date}"
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_summary(setup_db):
|
||||
from datetime import date, timedelta, timezone
|
||||
|
||||
async with AsyncClient(
|
||||
transport=ASGITransport(app=app), base_url="http://test"
|
||||
) as ac:
|
||||
from app.database import get_session
|
||||
from app.models import SleepSession
|
||||
from sqlalchemy import select
|
||||
|
||||
async with AsyncSession(engine) as session:
|
||||
# Create two sessions, one a few days ago and one today
|
||||
start1 = datetime.now(timezone.utc) - timedelta(days=5)
|
||||
end1 = start1 + timedelta(hours=8)
|
||||
new_s1 = SleepSession(start_time=start1, end_time=end1)
|
||||
session.add(new_s1)
|
||||
|
||||
start2 = datetime.now(timezone.utc) - timedelta(hours=2)
|
||||
end2 = datetime.now(timezone.utc)
|
||||
new_s2 = SleepSession(start_time=start2, end_time=end2)
|
||||
session.add(new_s2)
|
||||
|
||||
await session.commit()
|
||||
|
||||
from_date = (date.today() - timedelta(days=7)).isoformat()
|
||||
to_date = date.today().isoformat()
|
||||
response = await ac.get(
|
||||
f"/api/stats/summary?from_date={from_date}&to_date={to_date}"
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert len(response.json()["slots"]) > 0
|
||||
data = response.json()
|
||||
assert data["total_sleeps"] == 2
|
||||
assert data["total_sleeps_all_time"] == 2
|
||||
assert data["avg_sleeps_per_day_all_time"] > 0
|
||||
|
||||
|
||||
Reference in New Issue
Block a user