Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added start and end dates to parser output #197

Merged
merged 2 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion rules-engine/src/rules_engine/pydantic_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
from dataclasses import dataclass
from datetime import date
from enum import Enum
from functools import cached_property
from typing import Annotated, Any, Optional, Sequence

from pydantic import BaseModel, BeforeValidator, ConfigDict, Field
from pydantic import BaseModel, BeforeValidator, ConfigDict, Field, computed_field


class AnalysisType(Enum):
Expand Down Expand Up @@ -106,6 +107,38 @@ class NaturalGasBillingInput(BaseModel):

records: Sequence[NaturalGasBillingRecordInput]

# Suppress mypy error when computed_field is used with cached_property; see https://github.com/python/mypy/issues/1362
@computed_field # type: ignore[misc]
@cached_property
def overall_start_date(self) -> date:
if len(self.records) == 0:
raise ValueError(
"Natural gas billing records cannot be empty."
+ "Could not calculate overall start date from empty natural gas billing records."
+ "Try again with non-empty natural gas billing records."
)

min_date = date.max
for record in self.records:
min_date = min(min_date, record.period_start_date)
return min_date

# Suppress mypy error when computed_field is used with cached_property; see https://github.com/python/mypy/issues/1362
@computed_field # type: ignore[misc]
@cached_property
def overall_end_date(self) -> date:
if len(self.records) == 0:
raise ValueError(
"Natural gas billing records cannot be empty."
+ "Could not calculate overall start date from empty natural gas billing records."
+ "Try again with non-empty natural gas billing records."
)

max_date = date.min
for record in self.records:
max_date = max(max_date, record.period_end_date)
return max_date


class NormalizedBillingPeriodRecordBase(BaseModel):
"""
Expand Down
53 changes: 53 additions & 0 deletions rules-engine/tests/test_rules_engine/test_pydantic_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from datetime import date

import pytest

from rules_engine.pydantic_models import (
NaturalGasBillingInput,
NaturalGasBillingRecordInput,
)

_EXAMPLE_VALID_RECORDS = NaturalGasBillingInput(
records=[
NaturalGasBillingRecordInput(
period_start_date=date(2020, 1, 1),
period_end_date=date(2020, 1, 31),
usage_therms=10,
inclusion_override=None,
),
NaturalGasBillingRecordInput(
period_start_date=date(2020, 2, 1),
period_end_date=date(2020, 2, 28),
usage_therms=10,
inclusion_override=None,
),
]
)

_EXAMPLE_INVALID_RECORDS = NaturalGasBillingInput(
records=[] # create billing input with no records
)


def test_natural_gas_billing_input_overall_start_date():
expected_overall_start_date = date(2020, 1, 1)
actual_overall_start_date = _EXAMPLE_VALID_RECORDS.overall_start_date

assert expected_overall_start_date == actual_overall_start_date


def test_natural_gas_billing_input_overall_end_date():
expected_overall_end_date = date(2020, 2, 28)
actual_overall_end_date = _EXAMPLE_VALID_RECORDS.overall_end_date

assert expected_overall_end_date == actual_overall_end_date


def test_natural_gas_billing_input_overall_start_date_invalid():
with pytest.raises(ValueError):
_EXAMPLE_INVALID_RECORDS.overall_start_date


def test_natural_gas_billing_input_overall_end_date_invalid():
with pytest.raises(ValueError):
_EXAMPLE_INVALID_RECORDS.overall_end_date
Loading