An attach-mode batch models under the supplied sub_task without children 🟥

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Khalim Conn-Kowlessar 2026-07-07 11:52:45 +00:00
parent f317233939
commit b840af11de

View file

@ -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
# ---------------------------------------------------------------------------