diff --git a/nested_lookup/lookup_api.py b/nested_lookup/lookup_api.py index 2fd561b..a089bac 100644 --- a/nested_lookup/lookup_api.py +++ b/nested_lookup/lookup_api.py @@ -95,17 +95,11 @@ def _nested_update(document, key, value, val_len, run=0): document=list_items, key=key, value=value, val_len=val_len, run=run ) elif isinstance(document, dict): - if document.get(key): - # check if a value with the coresponding index exists and - # use it otherwise recycle the intially given value - if run < val_len: - val = value[run] - else: - run = 0 - val = value[run] - document[key] = val - run = run + 1 for dict_key, dict_value in iteritems(document): + if dict_key == key: + document[key] = value[0] + if len(value) > 1: + value.pop(0) _nested_update( document=dict_value, key=key, value=value, val_len=val_len, run=run ) diff --git a/test_lookup_api.py b/test_lookup_api.py index 09066e9..6a5cc87 100644 --- a/test_lookup_api.py +++ b/test_lookup_api.py @@ -311,7 +311,7 @@ class TestNestedUpdate(BaseLookUpApi): # if you need to adress a list of dicts, you have to # manually iterate over those and pass them to nested_update # one by one - self.assertNotEqual(updated_document[1]["salsa"][0]["burrito"]["taco"], 200) + self.assertEqual(updated_document[1]["salsa"][0]["burrito"]["taco"], 200) def test_nested_update_raise_error(self): doc = self.sample_data4 @@ -552,6 +552,18 @@ class TestNestedAlter(BaseLookUpApi): self.assertEqual(out[0]["taco"], 52) self.assertEqual(out[1]["salsa"][0]["burrito"]["taco"], 79) + def test_nested_alter_work_with_right_order(self): + document = {"taco": 42, "salsa": [{"burrito":{"key":20}}], "key":50} + + def callback(data): + return data + 100 + + altered_document = nested_alter(document, "key", callback, in_place=True) + + self.assertEqual(altered_document["salsa"][0]["burrito"]["key"], 120) + self.assertEqual(altered_document["key"], 150) + + def test_sample_data4(self): result = {