Skip to content

Commit

Permalink
Check if the selected widget is in the form (#75)
Browse files Browse the repository at this point in the history
* Check if the selected widget is in the form
* Test error (#76)
Co-authored-by: Mattia Almansi <m.almansi@bopen.eu>
Co-authored-by: Eddy Comyn-Platt <53045993+EddyCMWF@users.noreply.github.com>
Co-authored-by: Mattia Almansi <m.almansi@bopen.eu>
  • Loading branch information
ecmwf-cobarzan authored Jan 9, 2024
1 parent 9fb29b9 commit 06bc1e1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
4 changes: 3 additions & 1 deletion cads_adaptors/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def apply_constraints_in_old_cds_fashion(
per_constraint_result[selected_widget_name][
widget_name
] = set(widget_options)
else:
elif selected_widget_name in form.keys():
# factoring in Category 2 constraints
if selected_widget_name not in per_constraint_result:
per_constraint_result[selected_widget_name] = {}
Expand All @@ -273,6 +273,8 @@ def apply_constraints_in_old_cds_fashion(
per_constraint_result[selected_widget_name][widget_name] = set(
widget_options
)
else:
raise ParameterError(f"invalid param '{selected_widget_name}'")

for widget_name in form:
per_constraint_result_agg: set[Any] = set()
Expand Down
20 changes: 20 additions & 0 deletions tests/test_10_constraints.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import Any

import pytest

from cads_adaptors import constraints


Expand Down Expand Up @@ -82,6 +84,24 @@ def test_apply_constraints() -> None:
] == ["1"]


@pytest.mark.parametrize(
"selections",
(
{"foo": {"500"}},
{"foo": {"500"}, "level": {"500"}},
),
)
def test_apply_constraints_errors(selections: dict[str, set[Any]]) -> None:
form = {"level": {"500", "850"}, "param": {"Z", "T"}, "number": {"1"}}

raw_constraints = [
{"level": {"500"}, "param": {"Z"}},
{"level": {"850"}, "param": {"T"}},
]
with pytest.raises(constraints.ParameterError, match="invalid param 'foo'"):
constraints.apply_constraints(form, selections, raw_constraints)


def test_parse_constraints() -> None:
raw_constraints: list[dict[str, list[Any]]] = [
{"level": ["500"], "param": ["Z", "T"], "step": ["24", "36", "48"]},
Expand Down

0 comments on commit 06bc1e1

Please sign in to comment.