Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix constraints in get_unsupported_vars #6

Merged
merged 8 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions cads_adaptors/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@

from . import translators

SUPPORTED_CONSTRAINTS = [
"StringListWidget",
"StringListArrayWidget",
"StringChoiceWidget",
]


class ParameterError(TypeError):
pass
Expand All @@ -24,7 +18,7 @@ def get_unsupported_vars(
ogc_form = [ogc_form]
unsupported_vars = []
for schema in ogc_form:
if schema["type"] not in SUPPORTED_CONSTRAINTS:
if schema["type"] not in translators.SCHEMA_TRANSLATORS:
unsupported_vars.append(schema["name"])
return unsupported_vars

Expand Down Expand Up @@ -72,19 +66,24 @@ def parse_constraints(
return result


def parse_selection(selection: dict[str, list[Any] | str]) -> dict[str, set[Any]]:
def parse_selection(
selection: dict[str, list[Any] | str], unsupported_vars: list[str] = []
) -> dict[str, set[Any]]:
"""
Parse current selection and convert dict[str, list[Any]] into dict[str, set[Any]].

:param selection: a dictionary containing the current selection
:type: dict[str, list[Any]]
:param unsupported_vars: list of variables not supported
:type: list[str]

:rtype: dict[str, set[Any]]:
:return: a dict[str, set[Any]] containing the current selection.
"""
result = {}
for field_name, field_values in selection.items():
result[field_name] = set(ensure_sequence(field_values))
if field_name not in unsupported_vars:
result[field_name] = set(ensure_sequence(field_values))
return result


Expand Down Expand Up @@ -305,7 +304,13 @@ def parse_form(raw_form: list[Any] | dict[str, Any] | None) -> dict[str, set[Any
for field_name in ogc_form:
try:
if ogc_form[field_name]["schema_"]["type"] == "array":
form[field_name] = set(ogc_form[field_name]["schema_"]["items"]["enum"])
if ogc_form[field_name]["schema_"]["items"].get("enum"):
form[field_name] = set(
ogc_form[field_name]["schema_"]["items"]["enum"]
)
else:
# FIXME: temporarely fix for making constraints working from UI
form[field_name] = []
else:
form[field_name] = set(ogc_form[field_name]["schema_"]["enum"])
except KeyError:
Expand All @@ -322,7 +327,7 @@ def validate_constraints(
unsupported_vars = get_unsupported_vars(ogc_form)
constraints = parse_constraints(constraints)
constraints = remove_unsupported_vars(constraints, unsupported_vars)
selection = parse_selection(request["inputs"])
selection = parse_selection(request["inputs"], unsupported_vars)

return apply_constraints(parsed_form, selection, constraints)

Expand Down
2 changes: 1 addition & 1 deletion cads_adaptors/translators.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def translate_geographic_extent_map(input_cds_schema: dict[str, Any]) -> dict[st
"StringListWidget": translate_string_list,
"StringListArrayWidget": translate_string_list_array,
"StringChoiceWidget": translate_string_choice,
"GeographicExtentMapWidget": translate_geographic_extent_map,
"GeographicExtentWidget": translate_geographic_extent_map,
}


Expand Down
105 changes: 105 additions & 0 deletions tests/test_10_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,67 @@ def test_parse_constraints() -> None:
assert [{}] == constraints.parse_constraints([{}])


def test_remove_unsupported_vars() -> None:
raw_constraints: list[dict[str, list[Any]]] = [
{"level": ["500"], "param": ["Z", "T"], "step": ["24", "36", "48"]},
{"level": ["1000"], "param": ["Z"], "step": ["24", "48"]},
{"level": ["850"], "param": ["T"], "step": ["36", "48"], "unknown": "foo"},
]

parsed_constraints: list[dict[str, list[Any]]] = [
{"level": ["500"], "param": ["Z", "T"], "step": ["24", "36", "48"]},
{"level": ["1000"], "param": ["Z"], "step": ["24", "48"]},
{"level": ["850"], "param": ["T"], "step": ["36", "48"]},
]

form: list[dict[str, Any]] = [
{
"details": {
"groups": [{"values": ["Z"]}, {"values": ["T"]}],
"default": "Z",
},
"name": "param",
"label": "Variable",
"type": "StringListArrayWidget",
},
{
"details": {"values": ["500", "850", "1000"], "default": "500"},
"name": "level",
"label": "Pressure Level",
"type": "StringListWidget",
},
{
"details": {"values": ["24", "36", "48"], "default": "24"},
"name": "step",
"label": "Step",
"type": "StringListWidget",
},
{
"details": {"values": ["1", "2", "3"], "default": "1"},
"name": "number",
"label": "Number",
"type": "StringChoiceWidget",
},
{
"details": {"values": ["1", "2", "3"], "default": "1"},
"name": "area",
"label": "Area",
"type": "GeographicExtentWidget",
},
{
"name": "unknown",
"label": "Don't know hot to handle this",
"type": "UnknownWidget",
},
]
unsupported_vars = constraints.get_unsupported_vars(form)

assert unsupported_vars == ["unknown"]
assert parsed_constraints == constraints.remove_unsupported_vars(
raw_constraints, unsupported_vars
)


def test_parse_form() -> None:
form: list[dict[str, Any]] = [
{
Expand Down Expand Up @@ -168,3 +229,47 @@ def test_ensure_sequence() -> None:
assert constraints.ensure_sequence([]) == []
assert constraints.ensure_sequence(("1",)) == ("1",)
assert constraints.ensure_sequence("1") == ["1"]


def test_validate_constraints() -> None:
raw_form: list[dict[str, list[Any] | str]] = [
{
"details": {
"groups": [{"values": ["1"]}, {"values": ["2", "3"]}],
"default": "A",
},
"name": "param1",
"label": "Param1",
"type": "StringListArrayWidget",
},
{
"details": {"values": ["1", "2", "3"], "default": "1"},
"name": "param2",
"label": "Param2",
"type": "StringListWidget",
},
{
"details": {"values": ["1", "2", "3"], "default": "1"},
"name": "param3",
"label": "Param3",
"type": "StringListWidget",
},
{
"details": {"values": ["1", "2", "3"], "default": "1"},
"name": "param4",
"label": "Param4",
"type": "UnsupportedWidget",
},
]

selections: dict[str, Any] = {
"inputs": {"param1": "1", "param2": "1", "param4": "1"}
}

raw_constraints: list[dict[str, list[Any]]] = [
{"param1": ["1"], "param2": ["1", "2", "3"], "param3": ["1", "2", "3"]},
{"param1": ["2"], "param2": ["2"], "param3": ["2"]},
{"param1": ["3"], "param2": ["3"], "param3": ["3"]},
]

constraints.validate_constraints(raw_form, selections, raw_constraints)