Files
TJWaterServerBinary/tests/api/test_wndb_endpoint_threading.py
T

69 lines
2.2 KiB
Python

from threading import get_ident
from uuid import uuid4
from fastapi import FastAPI
from fastapi.testclient import TestClient
from app.api.v1.endpoints.components import curves
from app.api.v1.rest_router import api_router
from app.auth.project_dependencies import (
ProjectContext,
get_project_business_routing,
get_project_context,
)
from app.infra.db.project_routing import ActiveProjectRouting, get_active_project_routing
def test_sync_wndb_endpoint_parses_body_and_runs_in_worker_thread(monkeypatch) -> None:
call: dict[str, object] = {}
project_id = uuid4()
user_id = uuid4()
context = ProjectContext(
project_id=project_id,
project_code="project_a",
user_id=user_id,
project_role="member",
)
routing = ActiveProjectRouting(
project_code="project_a",
business_dsn="postgresql://user:password@db.example/project_a",
)
async def override_context() -> ProjectContext:
call["event_loop_thread"] = get_ident()
return context
async def override_routing() -> ActiveProjectRouting:
return routing
def fake_add_curve(network, changes):
call["worker_thread"] = get_ident()
call["network"] = network
call["operations"] = changes.operations
call["routing"] = get_active_project_routing()
return {"ok": True}
monkeypatch.setattr(curves, "add_curve", fake_add_curve)
app = FastAPI()
app.include_router(api_router)
app.dependency_overrides[get_project_context] = override_context
app.dependency_overrides[get_project_business_routing] = override_routing
with TestClient(app) as client:
response = client.post(
"/curves",
params={"curve": "C-1"},
json={"points": [[0, 10], [1, 20]]},
)
assert response.status_code == 201
assert response.json() == {"ok": True}
assert call == {
"event_loop_thread": call["event_loop_thread"],
"worker_thread": call["worker_thread"],
"network": "project_a",
"operations": [{"id": "C-1", "points": [[0, 10], [1, 20]]}],
"routing": routing,
}
assert call["worker_thread"] != call["event_loop_thread"]