diff --git a/tests/test_drop_key.py b/tests/test_drop_key.py new file mode 100644 index 0000000..9d79a89 --- /dev/null +++ b/tests/test_drop_key.py @@ -0,0 +1,73 @@ +import uparma +import pytest + + +test_data_list = [ + { + "input": { + ("general", "parameters"): [ + { + "name": "to_key_or_not_to_key", + "key_translations": { + "ursgal_style_1": "to_key_or_not_to_key", + "some_other_style": "to_key_or_not_to_key_", + }, + "value_translations": { + "ursgal_style_1": [[True, True], [False, False]], + "some_other_style": [[True, "i_lived"], [False, "i_died"]], + }, + }, + { + "name": "to_key_or_not_to_key_list", + "key_translations": { + "ursgal_style_1": "to_key_or_not_to_key_list", + "some_other_style": [ + "keyp_no_pun_intended", + "to_key_or_not_to_key_", + ], + }, + "value_translations": { + "ursgal_style_1": [[True, True], [False, False]], + "some_other_style": [[True, "i_lived"], [False, "i_died"]], + }, + }, + { + "name": "to_key_or_not_to_key_rip_delim", + "key_translations": { + "ursgal_style_1": "to_key_or_not_to_key_rip_delim", + "some_other_style": "to_key_or_not_to_key_DROP_KEY", + }, + "value_translations": { + "ursgal_style_1": [[True, True], [False, False]], + "some_other_style": [[True, "i_lived"], [False, "i_died"]], + }, + }, + ] + }, + } +] + + +@pytest.mark.parametrize("test_dict", test_data_list) +def test_load_data(test_dict): + up = uparma.UParma(refresh_jsons=False, parameter_data=test_dict["input"]) + translation = up.convert( + { + "to_key_or_not_to_key": True, + "to_key_or_not_to_key_list": False, + "to_key_or_not_to_key_rip_delim": False, + }, + "some_other_style", + ) + assert translation["to_key_or_not_to_key"]["translated_key"] is None + assert translation["to_key_or_not_to_key"]["translated_value"] == "i_lived" + assert translation["to_key_or_not_to_key_list"]["translated_key"] == [ + "keyp_no_pun_intended", + None, + ] + assert translation["to_key_or_not_to_key_list"]["translated_value"] == "i_died" + assert ( + translation["to_key_or_not_to_key_rip_delim"]["translated_key"] + == "to_key_or_not_to_key_DROP_KEY" + ) + assert translation["to_key_or_not_to_key_rip_delim"]["translated_value"] == "i_died" diff --git a/uparma/uparma.py b/uparma/uparma.py index 927ad7d..e4cbb2d 100755 --- a/uparma/uparma.py +++ b/uparma/uparma.py @@ -141,12 +141,10 @@ def _parse_jsons(self): # _id = uparma_entry["_id"] self.parameters[_id] = uparma_entry for key, value in uparma_entry["key_translations"].items(): - if isinstance(value, list): value = tuple(value) if key in self.parameter2id.keys(): - # found key does value also exist if value in self.parameter2id[key]: # parameter already found @@ -321,8 +319,14 @@ def translate(self, param_dict, original_style=None, translated_style=None): if _uparma_v == _uparma_vt: translated_value = _transtyle_v was_translated = True - if translated_key.endswith(""): - translated_key = None + if isinstance(translated_key, tuple) is True: + translated_key = [ + None if key.endswith("") else key + for key in translated_key + ] + else: + if translated_key.endswith(""): + translated_key = None template_dict.update( { "translated_key": translated_key, @@ -402,6 +406,14 @@ def get_default_params(self, style=None): translated_default = value["default_value"] else: translated_default = value["default_value"] + if isinstance(translated_key, list) is True: + translated_key = [ + None if key.endswith("") else key + for key in translated_key + ] + else: + if translated_key.endswith(""): + translated_key = None params[name] = { "original_key": value["name"], "original_value": value["default_value"],