tests: ensure helper function actually updates database schema
This commit is contained in:
parent
aba919adca
commit
aecd10115e
3 changed files with 18 additions and 4 deletions
|
|
@ -6,5 +6,5 @@ sqlite_url = os.environ.get("DATABASE_URL", "sqlite:///database.db")
|
||||||
engine = create_engine(sqlite_url, echo=False)
|
engine = create_engine(sqlite_url, echo=False)
|
||||||
|
|
||||||
|
|
||||||
def create_db_and_tables():
|
def create_db_and_tables(engine):
|
||||||
SQLModel.metadata.create_all(engine)
|
SQLModel.metadata.create_all(engine)
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ TEILCHEN_DATA_HEADER = "pk Name Description Number Tags".split()
|
||||||
|
|
||||||
class SammlerApp(App):
|
class SammlerApp(App):
|
||||||
async def on_mount(self) -> None:
|
async def on_mount(self) -> None:
|
||||||
create_db_and_tables()
|
create_db_and_tables(engine)
|
||||||
self.push_screen(AddInventoryScreen())
|
self.push_screen(AddInventoryScreen())
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
18
tests.py
18
tests.py
|
|
@ -1,16 +1,18 @@
|
||||||
|
from teilchensammler_cli.database import create_db_and_tables
|
||||||
|
from sqlalchemy.sql import text
|
||||||
import uuid
|
import uuid
|
||||||
from typing import Generator, Sequence
|
from typing import Generator, Sequence
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from sqlalchemy import Engine
|
from sqlalchemy import Engine
|
||||||
from sqlmodel import Session
|
from sqlmodel import Session, SQLModel, create_engine
|
||||||
|
|
||||||
from teilchensammler_cli.models import (
|
from teilchensammler_cli.models import (
|
||||||
Teilchen,
|
Teilchen,
|
||||||
TeilchenCreate,
|
TeilchenCreate,
|
||||||
|
add_to_database,
|
||||||
load_initial_data,
|
load_initial_data,
|
||||||
make_teilchen_input,
|
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)
|
db_teilchen = await add_to_database(teilchen, app_db)
|
||||||
|
|
||||||
assert teilchen == db_teilchen
|
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
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue