test: upgrade backend test suite, implement unit tests, and modernize CRUD patterns
Build Backend / build (push) Successful in 6m39s

This commit is contained in:
2026-06-08 16:17:11 +02:00
parent 4d53887fcc
commit a9e1e96622
13 changed files with 759 additions and 25 deletions
+13 -14
View File
@@ -28,8 +28,8 @@ async def get_sessions(
count_statement = count_statement.where(SleepSession.start_time >= query_from_date)
if query_to_date:
count_statement = count_statement.where(SleepSession.start_time <= query_to_date)
count_result = await session.exec(count_statement)
total = count_result.one() or 0
count_result = await session.execute(count_statement)
total = count_result.scalar() or 0
statement = select(SleepSession).order_by(SleepSession.start_time.desc())
if query_from_date:
@@ -38,14 +38,14 @@ async def get_sessions(
statement = statement.where(SleepSession.start_time <= query_to_date)
statement = statement.limit(limit)
result = await session.exec(statement)
sessions = result.all()
result = await session.execute(statement)
sessions = result.scalars().all()
return list(sessions), total
async def get_active_session(session: AsyncSession) -> Optional[SleepSession]:
statement = select(SleepSession).where(SleepSession.end_time == None)
result = await session.exec(statement)
return result.first()
result = await session.execute(statement)
return result.scalars().first()
async def create_sleep_session(session: AsyncSession, start_time: datetime) -> SleepSession:
new_session = SleepSession(start_time=start_time)
@@ -58,8 +58,8 @@ async def end_sleep_session(
session: AsyncSession, session_id: int, end_time: datetime
) -> Union[SleepSession, bool, str, None]:
statement = select(SleepSession).where(SleepSession.id == session_id)
result = await session.exec(statement)
sleep_session = result.first()
result = await session.execute(statement)
sleep_session = result.scalars().first()
if not sleep_session:
return None # Not found
@@ -83,8 +83,8 @@ async def end_sleep_session(
async def delete_session(session: AsyncSession, session_id: int) -> bool:
statement = select(SleepSession).where(SleepSession.id == session_id)
result = await session.exec(statement)
sleep_session = result.first()
result = await session.execute(statement)
sleep_session = result.scalars().first()
if not sleep_session:
return False
@@ -97,8 +97,8 @@ async def update_session(
session: AsyncSession, session_id: int, update_data: dict
) -> Optional[SleepSession]:
statement = select(SleepSession).where(SleepSession.id == session_id)
result = await session.exec(statement)
sleep_session = result.first()
result = await session.execute(statement)
sleep_session = result.scalars().first()
if not sleep_session:
return None
@@ -112,7 +112,6 @@ async def update_session(
await session.refresh(sleep_session)
return sleep_session
async def get_sleep_counts_by_start_date(
session: AsyncSession,
from_date: Optional[datetime] = None,
@@ -135,5 +134,5 @@ async def get_sleep_counts_by_start_date(
if query_to_date:
statement = statement.where(SleepSession.start_time <= query_to_date)
result = await session.exec(statement)
result = await session.execute(statement)
return list(result.all())