PMFlow/backend/tests/test_qa_context_workspaces_...

69 lines
2.5 KiB
Python

from __future__ import annotations
import importlib.util
from pathlib import Path
import sqlalchemy as sa
from alembic.migration import MigrationContext
from alembic.operations import Operations
def _load_migration_module():
path = (
Path(__file__).resolve().parents[1]
/ "alembic"
/ "versions"
/ "20260507_add_qa_context_workspaces.py"
)
spec = importlib.util.spec_from_file_location("qa_context_workspaces_migration", path)
module = importlib.util.module_from_spec(spec)
assert spec is not None
assert spec.loader is not None
spec.loader.exec_module(module)
return module
def _create_qa_session_states(metadata: sa.MetaData) -> None:
sa.Table(
"qa_session_states",
metadata,
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
sa.Column("tenant_id", sa.BigInteger(), nullable=False),
sa.Column("created_at", sa.TIMESTAMP(), nullable=False),
sa.Column("updated_at", sa.TIMESTAMP(), nullable=False),
sa.Column("session_id", sa.Integer(), nullable=False),
sa.Column("last_subject", sa.String(length=255), nullable=True),
sa.Column("last_entity_ids", sa.JSON(), nullable=True),
sa.Column("last_intent", sa.String(length=20), nullable=True),
sa.Column("ambiguity", sa.String(length=20), nullable=True),
sa.Column("rewrite_history", sa.JSON(), nullable=True),
sa.Column("pending_clarification", sa.JSON(), nullable=True),
sa.Column("pending_clarification_expires_at", sa.TIMESTAMP(), nullable=True),
)
def test_qa_context_workspaces_migration_adds_and_removes_workspace_columns() -> None:
migration = _load_migration_module()
engine = sa.create_engine("sqlite:///:memory:")
metadata = sa.MetaData()
_create_qa_session_states(metadata)
metadata.create_all(engine)
with engine.begin() as conn:
context = MigrationContext.configure(conn)
migration.op = Operations(context)
migration.upgrade()
inspector = sa.inspect(conn)
state_columns = {column["name"] for column in inspector.get_columns("qa_session_states")}
assert "active_workspace" in state_columns
assert "context_capsules" in state_columns
migration.downgrade()
inspector = sa.inspect(conn)
state_columns = {column["name"] for column in inspector.get_columns("qa_session_states")}
assert "active_workspace" not in state_columns
assert "context_capsules" not in state_columns