mirror of
https://github.com/Hestia-Homes/Model.git
synced 2026-06-08 11:17:27 +00:00
Replaces the handler's whole-pipeline Session (one transaction across all three stages, connection pinned during Ingestion's external IO) with a Unit-of-Work per stage (ADR-0012, added here). Each stage runs its batch in one unit and commits once; any property raising aborts the batch and the subtask fails noisily. - BaselineOrchestrator(unit_of_work, rebaseliner): one unit for the batch, commit once. Raise on a pre-SAP10 property leaves the unit uncommitted. - IngestionOrchestrator(unit_of_work, epc_fetcher, geospatial_repo, solar_fetcher): fetch/write split — phase 1 fetches the whole batch (EPC / coords / solar) with NO unit open; phase 2 writes in one unit and commits. The connection is never held during external IO. Geospatial S3 repo stays injected (reference data, not transactional). - Handler: module-scoped engine (pool reused across warm invocations) + a UoW factory; whole-pipeline `with Session` gone. `build_first_run_pipeline` composes on the factory. Source clients still behind the raising seam. - ADR-0012 records the decision (per-stage boundary, all-or-nothing batch, idempotent re-run, fetch/write split, module-scoped engine). Modelling stub left untouched (no-op, no DB) per the ADR. Tests: orchestrators on a shared FakeUnitOfWork (assert persisted batch + exactly-once commit + no-commit-on-raise). New real-DB E2E integration test: real PostgresUnitOfWork, Ingestion writes the EPC → Baseline reads it back through the repo → re-run replaces, not duplicates (1 EPC row, 1 baseline row after two runs). 121 pass in tests/; pyright strict clean; AAA. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
110 lines
3.6 KiB
Python
110 lines
3.6 KiB
Python
"""In-memory fakes for orchestrator unit tests (no DB, no network).
|
|
|
|
A `FakeUnitOfWork` exposes dict-backed fake repos and records commits, so a
|
|
test can drive an orchestrator and then assert what was persisted and that the
|
|
batch committed exactly once (ADR-0012)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from types import TracebackType
|
|
from typing import Any, Optional
|
|
|
|
from datatypes.epc.domain.epc_property_data import EpcPropertyData
|
|
from domain.baseline.baseline_performance import BaselinePerformance
|
|
from domain.property.property import Property
|
|
from repositories.baseline.baseline_repository import BaselineRepository
|
|
from repositories.epc.epc_repository import EpcRepository
|
|
from repositories.property.property_repository import PropertyRepository
|
|
from repositories.solar.solar_repository import SolarRepository
|
|
from repositories.unit_of_work import UnitOfWork
|
|
|
|
|
|
class FakePropertyRepo(PropertyRepository):
|
|
def __init__(self, by_id: dict[int, Property]) -> None:
|
|
self._by_id = by_id
|
|
|
|
def get(self, property_id: int) -> Property:
|
|
return self._by_id[property_id]
|
|
|
|
|
|
class FakeEpcRepo(EpcRepository):
|
|
def __init__(self, by_property: Optional[dict[int, EpcPropertyData]] = None) -> None:
|
|
self.saved: list[tuple[EpcPropertyData, Optional[int]]] = []
|
|
self._by_property = by_property or {}
|
|
|
|
def save(
|
|
self,
|
|
data: EpcPropertyData,
|
|
property_id: Optional[int] = None,
|
|
portfolio_id: Optional[int] = None,
|
|
) -> int:
|
|
self.saved.append((data, property_id))
|
|
if property_id is not None:
|
|
self._by_property[property_id] = data
|
|
return len(self.saved)
|
|
|
|
def get(self, epc_property_id: int) -> EpcPropertyData: # pragma: no cover
|
|
raise NotImplementedError
|
|
|
|
def get_for_property(self, property_id: int) -> Optional[EpcPropertyData]:
|
|
return self._by_property.get(property_id)
|
|
|
|
|
|
class FakeSolarRepo(SolarRepository):
|
|
def __init__(self) -> None:
|
|
self.saved: list[tuple[int, dict[str, Any]]] = []
|
|
|
|
def save(self, property_id: int, insights: dict[str, Any]) -> None:
|
|
self.saved.append((property_id, insights))
|
|
|
|
def get(self, property_id: int) -> Optional[dict[str, Any]]: # pragma: no cover
|
|
raise NotImplementedError
|
|
|
|
|
|
class FakeBaselineRepo(BaselineRepository):
|
|
def __init__(self) -> None:
|
|
self.saved: list[tuple[BaselinePerformance, int]] = []
|
|
|
|
def save(self, baseline: BaselinePerformance, property_id: int) -> int:
|
|
self.saved.append((baseline, property_id))
|
|
return len(self.saved)
|
|
|
|
def get_for_property(
|
|
self, property_id: int
|
|
) -> Optional[BaselinePerformance]: # pragma: no cover
|
|
raise NotImplementedError
|
|
|
|
|
|
class FakeUnitOfWork(UnitOfWork):
|
|
"""A unit that holds in-memory repos and counts commits."""
|
|
|
|
def __init__(
|
|
self,
|
|
*,
|
|
property: FakePropertyRepo,
|
|
epc: Optional[FakeEpcRepo] = None,
|
|
solar: Optional[FakeSolarRepo] = None,
|
|
baseline: Optional[FakeBaselineRepo] = None,
|
|
) -> None:
|
|
self.property = property
|
|
self.epc = epc or FakeEpcRepo()
|
|
self.solar = solar or FakeSolarRepo()
|
|
self.baseline = baseline or FakeBaselineRepo()
|
|
self.commits = 0
|
|
|
|
def __enter__(self) -> "FakeUnitOfWork":
|
|
return self
|
|
|
|
def __exit__(
|
|
self,
|
|
exc_type: Optional[type[BaseException]],
|
|
exc: Optional[BaseException],
|
|
tb: Optional[TracebackType],
|
|
) -> None:
|
|
return None
|
|
|
|
def commit(self) -> None:
|
|
self.commits += 1
|
|
|
|
def rollback(self) -> None:
|
|
return None
|