refactor(models): remove implicit-session Account tenant setters (#43064)

This commit is contained in:
Denis Ermakov authored and GitHub committed 2026-09-29 02:03:34 +00:00
1 parent 1ff3d3db68
commit 9bb9935f44
37 files changed
+63 -88

No files matched your search

@@ -25,10 +25,9 @@ class ConcreteTraceInstance(BaseTraceInstance):
@pytest.fixture(autouse=True)
def _bind_production_sessions(monkeypatch: pytest.MonkeyPatch, sqlite_engine: Engine) -> None:
"""Bind both service-owned ORM sessions to the test's SQLite engine."""
"""Bind the service-owned ORM session to the test's SQLite engine."""
database = SimpleNamespace(engine=sqlite_engine)
monkeypatch.setattr("core.ops.base_trace_instance.db", database)
monkeypatch.setattr("models.account.db", database)
def _persist_app(session: Session, *, created_by: str | None) -> App:
@@ -52,15 +52,11 @@ def sqlite_tool_db(
monkeypatch: pytest.MonkeyPatch,
sqlite_engine: Engine,
) -> Iterator[SqliteToolDb]:
"""Bind service-owned sessions and Account tenant reloads to SQLite."""
"""Bind service-owned sessions to SQLite."""
models = (App, Workflow, EndUser, Account, Tenant, TenantAccountJoin)
TypeBase.metadata.create_all(sqlite_engine, tables=[model.__table__ for model in models])
session_maker = sessionmaker(bind=sqlite_engine, expire_on_commit=False)
monkeypatch.setattr(workflow_tool_module.session_factory, "create_session", session_maker)
from models import account as account_module
monkeypatch.setattr(account_module, "db", SimpleNamespace(engine=sqlite_engine))
with session_maker() as caller_session:
yield SqliteToolDb(engine=sqlite_engine, session_maker=session_maker, caller_session=caller_session)
@@ -335,6 +335,14 @@ class TestTenantRelationshipIntegrity:
# Assert
assert tenant_id_none is None
def test_current_tenant_is_read_only(self):
"""current_tenant has no setter, so tenant switching cannot open a hidden session."""
account = Account(name="Test User", email="test@example.com")
tenant = Tenant(name="Test Tenant")
with pytest.raises(AttributeError):
account.current_tenant = tenant # type: ignore[misc]
@pytest.mark.parametrize("sqlite_session", [(Account, Tenant, TenantAccountJoin)], indirect=True)
def test_set_current_tenant_with_session_uses_caller_session(self, sqlite_session: Session):
account = Account(name="Test User", email="test@example.com")