diff --git a/app/services/burst_location.py b/app/services/burst_location.py index cb8154e..3892ca2 100644 --- a/app/services/burst_location.py +++ b/app/services/burst_location.py @@ -126,15 +126,16 @@ def run_burst_location_by_network( ) observed_source = "simulation_scheme_burst_realtime_normal_timerange" else: - burst_pressure_series, burst_pressure_samples = ( - _build_observed_series_from_scada( - network=network, - sensor_ids=selected_pressure_ids, - start_dt=burst_start_dt, - end_dt=burst_end_dt, - data_type="pressure", - series_name="burst_pressure", - ) + ( + burst_pressure_series, + burst_pressure_samples, + ) = _build_observed_series_from_scada( + network=network, + sensor_ids=selected_pressure_ids, + start_dt=burst_start_dt, + end_dt=burst_end_dt, + data_type="pressure", + series_name="burst_pressure", ) ( normal_pressure_series, diff --git a/tests/unit/test_burst_location_service.py b/tests/unit/test_burst_location_service.py index 7b4fa08..abe38a1 100644 --- a/tests/unit/test_burst_location_service.py +++ b/tests/unit/test_burst_location_service.py @@ -228,3 +228,74 @@ def test_run_burst_location_requires_simulation_scheme_name(monkeypatch, tmp_pat scada_burst_start=datetime(2025, 1, 1, 8, 0, 0), scada_burst_end=datetime(2025, 1, 1, 9, 0, 0), ) + + +def test_run_burst_location_monitoring_uses_scada_for_burst_and_realtime_for_normal( + monkeypatch, tmp_path +): + module = _load_burst_location_module() + captured = {} + scada_calls = [] + realtime_calls = [] + + monkeypatch.setattr( + module, + "get_all_scada_info", + lambda network: [ + { + "type": "pressure", + "associated_element_id": "J1", + "api_query_id": "pressure-query", + } + ], + ) + monkeypatch.setattr(module, "_prepare_burst_inp", lambda network: str(tmp_path / "fake.inp")) + monkeypatch.setattr( + module, + "run_burst_location", + lambda **kwargs: captured.update(kwargs) or {"located_pipe": "Pipe-001"}, + ) + + def fake_scada_query(**kwargs): + scada_calls.append(kwargs) + return { + "pressure-query": [ + {"time": kwargs["start_time"], "value": 20.0}, + {"time": kwargs["end_time"], "value": 22.0}, + ] + } + + def fake_realtime_query(**kwargs): + realtime_calls.append(kwargs) + return { + "J1": [ + {"time": kwargs["start_time"], "value": 10.0}, + {"time": kwargs["end_time"], "value": 12.0}, + ] + } + + monkeypatch.setattr( + module.InternalQueries, + "query_scada_by_ids_timerange", + staticmethod(fake_scada_query), + ) + monkeypatch.setattr( + module.InternalQueries, + "query_realtime_simulation_by_ids_timerange", + staticmethod(fake_realtime_query), + ) + + result = module.run_burst_location_by_network( + network="tjwater", + username="testuser", + data_source="monitoring", + burst_leakage=1.0, + scada_burst_start=datetime(2025, 1, 1, 8, 0, 0), + scada_burst_end=datetime(2025, 1, 1, 9, 0, 0), + ) + + assert result["observed_source"] == "scada_burst_realtime_normal_timerange" + assert len(scada_calls) == 1 + assert len(realtime_calls) == 1 + assert captured["burst_pressure"]["J1"] == pytest.approx(21.0) + assert captured["normal_pressure"]["J1"] == pytest.approx(11.0)