Skip to content

Commit

Permalink
Showing 8 changed files with 132 additions and 240 deletions.
34 changes: 14 additions & 20 deletions schemes/schemes/domain.py
Original file line number Diff line number Diff line change
@@ -32,7 +32,7 @@ def current_milestone(self) -> Milestone | None:
actual_milestones = [
revision.milestone
for revision in self._milestone_revisions
if revision.observation_type == ObservationType.ACTUAL and revision.effective_date_to is None
if revision.observation_type == ObservationType.ACTUAL and revision.effective.date_to is None
]
return sorted(actual_milestones)[-1] if actual_milestones else None

@@ -48,7 +48,7 @@ def funding_allocation(self) -> Decimal | None:
amounts = [
revision.amount
for revision in self._financial_revisions
if revision.type == FinancialType.FUNDING_ALLOCATION and revision.effective_date_to is None
if revision.type == FinancialType.FUNDING_ALLOCATION and revision.effective.date_to is None
]
return sum(amounts, Decimal(0)) if amounts else None

@@ -57,7 +57,7 @@ def spend_to_date(self) -> Decimal | None:
amounts = [
revision.amount
for revision in self._financial_revisions
if revision.type == FinancialType.SPENT_TO_DATE and revision.effective_date_to is None
if revision.type == FinancialType.SPENT_TO_DATE and revision.effective.date_to is None
]
return sum(amounts, Decimal(0)) if amounts else None

@@ -66,7 +66,7 @@ def change_control_adjustment(self) -> Decimal | None:
amounts = [
revision.amount
for revision in self._financial_revisions
if revision.source == DataSource.CHANGE_CONTROL and revision.effective_date_to is None
if revision.source == DataSource.CHANGE_CONTROL and revision.effective.date_to is None
]
return sum(amounts, Decimal(0)) if amounts else None

@@ -96,18 +96,20 @@ class FundingProgramme(Enum):

@dataclass(frozen=True)
class MilestoneRevision:
effective_date_from: date
effective_date_to: date | None
effective: DateRange
milestone: Milestone
observation_type: ObservationType
status_date: date


@dataclass(frozen=True)
class DateRange:
date_from: date
date_to: date | None

def __post_init__(self) -> None:
if not (self.effective_date_to is None or self.effective_date_from <= self.effective_date_to):
raise ValueError(
f"Effective date from '{self.effective_date_from}' must not "
f"be after effective date to '{self.effective_date_to}'"
)
if not (self.date_to is None or self.date_from <= self.date_to):
raise ValueError(f"From date '{self.date_from}' must not be after to date '{self.date_to}'")


class Milestone(IntEnum):
@@ -131,19 +133,11 @@ class ObservationType(Enum):

@dataclass(frozen=True)
class FinancialRevision:
effective_date_from: date
effective_date_to: date | None
effective: DateRange
type: FinancialType
amount: Decimal
source: DataSource

def __post_init__(self) -> None:
if not (self.effective_date_to is None or self.effective_date_from <= self.effective_date_to):
raise ValueError(
f"Effective date from '{self.effective_date_from}' must not "
f"be after effective date to '{self.effective_date_to}'"
)


class FinancialType(Enum):
EXPECTED_COST = auto()
15 changes: 7 additions & 8 deletions schemes/schemes/services.py
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@

