Skip to content

Commit 113803a

Browse files
committed
test + fix
1 parent cec5c86 commit 113803a

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

nowcasting_datamodel/read/read.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -151,19 +151,6 @@ def get_latest_forecast(
151151
query = query.filter(LocationSQL.gsp_id == gsp_id)
152152
order_by_items.append(LocationSQL.gsp_id)
153153

154-
# get the correct forecast value table
155-
if historic:
156-
data_model_forecast_value = ForecastValueLatestSQL
157-
else:
158-
data_model_forecast_value = ForecastValueSQL
159-
160-
# join forecast values
161-
query = query.join(data_model_forecast_value)
162-
163-
# filter by target time
164-
if start_target_time is not None:
165-
query = query.filter(data_model_forecast_value.target_time >= start_target_time)
166-
167154
order_by_items.append(ForecastSQL.created_utc.desc())
168155

169156
# this make the newest ones comes to the top
@@ -172,6 +159,23 @@ def get_latest_forecast(
172159
# get all results
173160
forecasts = query.first()
174161

162+
# filter on target time
163+
if start_target_time is not None:
164+
165+
# get the correct forecast value table
166+
if historic:
167+
data_model_forecast_value = ForecastValueLatestSQL
168+
else:
169+
data_model_forecast_value = ForecastValueSQL
170+
171+
forecast_values = session.query(data_model_forecast_value).\
172+
filter(data_model_forecast_value.target_time >= start_target_time).\
173+
filter(forecasts.id == data_model_forecast_value.forecast_id). \
174+
order_by(data_model_forecast_value.target_time).\
175+
all()
176+
177+
forecasts.forecast_values_latest = forecast_values
178+
175179
# sort list
176180
if forecasts is not None:
177181
if forecasts.forecast_values_latest is not None:

tests/read/test_read.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def test_read_target_time(db_session):
109109
session=db_session,
110110
gsp_id=f.location.gsp_id,
111111
historic=True,
112-
start_target_time=datetime(2022, 1, 1),
112+
start_target_time=datetime(2022, 1, 1, 0, 30),
113113
)
114114
assert forecast_read is not None
115115
assert forecast_read.location.gsp_id == f.location.gsp_id
@@ -341,3 +341,4 @@ def test_update_latest_input_data_last_updated_freeze_no_data(db_session):
341341
input_data_last_updated = get_latest_input_data_last_updated(session=db_session)
342342
assert input_data_last_updated.pv.replace(tzinfo=None) == now.replace(tzinfo=None)
343343
assert input_data_last_updated.gsp.replace(tzinfo=None) == datetime(1960, 1, 1)
344+
assert len(forecast_read.forecast_values_latest) == 1

0 commit comments

Comments
 (0)