from datetime import datetime, timezone from uuid import uuid4 import pytest from app.services import burst_location START = datetime(2026, 8, 1, tzinfo=timezone.utc) END = datetime(2026, 8, 2, tzinfo=timezone.utc) def test_analysis_simulation_query_is_keyed_by_run_id(monkeypatch): run_id = uuid4() captured = {} def fake_query(**kwargs): captured.update(kwargs) return {"J1": []} monkeypatch.setattr( burst_location.InternalQueries, "query_analysis_simulation_by_ids_timerange", staticmethod(fake_query), ) result = burst_location._query_simulation_values( network="demo", element_ids=["J1"], element_type="node", field="pressure", start_dt=START, end_dt=END, simulation_source="analysis", simulation_run_id=run_id, ) assert result == {"J1": []} assert captured["run_id"] == run_id assert "scheme_name" not in captured assert "scheme_type" not in captured def test_analysis_simulation_query_requires_run_id(): with pytest.raises(ValueError, match="simulation_run_id"): burst_location._query_simulation_values( network="demo", element_ids=["J1"], element_type="node", field="pressure", start_dt=START, end_dt=END, simulation_source="analysis", simulation_run_id=None, ) def test_scada_mapping_uses_canonical_asset_fields(monkeypatch): monkeypatch.setattr( burst_location, "get_all_scada_info", lambda network: [ { "device_id": "pressure-1", "device_type": "pressure", "node_id": "J1", "link_id": None, "api_query_id": "q-pressure", }, { "device_id": "flow-1", "device_type": "pipe_flow", "node_id": None, "link_id": "P1", "api_query_id": "q-flow", }, ], ) assert burst_location._build_scada_mapping("demo", "pressure") == { "J1": "q-pressure" } assert burst_location._build_scada_mapping("demo", "flow") == { "P1": "q-flow" } def test_burst_ids_are_read_from_analysis_run(monkeypatch): run_id = uuid4() monkeypatch.setattr( burst_location, "get_analysis_run", lambda network, value: { "run_id": value, "parameters": {"burst_ID": ["P1", "P2"]}, }, ) assert burst_location._get_simulation_run_burst_ids( network="demo", run_id=run_id ) == ["P1", "P2"]