fixed tests

This commit is contained in:
2026-05-15 20:41:05 +02:00
parent 96ce516ecf
commit 77df5d5d65
50 changed files with 1482 additions and 5089 deletions

View File

@@ -1,10 +1,9 @@
import asyncio
import os
import time
from pathlib import Path
from unittest.mock import AsyncMock, patch
import aiosqlite
import pytest
from app.db import (
_apply_migration,
_cleanup_wal_files,
@@ -37,9 +36,7 @@ async def test_open_db_respects_busy_timeout_for_concurrent_writes(tmp_path: Pat
database_path = str(tmp_path / "bangui_lock.db")
connection_a = await open_db(database_path)
try:
await connection_a.execute(
"CREATE TABLE IF NOT EXISTS test_lock (id INTEGER PRIMARY KEY, value TEXT);"
)
await connection_a.execute("CREATE TABLE IF NOT EXISTS test_lock (id INTEGER PRIMARY KEY, value TEXT);")
await connection_a.commit()
await connection_a.execute("BEGIN EXCLUSIVE;")
@@ -47,9 +44,7 @@ async def test_open_db_respects_busy_timeout_for_concurrent_writes(tmp_path: Pat
async def write_after_lock() -> None:
connection_b = await open_db(database_path)
try:
await connection_b.execute(
"INSERT INTO test_lock (value) VALUES ('locked');"
)
await connection_b.execute("INSERT INTO test_lock (value) VALUES ('locked');")
await connection_b.commit()
finally:
await connection_b.close()
@@ -148,16 +143,12 @@ async def test_apply_migration_is_atomic_success(tmp_path: Path) -> None:
await _apply_migration(db, 1)
# Verify the migration was recorded
async with db.execute(
"SELECT version FROM schema_migrations WHERE version = 1;"
) as cursor:
async with db.execute("SELECT version FROM schema_migrations WHERE version = 1;") as cursor:
row = await cursor.fetchone()
assert row is not None and row[0] == 1
# Verify the schema tables exist
async with db.execute(
"SELECT name FROM sqlite_master WHERE type='table' AND name='settings';"
) as cursor:
async with db.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='settings';") as cursor:
row = await cursor.fetchone()
assert row is not None
finally:
@@ -166,7 +157,7 @@ async def test_apply_migration_is_atomic_success(tmp_path: Path) -> None:
async def test_apply_migration_is_atomic_rollback(tmp_path: Path) -> None:
"""Test that migration is rolled back when a statement fails.
This test verifies that when an error occurs mid-migration, the
transaction is rolled back and the schema_migrations table is NOT updated.
"""
@@ -181,24 +172,22 @@ async def test_apply_migration_is_atomic_rollback(tmp_path: Path) -> None:
# Create a custom migration that will fail
from app import db as db_module
original_migrations = db_module._MIGRATIONS.copy()
# Add a migration that will fail on the second statement
db_module._MIGRATIONS[99] = """
CREATE TABLE test_rollback (id INTEGER PRIMARY KEY);
INSERT INTO nonexistent_table VALUES (1);
"""
try:
# Attempt migration; it should fail
with pytest.raises(Exception): # sqlite3 will raise an error
await _apply_migration(db, 99)
# Verify the migration was NOT recorded
async with db.execute(
"SELECT version FROM schema_migrations WHERE version = 99;"
) as cursor:
async with db.execute("SELECT version FROM schema_migrations WHERE version = 99;") as cursor:
row = await cursor.fetchone()
assert row is None
@@ -224,18 +213,14 @@ async def test_init_db_idempotent(tmp_path: Path) -> None:
await init_db(db)
# Get schema version
async with db.execute(
"SELECT MAX(version) FROM schema_migrations;"
) as cursor:
async with db.execute("SELECT MAX(version) FROM schema_migrations;") as cursor:
row1 = await cursor.fetchone()
# Initialize again (should be no-op)
await init_db(db)
# Verify schema version is unchanged
async with db.execute(
"SELECT MAX(version) FROM schema_migrations;"
) as cursor:
async with db.execute("SELECT MAX(version) FROM schema_migrations;") as cursor:
row2 = await cursor.fetchone()
assert row1 == row2
@@ -249,9 +234,12 @@ async def test_cleanup_wal_files_removes_orphaned_files(tmp_path: Path) -> None:
wal_path = Path(db_path + "-wal")
shm_path = Path(db_path + "-shm")
# Create the orphaned files
# Create the orphaned files with an old mtime so they look stale
wal_path.write_text("orphan")
shm_path.write_text("orphan")
old_mtime = time.time() - 20
os.utime(wal_path, (old_mtime, old_mtime))
os.utime(shm_path, (old_mtime, old_mtime))
assert wal_path.exists()
assert shm_path.exists()
@@ -270,4 +258,3 @@ async def test_cleanup_wal_files_handles_missing_files(tmp_path: Path) -> None:
# Should not raise
await _cleanup_wal_files(db_path)