diff --git a/pynxtools/dataconverter/helpers.py b/pynxtools/dataconverter/helpers.py index f4ac62a32..f8cedb6fc 100644 --- a/pynxtools/dataconverter/helpers.py +++ b/pynxtools/dataconverter/helpers.py @@ -858,7 +858,7 @@ def set_default_group(template): # defalut attribute key to the list of immediate child group dflt_key_to_grp_li: Optional[dict[str, list]] = {} # defalut attribute key to the group set by reader - dflt_key_to_exist_grp: dict[str, str] = {} + dflt_key_to_exist_grp: Optional[dict[str, str]] = {} # "/abc[DATA]/XYe[anything]/mnf[MNYZ]/anything" -> ['DATA', 'anything', 'MNYZ'] pattern = r"\[(.*?)\]" @@ -870,7 +870,7 @@ def set_default_group(template): # Cancel out the attribuutes if groups_list[-1].startswith("@"): continue - # Cancel out the fields + # Cancel out the fields and end groups without fields groups_list = groups_list[0:-1] if not groups_list: continue @@ -893,6 +893,7 @@ def set_default_group(template): dflt_key_to_grp_li[last_default_atttr] = {} # Data groups dflt_key_to_grp_li[last_default_atttr]["data"] = [] + # Entry groups dflt_key_to_grp_li[last_default_atttr]["entry"] = [] # Other groups dflt_key_to_grp_li[last_default_atttr]["other"] = [] @@ -915,8 +916,8 @@ def set_default_group(template): for deflt_key, value in dflt_key_to_grp_li.items(): pre_defalt_grp = dflt_key_to_exist_grp.get(deflt_key, None) - # Verify if user added the group here - if not pre_defalt_grp: + # Verify if user has added the group in default attribute + if pre_defalt_grp: if ( pre_defalt_grp in value["entry"] or pre_defalt_grp in value["data"] @@ -929,6 +930,11 @@ def set_default_group(template): if entry_default == deflt_key: template[entry_default] = entry_data_rnd continue + # Handle root level default + root_deflt = "/@default" + if deflt_key == root_deflt: + template[root_deflt] = entry_data_rnd + continue if value["entry"]: template[deflt_key] = value["entry"][0] diff --git a/tests/dataconverter/test_helpers.py b/tests/dataconverter/test_helpers.py index 70e1b0226..88e86d781 100644 --- a/tests/dataconverter/test_helpers.py +++ b/tests/dataconverter/test_helpers.py @@ -463,30 +463,48 @@ def test_path_in_data_dict(nxdl_path, expected, template): assert helpers.path_in_data_dict(nxdl_path, tuple(template.keys())) == expected -def test_set_default_group(template): +@pytest.mark.parametrize( + "temp_dict", + [ + { + "/ENTRY[entry1]/NXODD_name[nxodd_name]/float_value": 2.0, + "/ENTRY[entry1]/NXODD_name[nxodd_name_2]/float_value": 8.0, + "/ENTRY[entry1]/NXODD_name[nxodd_name_2]/DATA[data1]/data1": [3, 5, 6], + "/ENTRY[entry1]/NXODD_name[nxodd_name_2]/DATA[data2]/data2": [3, 5, 6], + } + ], +) +def test_set_default_group(temp_dict): """_summary_ Parameters ---------- template : Template """ + template = Template(temp_dict) assert ( "/@default" not in template ), "To test the root level /@default should be empty." assert ( - "/ENTRY[entry]/@default" not in template + "/ENTRY[entry1]/@default" not in template ), "To test default attribute, entry attribute should be empty." set_default_group(template) - assert ( - template["/@default"] == "entry", - "To test the root level /@default should be empty.", - ) - assert ( - template["/ENTRY[entry]/@default"] == "nxodd_name", - "To test default attribute, entry attribute should be empty.", - ) + assert template["/@default"] in [ + "data1", + "data2", + ], "To test the root level /@default should be empty." + + assert template["/ENTRY[entry1]/@default"] in [ + "nxodd_name", + "nxodd_name_2", + ], "To test default attribute, entry attribute should be empty." + + assert template["/ENTRY[entry1]/NXODD_name[nxodd_name_2]/@default"] in [ + "data1", + "data2", + ], "To test default attribute, entry attribute should be empty." def test_atom_type_extractor_and_hill_conversion():