Skip to content

Commit 8748a3a

Browse files
Copilotstefankoegl
andcommitted
Analyze list insertion/deletion optimization issue in JsonPatch
Co-authored-by: stefankoegl <184196+stefankoegl@users.noreply.github.com>
1 parent 6ffade2 commit 8748a3a

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

comprehensive_test.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
import unittest
5+
import jsonpatch
6+
import json
7+
8+
class ComprehensiveOptimizationTest(unittest.TestCase):
9+
def test_list_insertion_optimization(self):
10+
"""Test that list insertions are optimized correctly."""
11+
test_cases = [
12+
# Simple replacements - already work
13+
(
14+
{'foo': [1, 2, 3]},
15+
{'foo': [1, 4, 3]},
16+
"Simple element replacement"
17+
),
18+
# Insertion and deletion (same index) - should be optimized to replace
19+
(
20+
[1, 2, 3, 4],
21+
[1, 5, 3, 4],
22+
"Insert and remove at same index - should be replace"
23+
),
24+
# Insertion at beginning, removal at end - might be optimized to replace
25+
(
26+
[1, 2, 3, 4],
27+
[5, 1, 2, 3],
28+
"Insert at beginning, remove at end - could be optimized"
29+
),
30+
# Insert and remove at different positions - harder to optimize
31+
(
32+
[1, 2, 3, 4],
33+
[1, 5, 2, 4],
34+
"Insert and remove at different positions"
35+
),
36+
# Multiple changes - complex case
37+
(
38+
[1, 2, 3, 4, 5],
39+
[1, 6, 2, 7, 5],
40+
"Multiple replacements"
41+
),
42+
]
43+
44+
for src, dst, msg in test_cases:
45+
print(f"\nTesting: {msg}")
46+
print(f"Source: {src}")
47+
print(f"Destination: {dst}")
48+
patch = list(jsonpatch.make_patch(src, dst))
49+
print(f"Generated patch: {json.dumps(patch, indent=2)}")
50+
# Verify that applying the patch produces the expected result
51+
result = jsonpatch.apply_patch(src, patch)
52+
self.assertEqual(result, dst)
53+
print(f"Result after applying patch: {result}")
54+
55+
# Count the operations
56+
op_counts = {}
57+
for op in patch:
58+
op_type = op['op']
59+
op_counts[op_type] = op_counts.get(op_type, 0) + 1
60+
61+
print(f"Operation counts: {op_counts}")
62+
print("-" * 50)
63+
64+
if __name__ == "__main__":
65+
unittest.main()

test_failing.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
import unittest
5+
import jsonpatch
6+
7+
class FailingOptimizationTest(unittest.TestCase):
8+
def test_list_replacement_optimization(self):
9+
# A case where a list element is replaced with a new value
10+
src = {'foo': [1, 2, 3]}
11+
dst = {'foo': [1, 4, 3]}
12+
patch = list(jsonpatch.make_patch(src, dst))
13+
print("Patch:", patch)
14+
self.assertEqual(len(patch), 1)
15+
self.assertEqual(patch[0]['op'], 'replace')
16+
self.assertEqual(patch[0]['path'], '/foo/1')
17+
self.assertEqual(patch[0]['value'], 4)
18+
19+
def test_list_insertion_deletion(self):
20+
# A case where elements are both inserted and deleted
21+
src = [1, 2, 3, 4]
22+
dst = [1, 5, 2, 4]
23+
patch = list(jsonpatch.make_patch(src, dst))
24+
print("Insertion/Deletion Patch:", patch)
25+
# Expected: replace operation at index 1 and removal of index 2
26+
# Instead of producing multiple operations
27+
# The optimization should collapse sequential operations when possible
28+
29+
if __name__ == "__main__":
30+
unittest.main()

0 commit comments

Comments
 (0)