fix dict subtracting with dict and set
This commit is contained in:
@ -141,19 +141,27 @@ def subtract_dict(current_dict: Any, updating_dict: Any) -> Dict: # noqa: CCR00
|
|||||||
:return: combination of both dicts
|
:return: combination of both dicts
|
||||||
:rtype: Dict
|
:rtype: Dict
|
||||||
"""
|
"""
|
||||||
if current_dict is Ellipsis:
|
|
||||||
return dict()
|
|
||||||
for key, value in updating_dict.items():
|
for key, value in updating_dict.items():
|
||||||
old_key = current_dict.get(key, {})
|
old_key = current_dict.get(key, {})
|
||||||
new_value: Optional[Union[Dict, Set]] = None
|
new_value: Optional[Union[Dict, Set]] = None
|
||||||
if not old_key:
|
if not old_key:
|
||||||
continue
|
continue
|
||||||
if isinstance(value, collections.abc.Mapping):
|
if isinstance(value, set) and isinstance(old_key, set):
|
||||||
if isinstance(old_key, set):
|
|
||||||
old_key = convert_set_to_required_dict(old_key)
|
|
||||||
new_value = subtract_dict(old_key, value)
|
|
||||||
elif isinstance(value, set) and isinstance(old_key, set):
|
|
||||||
new_value = old_key.difference(value)
|
new_value = old_key.difference(value)
|
||||||
|
elif isinstance(value, (set, collections.abc.Mapping)) and isinstance(
|
||||||
|
old_key, (set, collections.abc.Mapping)
|
||||||
|
):
|
||||||
|
value = (
|
||||||
|
convert_set_to_required_dict(value)
|
||||||
|
if not isinstance(value, collections.abc.Mapping)
|
||||||
|
else value
|
||||||
|
)
|
||||||
|
old_key = (
|
||||||
|
convert_set_to_required_dict(old_key)
|
||||||
|
if not isinstance(old_key, collections.abc.Mapping)
|
||||||
|
else old_key
|
||||||
|
)
|
||||||
|
new_value = subtract_dict(old_key, value)
|
||||||
|
|
||||||
if new_value:
|
if new_value:
|
||||||
current_dict[key] = new_value
|
current_dict[key] = new_value
|
||||||
|
|||||||
@ -136,6 +136,44 @@ def test_subtracting_dict_inc_set_with_dict_inc_set():
|
|||||||
assert test == {"cc": {"aa": {"yy"}, "bb": Ellipsis}}
|
assert test == {"cc": {"aa": {"yy"}, "bb": Ellipsis}}
|
||||||
|
|
||||||
|
|
||||||
|
def test_subtracting_with_set_and_dict():
|
||||||
|
curr_dict = {
|
||||||
|
"translation": {
|
||||||
|
"filters": {
|
||||||
|
"values": Ellipsis,
|
||||||
|
"reports": {"report": {"charts": {"chart": Ellipsis}}},
|
||||||
|
},
|
||||||
|
"translations": {"language": Ellipsis},
|
||||||
|
"filtervalues": {
|
||||||
|
"filter": {"reports": {"report": {"charts": {"chart": Ellipsis}}}}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"chart": {
|
||||||
|
"reports": {
|
||||||
|
"report": {
|
||||||
|
"filters": {
|
||||||
|
"filter": {
|
||||||
|
"translation": {
|
||||||
|
"translations": {"language": Ellipsis},
|
||||||
|
"filtervalues": Ellipsis,
|
||||||
|
},
|
||||||
|
"values": {
|
||||||
|
"translation": {"translations": {"language": Ellipsis}}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
dict_to_update = {
|
||||||
|
"chart": Ellipsis,
|
||||||
|
"translation": {"filters", "filtervalues", "chartcolumns"},
|
||||||
|
}
|
||||||
|
test = subtract_dict(curr_dict, dict_to_update)
|
||||||
|
assert test == {"translation": {"translations": {"language": Ellipsis}}}
|
||||||
|
|
||||||
|
|
||||||
database = databases.Database(DATABASE_URL, force_rollback=True)
|
database = databases.Database(DATABASE_URL, force_rollback=True)
|
||||||
metadata = sqlalchemy.MetaData()
|
metadata = sqlalchemy.MetaData()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user