Skip to content

Commit

Permalink
Added start and end dates to parser output (#197)
Browse files Browse the repository at this point in the history
* added start and end dates to parser output

Co-authored-by: Debajyoti Debnath <[email protected]>
Co-authored-by: eriksynn <[email protected]>
Co-authored-by: AdamFinkle <[email protected]>
Co-authored-by: Jonathan Kwan <[email protected]>
Co-authored-by: thatoldplatitude <[email protected]>

* added comment tagging mypy issue

---------

Co-authored-by: Debajyoti Debnath <[email protected]>
Co-authored-by: eriksynn <[email protected]>
Co-authored-by: AdamFinkle <[email protected]>
Co-authored-by: Jonathan Kwan <[email protected]>
Co-authored-by: thatoldplatitude <[email protected]>
  • Loading branch information
6 people authored Jun 5, 2024
1 parent 433ef81 commit 0df7f6e
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
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

0 comments on commit 0df7f6e

Please sign in to comment.