test: upgrade backend test suite, implement unit tests, and modernize CRUD patterns
Build Backend / build (push) Successful in 6m39s
Build Backend / build (push) Successful in 6m39s
This commit is contained in:
@@ -1,4 +1,35 @@
|
||||
import pytest
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from app.models import SleepSession
|
||||
from sqlalchemy import select
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_summary_double_counting_midnight(api_client, db_session):
|
||||
# 1. Setup: Insert a sleep session that spans across midnight
|
||||
start_time = datetime(2024, 1, 1, 23, 0)
|
||||
end_time = datetime(2024, 1, 2, 7, 0)
|
||||
|
||||
new_session = SleepSession(start_time=start_time, end_time=end_time)
|
||||
db_session.add(new_session)
|
||||
await db_session.commit()
|
||||
|
||||
# 2. Call the stats endpoint for a period that includes this session
|
||||
from_date = "2024-01-01T00:00:00"
|
||||
to_date = "2024-01-02T23:59:59"
|
||||
|
||||
async with api_client as ac:
|
||||
response = await ac.get(f"/api/stats/summary?from_date={from_date}&to_date={to_date}")
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
|
||||
# 3. Verify that total_sleeps is exactly 1 for the requested period
|
||||
print(f"DEBUG: data={data}")
|
||||
assert data["total_sleeps"] == 1, f"Expected 1 sleep session, got {data['total_sleeps']}"
|
||||
|
||||
# 4. Verify that all-time stats also count this session exactly once
|
||||
assert data["total_sleeps_all_time"] == 1, f"Expected 1 all-time sleep session, got {data['total_sleeps_all_time']}"
|
||||
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from app.models import SleepSession
|
||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||
|
||||
Reference in New Issue
Block a user