From 3cf44d64099d587e06ad2a9c50c20847191853b9 Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Tue, 7 Jul 2026 11:55:49 +0000 Subject: [PATCH] =?UTF-8?q?An=20attach-mode=20partial=20failure=20persists?= =?UTF-8?q?=20successes=20then=20records=20the=20failure=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 | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/tests/applications/modelling_e2e/test_handler.py b/tests/applications/modelling_e2e/test_handler.py index fe7ef7815..f2e33187b 100644 --- a/tests/applications/modelling_e2e/test_handler.py +++ b/tests/applications/modelling_e2e/test_handler.py @@ -438,6 +438,79 @@ def test_attach_mode_models_the_batch_without_child_subtasks() -> None: assert [r.property_id for r in plan_requests] == [pid1, pid2] +def test_attach_mode_partial_failure_persists_successes_then_records_failure() -> None: + """In attach mode a failing property doesn't stop its siblings: the batch + flushes the successes, then raises SubTaskFailure carrying + {succeeded, failed} so the sub_task record holds the outcome (ADR-0055).""" + # Arrange — property 222's EPC fetch blows up; 111 models fine + pid_ok, pid_bad = 111, 222 + mock_engine = _engine_mock([pid_ok, pid_bad], [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) + ) + epc_client = stack.enter_context( + patch("applications.modelling_e2e.handler.EpcClientService") + ).return_value + epc_client.get_by_uprn.side_effect = [ + MagicMock(), + RuntimeError("gov API exploded"), + ] + 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 + from domain.tasks.subtasks import SubTaskFailure + + with pytest.raises(SubTaskFailure) as raised: + handler.__wrapped__( # type: ignore[attr-defined] + {"task_id": str(task_id), "subtask_id": str(uuid4()), + "property_ids": [pid_ok, pid_bad], "portfolio_id": PORTFOLIO_ID, + "scenario_id": SCENARIO_ID, "refetch_solar": False, + "dry_run": False}, + None, mock_orch, task_id, + ) + + # Assert — the success flushed before the failure was recorded + plan_requests = mock_uow.plan.save_batch.call_args.args[0] + assert [r.property_id for r in plan_requests] == [pid_ok] + details = raised.value.details + assert details is not None + assert details["succeeded"] == 1 + assert details["failed"] == [ + {"property_id": pid_bad, "error": "gov API exploded"} + ] + + # --------------------------------------------------------------------------- # Lodged EPC path # ---------------------------------------------------------------------------