An attach-mode partial failure persists successes then records the failure 🟥

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Khalim Conn-Kowlessar 2026-07-07 11:55:49 +00:00
parent 7709518431
commit 3cf44d6409

View file

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