From 9737d90f1bdb8452c0a33b69b22976fff305454f Mon Sep 17 00:00:00 2001 From: Michael Chihlas Date: Sat, 25 Apr 2026 01:49:50 -0400 Subject: [PATCH] fix(tests): repair two pre-existing bugs blocking the backend CI gate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. backend/app/models/network_diagram.py — `nodes` and `edges` columns used `server_default="'[]'"` (a Python string), which SQLAlchemy wraps in single quotes when generating DDL, producing `JSONB DEFAULT '''[]'''` — invalid JSON. Switch to `server_default=text("'[]'::jsonb")` so the literal is passed through and the table can actually be created. Surfaced on every CI run as `asyncpg.exceptions.InvalidTextRepresentationError: invalid input syntax for type json` at fixture setup time, cascading hundreds of test errors. 2. backend/tests/conftest.py — drop the deprecated session-scoped `event_loop` fixture. Since pytest-asyncio 0.23+, the plugin manages the loop itself; redefining it with a session scope but never `set_event_loop()`-ing it left the loop dangling, so any test that called `asyncio.run()` (e.g. `test_tasks_are_isolated`) closed the process loop and broke the next async test in the module — `test_require_tenant_context_raises_403_when_no_account` was the visible casualty in the CI logs. Verified locally: - `pytest tests/test_uploads.py::test_upload_success` — was setup-error on `network_diagrams` DDL; now passes. - `pytest tests/test_tenant_context.py` — was 1 fail / 3 pass; now 4/4. Both are real bugs, not test infrastructure churn. Pre-existing on main; not introduced here. Co-Authored-By: Claude Opus 4.7 --- backend/app/models/network_diagram.py | 6 +++--- backend/tests/conftest.py | 8 -------- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/backend/app/models/network_diagram.py b/backend/app/models/network_diagram.py index 347216da..2a8f3e45 100644 --- a/backend/app/models/network_diagram.py +++ b/backend/app/models/network_diagram.py @@ -3,7 +3,7 @@ import uuid from datetime import datetime, timezone from typing import Any, TYPE_CHECKING -from sqlalchemy import String, Text, Boolean, DateTime, ForeignKey +from sqlalchemy import String, Text, Boolean, DateTime, ForeignKey, text from sqlalchemy.orm import Mapped, mapped_column, relationship from sqlalchemy.dialects.postgresql import UUID, JSONB @@ -30,8 +30,8 @@ class NetworkDiagram(Base): client_name: Mapped[str | None] = mapped_column(String(255), nullable=True) asset_name: Mapped[str | None] = mapped_column(String(255), nullable=True) description: Mapped[str | None] = mapped_column(Text, nullable=True) - nodes: Mapped[list[dict[str, Any]]] = mapped_column(JSONB, nullable=False, server_default="'[]'") - edges: Mapped[list[dict[str, Any]]] = mapped_column(JSONB, nullable=False, server_default="'[]'") + nodes: Mapped[list[dict[str, Any]]] = mapped_column(JSONB, nullable=False, server_default=text("'[]'::jsonb")) + edges: Mapped[list[dict[str, Any]]] = mapped_column(JSONB, nullable=False, server_default=text("'[]'::jsonb")) thumbnail_url: Mapped[str | None] = mapped_column(Text, nullable=True) is_archived: Mapped[bool] = mapped_column( Boolean, nullable=False, default=False, diff --git a/backend/tests/conftest.py b/backend/tests/conftest.py index 9c1f60e6..0adf36ff 100644 --- a/backend/tests/conftest.py +++ b/backend/tests/conftest.py @@ -32,14 +32,6 @@ TEST_DATABASE_URL = os.environ.get( ) -@pytest.fixture(scope="session") -def event_loop() -> Generator: - """Create an instance of the default event loop for each test case.""" - loop = asyncio.get_event_loop_policy().new_event_loop() - yield loop - loop.close() - - @pytest.fixture async def test_db() -> AsyncGenerator[AsyncSession, None]: """ -- 2.49.1