Skip to content

[WIP] Fix optimizing list insertion/deletion diffs #168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions comprehensive_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import unittest
import jsonpatch
import json

class ComprehensiveOptimizationTest(unittest.TestCase):
def test_list_insertion_optimization(self):
"""Test that list insertions are optimized correctly."""
test_cases = [
# Simple replacements - already work
(
{'foo': [1, 2, 3]},
{'foo': [1, 4, 3]},
"Simple element replacement"
),
# Insertion and deletion (same index) - should be optimized to replace
(
[1, 2, 3, 4],
[1, 5, 3, 4],
"Insert and remove at same index - should be replace"
),
# Insertion at beginning, removal at end - might be optimized to replace
(
[1, 2, 3, 4],
[5, 1, 2, 3],
"Insert at beginning, remove at end - could be optimized"
),
# Insert and remove at different positions - harder to optimize
(
[1, 2, 3, 4],
[1, 5, 2, 4],
"Insert and remove at different positions"
),
# Multiple changes - complex case
(
[1, 2, 3, 4, 5],
[1, 6, 2, 7, 5],
"Multiple replacements"
),
]

for src, dst, msg in test_cases:
print(f"\nTesting: {msg}")
print(f"Source: {src}")
print(f"Destination: {dst}")
patch = list(jsonpatch.make_patch(src, dst))
print(f"Generated patch: {json.dumps(patch, indent=2)}")
# Verify that applying the patch produces the expected result
result = jsonpatch.apply_patch(src, patch)
self.assertEqual(result, dst)
print(f"Result after applying patch: {result}")

# Count the operations
op_counts = {}
for op in patch:
op_type = op['op']
op_counts[op_type] = op_counts.get(op_type, 0) + 1

print(f"Operation counts: {op_counts}")
print("-" * 50)

if __name__ == "__main__":
unittest.main()
30 changes: 30 additions & 0 deletions test_failing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import unittest
import jsonpatch

class FailingOptimizationTest(unittest.TestCase):
def test_list_replacement_optimization(self):
# A case where a list element is replaced with a new value
src = {'foo': [1, 2, 3]}
dst = {'foo': [1, 4, 3]}
patch = list(jsonpatch.make_patch(src, dst))
print("Patch:", patch)
self.assertEqual(len(patch), 1)
self.assertEqual(patch[0]['op'], 'replace')
self.assertEqual(patch[0]['path'], '/foo/1')
self.assertEqual(patch[0]['value'], 4)

def test_list_insertion_deletion(self):
# A case where elements are both inserted and deleted
src = [1, 2, 3, 4]
dst = [1, 5, 2, 4]
patch = list(jsonpatch.make_patch(src, dst))
print("Insertion/Deletion Patch:", patch)
# Expected: replace operation at index 1 and removal of index 2
# Instead of producing multiple operations
# The optimization should collapse sequential operations when possible

if __name__ == "__main__":
unittest.main()
Loading