Two related fixes that came out of running the W5 tests locally:
1. tests/__init__.py — empty file, makes 'tests/' a package so pytest
stops inserting it into sys.path. Without it, 'tests/docker/'
(the docker-image test category) shadowed the installed docker SDK
on every engine-touching test in the repo:
module 'docker' has no attribute 'DockerClient'
Pytest's default --import-mode=prepend was the culprit; making
tests/ a package is the cheapest fix and doesn't change
--import-mode for the whole tree.
2. delete_topology_decky / delete_topology_edge / delete_lan grow an
'enforce_pending: bool = True' kwarg. Default preserves the HTTP
CRUD guard (api_decky_crud / api_edge_crud / api_lan_crud get the
409 for free). apply_remove_decky / apply_detach_decky /
apply_remove_lan now pass enforce_pending=False — the mutator
queue is the live-editing surface and has its own active-topology
gating; the repo's pending-only guard was for design-time CRUD
that mustn't bypass it. Without this, apply_remove_decky was
silently broken on active topologies pre-W5; W5's new test
surfaced it on first run.
10/10 new W5 tests pass; 58/58 across mutator + topology suites.
81 lines
2.8 KiB
Python
81 lines
2.8 KiB
Python
"""Topology edge CRUD + status-event log."""
|
|
from __future__ import annotations
|
|
|
|
from typing import Any, Optional
|
|
|
|
from sqlalchemy import desc, select, text
|
|
|
|
from decnet.web.db.models import TopologyEdge, TopologyStatusEvent
|
|
|
|
|
|
class TopologyEdgesMixin:
|
|
"""``self._assert_pending`` / ``self._check_and_bump_version`` resolve
|
|
through ``TopologyCoreMixin`` via MRO."""
|
|
|
|
async def add_topology_edge(
|
|
self,
|
|
data: dict[str, Any],
|
|
*,
|
|
expected_version: Optional[int] = None,
|
|
) -> str:
|
|
async with self._session() as session:
|
|
await self._check_and_bump_version(
|
|
session, data["topology_id"], expected_version
|
|
)
|
|
row = TopologyEdge(**data)
|
|
session.add(row)
|
|
await session.commit()
|
|
await session.refresh(row)
|
|
return row.id
|
|
|
|
async def delete_topology_edge(
|
|
self,
|
|
edge_id: str,
|
|
*,
|
|
expected_version: Optional[int] = None,
|
|
enforce_pending: bool = True,
|
|
) -> None:
|
|
"""Delete one edge. ``enforce_pending=True`` by default — the
|
|
mutator's ``apply_detach_decky`` opts out, same rationale as
|
|
``delete_topology_decky``.
|
|
"""
|
|
async with self._session() as session:
|
|
result = await session.execute(
|
|
select(TopologyEdge).where(TopologyEdge.id == edge_id)
|
|
)
|
|
edge = result.scalar_one_or_none()
|
|
if edge is None:
|
|
return
|
|
if enforce_pending:
|
|
await self._assert_pending(session, edge.topology_id)
|
|
if expected_version is not None:
|
|
await self._check_and_bump_version(
|
|
session, edge.topology_id, expected_version
|
|
)
|
|
await session.execute(
|
|
text("DELETE FROM topology_edges WHERE id = :e"),
|
|
{"e": edge_id},
|
|
)
|
|
await session.commit()
|
|
|
|
async def list_topology_edges(
|
|
self, topology_id: str
|
|
) -> list[dict[str, Any]]:
|
|
async with self._session() as session:
|
|
result = await session.execute(
|
|
select(TopologyEdge).where(TopologyEdge.topology_id == topology_id)
|
|
)
|
|
return [r.model_dump(mode="json") for r in result.scalars().all()]
|
|
|
|
async def list_topology_status_events(
|
|
self, topology_id: str, limit: int = 100
|
|
) -> list[dict[str, Any]]:
|
|
async with self._session() as session:
|
|
result = await session.execute(
|
|
select(TopologyStatusEvent)
|
|
.where(TopologyStatusEvent.topology_id == topology_id)
|
|
.order_by(desc(TopologyStatusEvent.at))
|
|
.limit(limit)
|
|
)
|
|
return [r.model_dump(mode="json") for r in result.scalars().all()]
|