diff --git a/cads_adaptors/constraints.py b/cads_adaptors/constraints.py index 613ef061..cef60f94 100644 --- a/cads_adaptors/constraints.py +++ b/cads_adaptors/constraints.py @@ -4,12 +4,6 @@ from . import translators -SUPPORTED_CONSTRAINTS = [ - "StringListWidget", - "StringListArrayWidget", - "StringChoiceWidget", -] - class ParameterError(TypeError): pass @@ -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 @@ -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 @@ -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: @@ -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) diff --git a/cads_adaptors/translators.py b/cads_adaptors/translators.py index 223a033b..bed75922 100644 --- a/cads_adaptors/translators.py +++ b/cads_adaptors/translators.py @@ -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, } diff --git a/tests/test_10_constraints.py b/tests/test_10_constraints.py index 2e2d3ae6..f85ce8d3 100644 --- a/tests/test_10_constraints.py +++ b/tests/test_10_constraints.py @@ -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]] = [ { @@ -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)