cmvr-visual/backend/tests/test_schema_v6.py

43 lines
2.1 KiB
Python
Raw Permalink Normal View History

from pathlib import Path
from alembic import command
from alembic.config import Config
from sqlalchemy import create_engine, text, inspect
from app.models import Base
from app.core.schema import schema_preflight, check_schema_revision
def test_v6_preserves_005_assets_and_metadata(tmp_path):
config = Config(str(Path(__file__).parents[1] / "alembic.ini"))
path = tmp_path / "folders.sqlite"
config.attributes["url"] = "sqlite+aiosqlite:///" + str(path)
command.upgrade(config, "005")
engine = create_engine("sqlite:///" + str(path))
with engine.begin() as connection:
connection.execute(
text(
"INSERT INTO visual_users(id,username,display_name,password_hash,role,disabled,must_change_password,created_at) VALUES('owner','owner','Owner','UNCHANGED','user',0,0,CURRENT_TIMESTAMP)"
)
)
connection.execute(
text(
"INSERT INTO visual_assets(id,owner_id,name,storage_key,kind,mime_type,size_bytes,sha256,tags,favorite,origin,ready,library_scope,created_at) VALUES('asset','owner','Original','owner/asset/source.png','image','image/png',123,:sha,'[]',0,'upload',1,'private',CURRENT_TIMESTAMP)"
),
{"sha": "0" * 64},
)
command.upgrade(config, "006")
command.upgrade(config, "head")
with engine.begin() as connection:
assert connection.execute(text("SELECT id,storage_key FROM visual_assets")).one() == (
"asset",
"owner/asset/source.png",
)
assert connection.scalar(text("SELECT COUNT(*) FROM visual_asset_folders")) == 0
diff = schema_preflight(connection, Base.metadata)
assert diff.matches, diff.differences
check_schema_revision(connection, {"009"})
for table in ("visual_jobs", "visual_drafts", "visual_workflow_runs"):
fks = inspect(connection).get_foreign_keys(table)
folder_fks = [f for f in fks if f["referred_table"] == "visual_folders"]
assert folder_fks
assert all(f["options"]["ondelete"] == "SET NULL" for f in folder_fks)