]> git.puffer.fish Git - matthieu/frr.git/commitdiff
tests: Fix JSON diff for nested lists
authorGalaxyGorilla <sascha@netdef.org>
Fri, 22 May 2020 21:18:46 +0000 (21:18 +0000)
committerGalaxyGorilla <sascha@netdef.org>
Fri, 22 May 2020 22:29:35 +0000 (22:29 +0000)
The involved piece of code is supposed to find a 'closest' match for two
JSON structures using another JSON diff. However, it can happen that
during that new diff the JSON structures are altered (elements from a
list are deleted when 'found'). This is in general ok when the deleted
element is part of the JSON structure which 'matches', but when it later
turns out that some other element of the structure doesn't fit, then the
whole structure should be recovered. This is now realized by using a
deepcopy for the besaid new JSON diff such that the original is only
altered (e.g. deleted) when the diff is clean.

Signed-off-by: GalaxyGorilla <sascha@netdef.org>
tests/topotests/lib/test/test_json.py
tests/topotests/lib/topotest.py

index 4b796640188e5834658baf849a0fdeb09370eacc..7a061a9bc696bcd2637fa3caf68a44aa6805bb13 100755 (executable)
@@ -463,5 +463,39 @@ def test_json_object_asterisk_matching():
     assert json_cmp(dcomplete, dsub4) is None
 
 
+def test_json_list_nested_with_objects():
+
+    dcomplete = [
+        {
+            "key": 1,
+            "list": [
+                123
+            ]
+        },
+        {
+            "key": 2,
+            "list": [
+                123
+            ]
+        }
+    ]
+
+    dsub1 = [
+        {
+            "key": 2,
+            "list": [
+                123
+            ]
+        },
+        {
+            "key": 1,
+            "list": [
+                123
+            ]
+        }
+    ]
+
+    assert json_cmp(dcomplete, dsub1) is None
+
 if __name__ == "__main__":
     sys.exit(pytest.main())
index b0236b01d60c2e5f459bf37d1edfb746fa9632e0..cefb61d198132d377cb5504c930a7c4b6aacc973 100644 (file)
@@ -173,7 +173,9 @@ def gen_json_diff_report(d1, d2, exact=False, path="> $", acc=(0, "")):
                 closest_diff = None
                 closest_idx = None
                 for idx1, v1 in zip(range(0, len(d1)), d1):
-                    tmp_diff = gen_json_diff_report(v1, v2, path=add_idx(idx1))
+                    tmp_v1 = deepcopy(v1)
+                    tmp_v2 = deepcopy(v2)
+                    tmp_diff = gen_json_diff_report(tmp_v1, tmp_v2, path=add_idx(idx1))
                     if not has_errors(tmp_diff):
                         found_match = True
                         del d1[idx1]