Skip to content

Commit

Permalink
Fix pyodide test
Browse files Browse the repository at this point in the history
Fix python fixture creation

Changed to pydantic custom parser

Fix the test
  • Loading branch information
vladistan committed Oct 2, 2024
1 parent 0401181 commit c40bd57
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 13 deletions.
3 changes: 1 addition & 2 deletions heat-stack/app/utils/pyodide.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion rules-engine/src/rules_engine/pydantic_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]


Expand Down
9 changes: 0 additions & 9 deletions rules-engine/tests/test_rules_engine/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion rules-engine/tests/test_rules_engine/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit c40bd57

Please sign in to comment.