From b840af11de0086e4bbeb3a373fc4c0a931242d6a Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Tue, 7 Jul 2026 11:52:45 +0000 Subject: [PATCH] =?UTF-8?q?An=20attach-mode=20batch=20models=20under=20the?= =?UTF-8?q?=20supplied=20sub=5Ftask=20without=20children=20=F0=9F=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Fable 5 --- .../modelling_e2e/test_handler.py | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/tests/applications/modelling_e2e/test_handler.py b/tests/applications/modelling_e2e/test_handler.py index 80c44aeeb..fe7ef7815 100644 --- a/tests/applications/modelling_e2e/test_handler.py +++ b/tests/applications/modelling_e2e/test_handler.py @@ -378,6 +378,66 @@ def test_handler_creates_one_child_subtask_per_property_id() -> None: assert [i["property_id"] for i in inputs_per_subtask] == [pid1, pid2, pid3] +def test_attach_mode_models_the_batch_without_child_subtasks() -> None: + """A batch carrying task_id + subtask_id (a Modelling Run, ADR-0055) runs + entirely under the distributor's pre-created sub_task: no per-property + child SubTasks are created, and the batch still persists.""" + # Arrange + pid1, pid2 = 111, 222 + mock_engine = _engine_mock([pid1, pid2], [1001, 1002], [POSTCODE, POSTCODE]) + mock_orch = _mock_orchestrator() + task_id = uuid4() + + with ExitStack() as stack: + stack.enter_context(patch("applications.modelling_e2e.handler.os.environ", _ENV)) + stack.enter_context( + patch("applications.modelling_e2e.handler._get_engine", return_value=mock_engine) + ) + stack.enter_context( + patch("applications.modelling_e2e.handler.EpcClientService") + ).return_value.get_by_uprn.return_value = MagicMock() + stack.enter_context(patch("applications.modelling_e2e.handler.GeospatialS3Repository")) + stack.enter_context(patch("applications.modelling_e2e.handler.GoogleSolarApiClient")) + stack.enter_context( + patch("applications.modelling_e2e.handler._spatial_for", return_value=None) + ) + stack.enter_context( + patch("applications.modelling_e2e.handler._solar_insights_for", return_value=None) + ) + stack.enter_context( + patch("applications.modelling_e2e.handler.overlays_from", return_value=[]) + ) + stack.enter_context( + patch("applications.modelling_e2e.handler.PropertyOverridesPostgresReader") + ).return_value.overrides_for_many.return_value = {} + stack.enter_context( + patch("applications.modelling_e2e.handler.ScenarioPostgresRepository") + ).return_value.get_many.return_value = [MagicMock()] + stack.enter_context(patch("applications.modelling_e2e.handler.catalogue_snapshot_with_off_catalogue_overrides")) + stack.enter_context(patch("applications.modelling_e2e.handler.Session")) + stack.enter_context( + patch("applications.modelling_e2e.handler.run_modelling", return_value=_plan_mock()) + ) + MockUoW = stack.enter_context(patch("applications.modelling_e2e.handler.PostgresUnitOfWork")) + mock_uow = MagicMock() + MockUoW.return_value.__enter__.return_value = mock_uow + MockUoW.return_value.__exit__.return_value = False + + # Act + from applications.modelling_e2e.handler import handler + handler.__wrapped__( # type: ignore[attr-defined] + {"task_id": str(task_id), "subtask_id": str(uuid4()), + "property_ids": [pid1, pid2], "portfolio_id": PORTFOLIO_ID, + "scenario_id": SCENARIO_ID, "refetch_solar": False, "dry_run": False}, + None, mock_orch, task_id, + ) + + # Assert — no child SubTasks; the whole batch persisted in the one UoW + mock_orch.run_subtasks.assert_not_called() + plan_requests = mock_uow.plan.save_batch.call_args.args[0] + assert [r.property_id for r in plan_requests] == [pid1, pid2] + + # --------------------------------------------------------------------------- # Lodged EPC path # ---------------------------------------------------------------------------