Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdudfield committed Nov 29, 2024
1 parent 936dee9 commit f32e1c1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
19 changes: 12 additions & 7 deletions india_forecast_app/adjuster.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def get_me_values(
return me_df


def zero_out_nighttime(db_session, forecast_values_df: pd.DataFrame, site_uuid: str):
def zero_out_nighttime(db_session, forecast_values_df: pd.DataFrame, site_uuid: str, elevation_limit:Optional[float]= 0):

Check failure on line 143 in india_forecast_app/adjuster.py

View workflow job for this annotation

GitHub Actions / lint_and_test / Lint the code and run the tests

Ruff (D417)

india_forecast_app/adjuster.py:143:5: D417 Missing argument description in the docstring for `zero_out_nighttime`: `elevation_limit`

Check failure on line 143 in india_forecast_app/adjuster.py

View workflow job for this annotation

GitHub Actions / lint_and_test / Lint the code and run the tests

Ruff (E501)

india_forecast_app/adjuster.py:143:101: E501 Line too long (121 > 100)
"""
Zero out nighttime values in forecast
Expand All @@ -163,12 +163,17 @@ def zero_out_nighttime(db_session, forecast_values_df: pd.DataFrame, site_uuid:
latitude=latitude,
longitude=longitude,
)
elevation = solpos["elevation"]
elevation = solpos[["elevation"]]

# merge with forecast_values_df on start_utc
forecast_values_df = forecast_values_df.merge(elevation, on="start_utc", how="left")

# zero out nighttime values
forecast_values_df.loc[forecast_values_df['elevation'] < elevation_limit, "forecast_power_kw"] = 0

Check failure on line 172 in india_forecast_app/adjuster.py

View workflow job for this annotation

GitHub Actions / lint_and_test / Lint the code and run the tests

Ruff (E501)

india_forecast_app/adjuster.py:172:101: E501 Line too long (106 > 100)

# drop elevation column
forecast_values_df.drop(columns=["elevation"], inplace=True)

# zero out nighttime values
forecast_values_df["forecast_power_kw"] = forecast_values_df.apply(
lambda x: 0 if elevation <= 0 else x["forecast_power_kw"], axis=1
)

return forecast_values_df

Expand Down Expand Up @@ -236,7 +241,7 @@ def adjust_forecast_with_adjuster(
# make sure there are no positive values at nighttime
forecast_values_df_adjust = zero_out_nighttime(
db_session=db_session,
forecast_values_df=forecast_values_df,
forecast_values_df=forecast_values_df_adjust,
site_uuid=forecast_meta["site_uuid"],
)

Expand Down
34 changes: 33 additions & 1 deletion tests/test_adjuster.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@
from datetime import datetime

import pandas as pd
import pytest

from india_forecast_app.adjuster import adjust_forecast_with_adjuster, get_me_values
from india_forecast_app.adjuster import (
adjust_forecast_with_adjuster,
get_me_values,
zero_out_nighttime,
)


def test_get_me_values_no_values(db_session, sites):
Expand Down Expand Up @@ -104,3 +109,30 @@ def test_adjust_forecast_with_adjuster_no_values(db_session, sites):

assert len(forecast_values_df) == 5
assert forecast_values_df["forecast_power_kw"].sum() == 15


@pytest.mark.parametrize("asset_type", ["pv", "wind"])
def test_zero_out_nighttime(asset_type, db_session, sites):

Check failure on line 115 in tests/test_adjuster.py

View workflow job for this annotation

GitHub Actions / lint_and_test / Lint the code and run the tests

Ruff (D103)

tests/test_adjuster.py:115:5: D103 Missing docstring in public function
forecast_values_df = pd.DataFrame(
{
"forecast_power_kw": [1, 2, 3, 4, 5],
"horizon_minutes": [15, 30, 45, 60, 1200],
"start_utc": [
pd.Timestamp("2024-11-01 23:00:00") + pd.Timedelta(f"{i}H") for i in range(0, 5)
],
}
)

sites[0].asset_type = asset_type

forecast_values_df = zero_out_nighttime(
db_session, forecast_values_df=forecast_values_df, site_uuid=sites[0].site_uuid
)

assert len(forecast_values_df) == 5
night_sum = forecast_values_df["forecast_power_kw"][0:2].sum()
if asset_type == "pv":
assert night_sum == 0
else:
assert night_sum > 0
assert forecast_values_df["forecast_power_kw"][2:].sum() > 0

0 comments on commit f32e1c1

Please sign in to comment.