from fastapi.testclient import TestClient from tests.conftest import build_test_app, install_stub, load_module_from_path class DummyChangeSet: def __init__(self, operations=None): self.operations = [operations] if isinstance(operations, dict) else operations or [] def _load_regions_module(monkeypatch): install_stub(monkeypatch, "app.services", package=True) install_stub( monkeypatch, "app.services.tjnetwork", { "ChangeSet": DummyChangeSet, "add_region": lambda network, cs: cs, "delete_region": lambda network, cs: cs, "get_nodes_in_region": lambda network, region_id: ["J1"], "get_region": lambda network, region_id: { "id": region_id, "region_type": "DMA", "boundary": [[0, 0], [1, 0], [0, 0]], }, "get_region_schema": lambda network: {"id": {"type": "str"}}, "get_regions": lambda network: ["DMA-1"], "set_region": lambda network, cs: cs, }, ) return load_module_from_path( "tests_regions_endpoints_module", "app/api/v1/endpoints/network/regions.py", ) def test_regions_are_exposed_as_one_generic_resource(monkeypatch): module = _load_regions_module(monkeypatch) client = TestClient(build_test_app(module.router, "/api/v1")) response = client.get("/api/v1/regions", params={"network": "demo"}) assert response.status_code == 200 assert response.json()[0]["region_type"] == "DMA" def test_add_region_converts_boundary_to_tuples(monkeypatch): module = _load_regions_module(monkeypatch) captured = {} def add(network, changeset): captured["operation"] = changeset.operations[0] return changeset monkeypatch.setattr(module, "add_region", add) client = TestClient(build_test_app(module.router, "/api/v1")) response = client.post( "/api/v1/regions", params={"network": "demo"}, json={ "id": "DMA-1", "region_type": "DMA", "boundary": [[0, 0], [1, 0], [0, 0]], }, ) assert response.status_code == 200 assert captured["operation"]["boundary"] == [(0, 0), (1, 0), (0, 0)] def test_region_nodes_use_generic_region_id(monkeypatch): module = _load_regions_module(monkeypatch) client = TestClient(build_test_app(module.router, "/api/v1")) response = client.get( "/api/v1/regions/nodes", params={"network": "demo", "id": "DMA-1"} ) assert response.status_code == 200 assert response.json() == ["J1"]