from schemes.schemes.domain import (
DataSource,
DateRange,
FinancialRevision,
FinancialType,
FundingProgramme,
@@ -123,8 +124,8 @@ def add(self, *schemes: Scheme) -> None:
connection.execute(
insert(self._scheme_milestone_table).values(
capital_scheme_id=scheme.id,
effective_date_from=milestone_revision.effective_date_from,
effective_date_to=milestone_revision.effective_date_to,
effective_date_from=milestone_revision.effective.date_from,
effective_date_to=milestone_revision.effective.date_to,
milestone_id=MILESTONE_MAPPER.to_id(milestone_revision.milestone),
observation_type_id=OBSERVATION_TYPE_MAPPER.to_id(milestone_revision.observation_type),
status_date=milestone_revision.status_date,
@@ -134,8 +135,8 @@ def add(self, *schemes: Scheme) -> None:
connection.execute(
insert(self._capital_scheme_financial_table).values(
capital_scheme_id=scheme.id,
effective_date_from=financial_revision.effective_date_from,
effective_date_to=financial_revision.effective_date_to,
effective_date_from=financial_revision.effective.date_from,
effective_date_to=financial_revision.effective.date_to,
financial_type_id=FINANCIAL_TYPE_MAPPER.to_id(financial_revision.type),
amount=financial_revision.amount,
data_source_id=DATA_SOURCE_MAPPER.to_id(financial_revision.source),
@@ -231,8 +232,7 @@ def _capital_scheme_to_domain(row: Row[Any]) -> Scheme:
@staticmethod
def _scheme_milestone_to_domain(row: Row[Any]) -> MilestoneRevision:
return MilestoneRevision(
effective_date_from=row.effective_date_from,
effective_date_to=row.effective_date_to,
effective=DateRange(row.effective_date_from, row.effective_date_to),
milestone=MILESTONE_MAPPER.to_domain(row.milestone_id),
observation_type=OBSERVATION_TYPE_MAPPER.to_domain(row.observation_type_id),
status_date=row.status_date,
@@ -241,8 +241,7 @@ def _scheme_milestone_to_domain(row: Row[Any]) -> MilestoneRevision:
@staticmethod
def _capital_scheme_financial_to_domain(row: Row[Any]) -> FinancialRevision:
return FinancialRevision(
effective_date_from=row.effective_date_from,
effective_date_to=row.effective_date_to,
effective=DateRange(row.effective_date_from, row.effective_date_to),
type=FINANCIAL_TYPE_MAPPER.to_domain(row.financial_type_id),
amount=row.amount,
source=DATA_SOURCE_MAPPER.to_domain(row.data_source_id),
13 changes: 9 additions & 4 deletions schemes/schemes/views.py
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@
from schemes.authorities.services import AuthorityRepository
from schemes.schemes.domain import (
DataSource,
DateRange,
FinancialRevision,
FinancialType,
FundingProgramme,
@@ -238,8 +239,10 @@ class MilestoneRevisionRepr:

def to_domain(self) -> MilestoneRevision:
return MilestoneRevision(
effective_date_from=date.fromisoformat(self.effective_date_from),
effective_date_to=date.fromisoformat(self.effective_date_to) if self.effective_date_to else None,
effective=DateRange(
date_from=date.fromisoformat(self.effective_date_from),
date_to=date.fromisoformat(self.effective_date_to) if self.effective_date_to else None,
),
milestone=self._milestone_to_domain(self.milestone),
observation_type=self._observation_type_to_domain(self.observation_type),
status_date=date.fromisoformat(self.status_date),
@@ -279,8 +282,10 @@ class FinancialRevisionRepr:

def to_domain(self) -> FinancialRevision:
return FinancialRevision(
effective_date_from=date.fromisoformat(self.effective_date_from),
effective_date_to=date.fromisoformat(self.effective_date_to) if self.effective_date_to else None,
effective=DateRange(
date_from=date.fromisoformat(self.effective_date_from),
date_to=date.fromisoformat(self.effective_date_to) if self.effective_date_to else None,
),
type=self._financial_type_to_domain(self.type),
amount=Decimal(self.amount),
source=self._data_source_to_domain(self.source),
7 changes: 3 additions & 4 deletions tests/integration/test_authorities.py
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@
from schemes.authorities.services import AuthorityRepository
from schemes.schemes.domain import (
DataSource,
DateRange,
FinancialRevision,
FinancialType,
FundingProgramme,
@@ -160,8 +161,7 @@ def test_add_schemes_milestone_revisions(self, schemes: SchemeRepository, client
assert scheme1.id == 1
assert scheme1.milestone_revisions == [
MilestoneRevision(
effective_date_from=date(2020, 1, 1),
effective_date_to=None,
effective=DateRange(date(2020, 1, 1), None),
milestone=Milestone.DETAILED_DESIGN_COMPLETED,
observation_type=ObservationType.ACTUAL,
status_date=date(2020, 1, 1),
@@ -195,8 +195,7 @@ def test_add_schemes_financial_revisions(self, schemes: SchemeRepository, client
assert scheme1.id == 1
assert scheme1.financial_revisions == [
FinancialRevision(
effective_date_from=date(2020, 1, 1),
effective_date_to=None,
effective=DateRange(date(2020, 1, 1), None),
type=FinancialType.FUNDING_ALLOCATION,
amount=Decimal("100000"),
source=DataSource.ATF4_BID,
22 changes: 8 additions & 14 deletions tests/integration/test_scheme.py
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@
from schemes.authorities.services import AuthorityRepository
from schemes.schemes.domain import (
DataSource,
DateRange,
FinancialRevision,
FinancialType,
FundingProgramme,
@@ -83,8 +84,7 @@ def test_scheme_shows_overview(schemes: SchemeRepository, client: FlaskClient) -
scheme.funding_programme = FundingProgramme.ATF4
scheme.update_milestone(
MilestoneRevision(
effective_date_from=date(2020, 1, 1),
effective_date_to=None,
effective=DateRange(date(2020, 1, 1), None),
milestone=Milestone.DETAILED_DESIGN_COMPLETED,
observation_type=ObservationType.ACTUAL,
status_date=date(2020, 1, 1),
@@ -119,26 +119,23 @@ def test_scheme_shows_funding(schemes: SchemeRepository, client: FlaskClient) ->
scheme = Scheme(id_=1, name="Wirral Package", authority_id=1)
scheme.update_financial(
FinancialRevision(
effective_date_from=date(2020, 1, 1),
effective_date_to=None,
effective=DateRange(date(2020, 1, 1), None),
type=FinancialType.FUNDING_ALLOCATION,
amount=Decimal("100000"),
source=DataSource.ATF4_BID,
)
)
scheme.update_financial(
FinancialRevision(
effective_date_from=date(2020, 1, 1),
effective_date_to=None,
effective=DateRange(date(2020, 1, 1), None),
type=FinancialType.SPENT_TO_DATE,
amount=Decimal("50000"),
source=DataSource.ATF4_BID,
)
)
scheme.update_financial(
FinancialRevision(
effective_date_from=date(2020, 1, 1),
effective_date_to=None,
effective=DateRange(date(2020, 1, 1), None),
type=FinancialType.CHANGE_CONTROL_FUNDING_REALLOCATION,
amount=Decimal("10000"),
source=DataSource.CHANGE_CONTROL,
@@ -160,26 +157,23 @@ def test_scheme_shows_zero_funding(schemes: SchemeRepository, client: FlaskClien
scheme = Scheme(id_=1, name="Wirral Package", authority_id=1)
scheme.update_financial(
FinancialRevision(
effective_date_from=date(2020, 1, 1),
effective_date_to=None,
effective=DateRange(date(2020, 1, 1), None),
type=FinancialType.FUNDING_ALLOCATION,
amount=Decimal("0"),
source=DataSource.ATF4_BID,
)
)
scheme.update_financial(
FinancialRevision(
effective_date_from=date(2020, 1, 1),
effective_date_to=None,
effective=DateRange(date(2020, 1, 1), None),
type=FinancialType.SPENT_TO_DATE,
amount=Decimal("0"),
source=DataSource.ATF4_BID,
)
)
scheme.update_financial(
FinancialRevision(
effective_date_from=date(2020, 1, 1),
effective_date_to=None,
effective=DateRange(date(2020, 1, 1), None),
type=FinancialType.CHANGE_CONTROL_FUNDING_REALLOCATION,
amount=Decimal("0"),
source=DataSource.CHANGE_CONTROL,
Loading

0 comments on commit 2a9076f

Please sign in to comment.