From c40bd57d76c0332be092715a6a3d2815be11debc Mon Sep 17 00:00:00 2001 From: Vlad Korolev Date: Tue, 1 Oct 2024 20:55:40 -0400 Subject: [PATCH] Fix pyodide test Fix python fixture creation Changed to pydantic custom parser Fix the test --- heat-stack/app/utils/pyodide.test.ts | 3 +-- rules-engine/src/rules_engine/pydantic_models.py | 6 +++++- rules-engine/tests/test_rules_engine/test_engine.py | 9 --------- rules-engine/tests/test_rules_engine/test_utils.py | 2 +- 4 files changed, 7 insertions(+), 13 deletions(-) diff --git a/heat-stack/app/utils/pyodide.test.ts b/heat-stack/app/utils/pyodide.test.ts index 16e7cd17..1dff3364 100644 --- a/heat-stack/app/utils/pyodide.test.ts +++ b/heat-stack/app/utils/pyodide.test.ts @@ -220,7 +220,6 @@ test('pyodide solves climate change', async () => { } const executePy = await pyodide.runPythonAsync(` - from datetime import date from rules_engine import parser from rules_engine.pydantic_models import ( FuelType, @@ -238,7 +237,7 @@ test('pyodide solves climate change', async () => { summaryInput = SummaryInput(**summaryInputFromJs) - temperatureInput = TemperatureInput(dates=[date.fromisoformat(date_) for date_ in temperatureInputFromJs["dates"]], temperatures=temperatureInputFromJs["temperatures"]) + temperatureInput = TemperatureInput(**temperatureInputFromJs) outputs = engine.get_outputs_natural_gas(summaryInput,temperatureInput, naturalGasInputRecords) diff --git a/rules-engine/src/rules_engine/pydantic_models.py b/rules-engine/src/rules_engine/pydantic_models.py index 6c98921f..fce42087 100644 --- a/rules-engine/src/rules_engine/pydantic_models.py +++ b/rules-engine/src/rules_engine/pydantic_models.py @@ -171,8 +171,12 @@ class NormalizedBillingPeriodRecord(NormalizedBillingPeriodRecordBase): whole_home_heat_loss_rate: Optional[float] = Field(frozen=True) +def _date_string_parser(rate: str) -> datetime: + return datetime.strptime(rate, "%Y-%m-%d") + + class TemperatureInput(BaseModel): - dates: list[datetime] + dates: list[Annotated[datetime, BeforeValidator(_date_string_parser)]] temperatures: list[float] diff --git a/rules-engine/tests/test_rules_engine/test_engine.py b/rules-engine/tests/test_rules_engine/test_engine.py index a0ce2233..40ca3346 100644 --- a/rules-engine/tests/test_rules_engine/test_engine.py +++ b/rules-engine/tests/test_rules_engine/test_engine.py @@ -242,21 +242,12 @@ def sample_normalized_billing_periods() -> list[NormalizedBillingPeriodRecordBas billing_periods = [ NormalizedBillingPeriodRecordBase( -<<<<<<< HEAD period_start_date=datetime.fromisoformat(x["period_start_date"]), period_end_date=datetime.fromisoformat(x["period_end_date"]), usage=x["usage"], inclusion_override=x["inclusion_override"], ) for x in billing_periods_dict -======= - period_start_date=date.fromisoformat(x["period_start_date"]), - period_end_date=date.fromisoformat(x["period_end_date"]), - usage=x["usage"], - analysis_type_override=x["analysis_type_override"], - inclusion_override=x["inclusion_override"], - ) for x in billing_periods_dict ->>>>>>> 333445e (Changed dates to datetimes and fixed all the tests) ] return billing_periods diff --git a/rules-engine/tests/test_rules_engine/test_utils.py b/rules-engine/tests/test_rules_engine/test_utils.py index 57c622f4..32a6c81b 100644 --- a/rules-engine/tests/test_rules_engine/test_utils.py +++ b/rules-engine/tests/test_rules_engine/test_utils.py @@ -172,7 +172,7 @@ def load_temperature_data(path: Path, weather_station: str) -> TemperatureInput: row: Any for row in reader: - dates.append(datetime.strptime(row["Date"], "%Y-%m-%d")) + dates.append(row["Date"]) temperatures.append(row[weather_station]) return TemperatureInput(dates=dates, temperatures=temperatures)