tests: ensure helper function actually updates database schema

This commit is contained in:
bronsen 2026-02-22 18:58:05 +01:00
parent aba919adca
commit aecd10115e
3 changed files with 18 additions and 4 deletions

View file

@ -6,5 +6,5 @@ sqlite_url = os.environ.get("DATABASE_URL", "sqlite:///database.db")
engine = create_engine(sqlite_url, echo=False)
def create_db_and_tables():
def create_db_and_tables(engine):
SQLModel.metadata.create_all(engine)

View file

@ -26,7 +26,7 @@ TEILCHEN_DATA_HEADER = "pk Name Description Number Tags".split()
class SammlerApp(App):
async def on_mount(self) -> None:
create_db_and_tables()
create_db_and_tables(engine)
self.push_screen(AddInventoryScreen())

View file

@ -1,16 +1,18 @@
from teilchensammler_cli.database import create_db_and_tables
from sqlalchemy.sql import text
import uuid
from typing import Generator, Sequence
import pytest
from sqlalchemy import Engine
from sqlmodel import Session
from sqlmodel import Session, SQLModel, create_engine
from teilchensammler_cli.models import (
Teilchen,
TeilchenCreate,
add_to_database,
load_initial_data,
make_teilchen_input,
add_to_database,
)
@ -174,3 +176,15 @@ async def test_data_provided_to_addtodatabase_ends_up_in_database(app_db: Engine
db_teilchen = await add_to_database(teilchen, app_db)
assert teilchen == db_teilchen
async def test_created_database_contains_expected_tables(engine: Engine):
with engine.connect() as connection:
statement = text("SELECT name from sqlite_schema WHERE type = 'table'")
results = connection.execute(statement).all()
assert len(results) == 0
create_db_and_tables(engine)
results = connection.execute(statement).all()
assert len(results) == 1