The plan repository reports which properties already have a default plan 🟥

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Khalim Conn-Kowlessar 2026-07-07 16:08:30 +00:00
parent 39beb303ef
commit 015db4275b
4 changed files with 37 additions and 0 deletions

View file

@ -45,6 +45,9 @@ class PlanPostgresRepository(PlanRepository):
[PlanSaveRequest(plan, property_id=property_id, scenario_id=scenario_id, portfolio_id=portfolio_id, is_default=is_default)]
)[0]
def property_ids_with_default_plans(self, property_ids: list[int]) -> set[int]:
raise NotImplementedError
def save_batch(self, requests: list[PlanSaveRequest]) -> list[int]:
"""Persist all Plans in three statements regardless of batch size.

View file

@ -54,3 +54,11 @@ class PlanRepository(ABC):
UPDATE only when at least one request has ``is_default=True``. Keeps
prior Plans as history (ADR-0017)."""
...
@abstractmethod
def property_ids_with_default_plans(self, property_ids: list[int]) -> set[int]:
"""The subset of ``property_ids`` that already have a default Plan.
A property outside this set is receiving its first Plan, which becomes
its default whatever the Scenario says (the legacy ``p.is_new`` rule
readers need one default Plan per property)."""
...

View file

@ -240,6 +240,11 @@ class FakePlanRepository(PlanRepository):
for r in requests
]
def property_ids_with_default_plans(self, property_ids: list[int]) -> set[int]:
# The fake store does not track defaults; every property reads as
# first-time (no default yet).
return set()
class _UnsetProductRepo(ProductRepository):
"""Default for a `FakeUnitOfWork` built without a catalogue — raises if a

View file

@ -206,3 +206,24 @@ def test_rerun_as_non_default_does_not_demote_the_prior_default(
assert len(plan_rows) == 2
assert by_id[first_id].is_default is True
def test_reports_which_properties_already_have_a_default_plan(
db_engine: Engine,
) -> None:
# Arrange — property 20 has a default Plan, 21 only a non-default one,
# 22 has no Plans at all
with Session(db_engine) as session:
repo = PlanPostgresRepository(session)
repo.save(_plan(), property_id=20, scenario_id=7, portfolio_id=1, is_default=True)
repo.save(_plan(), property_id=21, scenario_id=7, portfolio_id=1, is_default=False)
session.commit()
# Act
with Session(db_engine) as session:
have_default = PlanPostgresRepository(session).property_ids_with_default_plans(
[20, 21, 22]
)
# Assert — only the property with a default Plan is reported
assert have_default == {20}