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"\n Testing: { 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 ()
0 commit comments