Skip to content

Commit

Permalink
fix validation rules
Browse files Browse the repository at this point in the history
Signed-off-by: Victor Garcia Reolid <victor@seita.nl>
  • Loading branch information
victorgarcia98 committed Dec 19, 2023
1 parent 70844e8 commit 5bd5d98
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
22 changes: 14 additions & 8 deletions flexmeasures/data/schemas/scheduling/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ class StorageFlexModelSchema(Schema):
"%", data_key="discharging-efficiency", required=False
)

roundtrip_efficiency = EfficiencyField(data_key="roundtrip-efficiency")
roundtrip_efficiency = EfficiencyField(
data_key="roundtrip-efficiency", required=False
)

storage_efficiency = EfficiencyField(data_key="storage-efficiency")
prefer_charging_sooner = fields.Bool(data_key="prefer-charging-sooner")
Expand Down Expand Up @@ -146,18 +148,22 @@ def check_whether_targets_exceed_max_planning_horizon(self, data: dict, **kwargs
)

@validates_schema
def check_whether_efficiency_pair_is_incomplete(self, data: dict, **kwargs):
def check_redundant_efficiencies(self, data: dict, **kwargs):
"""
Check if one of the efficiency fields is missing.
Check that none of the following cases occurs:
(1) flex-model contains both a round-trip efficiency and a charging efficiency
(2) flex-model contains both a round-trip efficiency and a discharging efficiency
(3) flex-model contains a round-trip efficiency, a charging efficiency and a discharging efficiency
:raise: ValidationError
"""
fields = ["charging_efficiency", "discharging_efficiency"]

if any(fields[b] in data and fields[1 - b] not in data for b in [0, 1]):
raise ValidationError(
"`charging-efficiency` and `discharge-efficiency` need to be provided as a pair. Only one of them was povided"
)
for field in ["charging_efficiency", "discharging_efficiency"]:
if field in data and "roundtrip_efficiency" in data:
raise ValidationError(
f"Fields `{field}` and `rountrip_efficiency` are mutually exclusive."
)

@post_load
def post_load_sequence(self, data: dict, **kwargs) -> dict:
Expand Down
17 changes: 12 additions & 5 deletions flexmeasures/data/schemas/tests/test_scheduling.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,30 @@ def test_process_scheduler_flex_model_process_type(db, app, setup_dummy_sensors)
[
"charging-efficiency",
],
True,
False,
),
(
[
"discharging-efficiency",
],
True,
False,
),
(["discharging-efficiency", "charging-efficiency"], False),
(
["discharging-efficiency", "charging-efficiency", "roundtrip_efficiency"],
True,
),
(["discharging-efficiency", "roundtrip-efficiency"], True),
(["charging-efficiency", "roundtrip-efficiency"], True),
(["roundtrip-efficiency"], False),
],
)
def test_efficiency_pair(
db, app, setup_dummy_sensors, setup_efficiency_sensors, fields, fails
):
"""
Check that the fields `charging-efficiency` and `discharging-efficiency` always go together.
If one of them is missing, the validation should raise an error.
Check that the efficiency can only be defined by the roundtrip efficiency field
or by the (dis)charging efficiency fields.
"""

sensor1, _ = setup_dummy_sensors
Expand All @@ -117,7 +124,7 @@ def load_schema():
"soc-at-start": 0,
}
for f in fields:
flex_model[f] = {"sensor": setup_efficiency_sensors.id}
flex_model[f] = "90%"

schema.load(flex_model)

Expand Down

0 comments on commit 5bd5d98

Please sign in to comment.