Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
mcucchi9 committed Jul 23, 2024
1 parent 2906e3f commit 953198d
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 16 deletions.
27 changes: 16 additions & 11 deletions cads_processing_api_service/translators.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,13 @@ def translate_request_ids_into_labels(
input_key_id: str(input_value_id)
for input_key_id, input_value_id in request.items()
}
exclusive_group_widgets_children = []
for cds_input_schema in cds_form:
if cds_input_schema.get("type", None) == "ExclusiveGroupWidget":
exclusive_group_widgets_children.extend(cds_input_schema["children"])
for cds_input_schema in cds_form:
cds_input_schema_name = cds_input_schema.get("name", None)
if cds_input_schema_name in ("global", "area"):
if cds_input_schema_name in exclusive_group_widgets_children:
continue
if cds_input_schema.get("type", None) == "ExclusiveGroupWidget":
input_key_label = cds_input_schema["label"]
Expand All @@ -229,19 +233,20 @@ def translate_request_ids_into_labels(
# TODO: handle default values
input_key_id = cds_input_schema.get("name", None)
input_key_label = cds_input_schema.get("label", None)
logger.info("input_key_id", input_key_id=input_key_id)
if input_key_id in request_labels:
del request_labels[input_key_id]
input_value_ids = request[input_key_id]
if not isinstance(input_value_ids, list):
input_value_ids = [input_value_ids]
request_labels[input_key_label] = make_request_labels(
input_value_ids, cds_input_schema
)
logger.info("if")
elif default := cds_input_schema.get("details", {}).get("default", None):
logger.info("elif", default=default)
request_labels[input_key_label] = [default]
elif default_value_ids := cds_input_schema.get("details", {}).get(
"default", None
):
input_value_ids = default_value_ids
else:
continue
if not isinstance(input_value_ids, list):
input_value_ids = [input_value_ids]
request_labels[input_key_label] = make_request_labels(
input_value_ids, cds_input_schema
)

return request_labels

Expand Down
83 changes: 78 additions & 5 deletions tests/test_10_translators.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@

TEST_INPUT_CDS_SCHEMAS: dict[str, Any] = {
"string_list": {
"name": "string_list",
"label": "String List",
"details": {"labels": {"val1": "Val1", "val2": "Val2", "val3": "Val3"}},
"type": "StringListWidget",
},
"string_list_array": {
"name": "string_list_array",
"label": "String List Array",
"details": {
"groups": [
{"labels": {"val1": "Val1", "val2": "Val2"}},
Expand All @@ -33,21 +37,29 @@
"type": "StringListArrayWidget",
},
"string_choice": {
"name": "string_choice",
"label": "String Choice",
"details": {
"labels": {"val1": "Val1", "val2": "Val2", "val3": "Val3"},
"default": "val1",
},
"type": "StringChoiceWidget",
},
"geographic_extent_map": {
"name": "geographic_extent_map",
"label": "Geographic Extent Map",
"details": {"default": [1, 2, 3, 4]},
"type": "GeographicExtentMapWidget",
},
"geographic_location": {
"name": "geographic_location",
"label": "Geographic Location",
"details": {},
"type": "GeographicLocationWidget",
},
"string_list_array_groups": {
"name": "string_list_array_groups",
"label": "String List Array Groups",
"details": {
"groups": [
{
Expand All @@ -67,9 +79,30 @@
"type": "StringListArrayWidget",
},
"free_edition_widget": {
"name": "free_edition_widget",
"label": "Free Edition Widget",
"type": "FreeEditionWidget",
"details": {},
},
"exclusive_group_widget": {
"name": "exclusive_group_widget",
"label": "Exclusive Group Widget",
"type": "ExclusiveGroupWidget",
"children": ["child_1", "child_2"],
"details": {"default": "child_1"},
},
"child_1": {
"name": "child_1",
"label": "Child 1",
"type": "Child1Widget",
"details": {},
},
"child_2": {
"name": "child_2",
"label": "Child 2",
"type": "Child2Widget",
"details": {},
},
}


Expand Down Expand Up @@ -143,7 +176,6 @@ def test_translate_string_list() -> None:
res_output = cads_processing_api_service.translators.translate_string_list(
test_input
)

assert res_output == exp_ouput


Expand All @@ -156,7 +188,6 @@ def test_translate_string_list_array() -> None:
res_output = cads_processing_api_service.translators.translate_string_list_array(
test_input
)

assert res_output == exp_ouput

test_input = TEST_INPUT_CDS_SCHEMAS["string_list_array_groups"]
Expand All @@ -170,7 +201,6 @@ def test_translate_string_list_array() -> None:
res_output = cads_processing_api_service.translators.translate_string_list_array(
test_input
)

assert res_output == exp_ouput


Expand All @@ -180,7 +210,6 @@ def test_translate_string_choice() -> None:
res_output = cads_processing_api_service.translators.translate_string_choice(
test_input
)

assert res_output == exp_ouput


Expand All @@ -198,7 +227,6 @@ def test_translate_geographic_extent_map() -> None:
test_input
)
)

assert res_output == exp_ouput


Expand Down Expand Up @@ -236,6 +264,51 @@ def test_make_request_labels() -> None:
assert res_output == exp_output


def test_translate_request_ids_into_labels() -> None:
request = {"key1": "val1", "key2": "val2"}
cds_schema = None
exp_output = {"key1": "val1", "key2": "val2"}
res_output = (
cads_processing_api_service.translators.translate_request_ids_into_labels(
request, cds_schema
)
)
assert res_output == exp_output

request = {
"string_list": ["val1", "val2"],
"string_choice": "val1",
"unknown_key": "unknown_value",
}
cds_schema = [
TEST_INPUT_CDS_SCHEMAS["string_list"],
TEST_INPUT_CDS_SCHEMAS["string_choice"],
]
exp_output = {
"String List": ["Val1", "Val2"],
"String Choice": ["Val1"],
"unknown_key": "unknown_value",
}
res_output = (
cads_processing_api_service.translators.translate_request_ids_into_labels(
request, cds_schema
)
)
assert res_output == exp_output

request = {}
cds_schema = [
TEST_INPUT_CDS_SCHEMAS["string_choice"],
TEST_INPUT_CDS_SCHEMAS["exclusive_group_widget"],
TEST_INPUT_CDS_SCHEMAS["child_1"],
TEST_INPUT_CDS_SCHEMAS["child_2"],
]
exp_output = {
"String Choice": ["Val1"],
"Exclusive Group Widget": ["Child 1"],
}


def test_format_request_value() -> None:
test_value_1 = "test_value"
exp_output_1 = "'test_value'"
Expand Down

0 comments on commit 953198d

Please sign in to comment.