feat(persistence): PR6 T5.11 — ORM workspace_id nullable=False
Flip the 4 business ORM models (ThreadMetaRow, RunRow, FeedbackRow, RunEventRow) to ``workspace_id: Mapped[str]`` with ``nullable=False``. PR5's alembic 0003 already enforces NOT NULL at the DB layer; this aligns the ORM-driven ``create_all()`` path (dev / tests) with the same invariant so a new install ends up at the post-0003 schema without running alembic. Test fallout absorbed: - `tests/conftest.py` autouse seed now produces a fully consistent pair: user row (default_workspace_id = test-workspace-autouse) plus the workspace itself. The PR5 backfill script's "users without default_workspace_id" query no longer picks the fixture up. Insert order is user → workspace → UPDATE user, walking around the chicken- and-egg FK between `workspaces.owner_id` and `users.default_workspace_id`. - `tests/test_backfill_workspace_id.py` adds a file-scoped autouse fixture that temporarily flips `column.nullable = True` for the four business tables (production correctness comes from alembic 0003; the script's own job is exactly to fill rows between 0002 and 0003 so its tests need that transient state to be representable). Its `_init_engine` also deletes the autouse seed rows to match the "fresh DB" model the tests assume. - `test_thread_meta_workspace_filter::test_create_workspace_none_bypasses` renamed to `test_create_workspace_none_rejected_by_orm` and asserts the new IntegrityError on explicit None — write paths can no longer bypass workspace scope. - 5 `test_workspace_context` tests + the auth-middleware reset test get `@pytest.mark.no_auto_workspace` so they keep testing the unset-contextvar path. - `test_workspace_repo::test_list_by_user_bypass_returns_all` switches to membership assertions instead of strict equality since the autouse fixture surfaces under `user_id=None`. 3214 passed, 30 skipped; the remaining 17 are the documented pre-existing caplog ordering flakes (all pass in isolation).
This commit is contained in:
@@ -28,11 +28,13 @@ from deerflow.runtime.workspace_context import (
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@pytest.mark.no_auto_workspace
|
||||
def test_default_is_none():
|
||||
"""Before any set, contextvar returns None."""
|
||||
assert get_current_workspace() is None
|
||||
|
||||
|
||||
@pytest.mark.no_auto_workspace
|
||||
def test_set_and_reset_roundtrip():
|
||||
"""set_current_workspace returns a token that reset restores."""
|
||||
workspace = SimpleNamespace(id="ws-1", role="owner")
|
||||
@@ -44,6 +46,7 @@ def test_set_and_reset_roundtrip():
|
||||
assert get_current_workspace() is None
|
||||
|
||||
|
||||
@pytest.mark.no_auto_workspace
|
||||
def test_require_current_workspace_raises_when_unset():
|
||||
"""require_current_workspace raises RuntimeError if contextvar is unset."""
|
||||
assert get_current_workspace() is None
|
||||
@@ -93,6 +96,7 @@ def test_default_workspace_id_is_default():
|
||||
assert DEFAULT_WORKSPACE_ID == "default"
|
||||
|
||||
|
||||
@pytest.mark.no_auto_workspace
|
||||
def test_effective_workspace_id_returns_default_when_no_workspace():
|
||||
"""No workspace in context -> fallback to DEFAULT_WORKSPACE_ID."""
|
||||
assert get_effective_workspace_id() == "default"
|
||||
@@ -132,6 +136,7 @@ def test_resolve_auto_reads_from_contextvar():
|
||||
reset_current_workspace(token)
|
||||
|
||||
|
||||
@pytest.mark.no_auto_workspace
|
||||
def test_resolve_auto_raises_when_unset():
|
||||
assert get_current_workspace() is None
|
||||
with pytest.raises(RuntimeError, match="workspace_id=AUTO but no workspace"):
|
||||
|
||||
Reference in New Issue
Block a user