-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathcomprehensive_test.py
More file actions
65 lines (59 loc) · 2.22 KB
/
comprehensive_test.py
File metadata and controls
65 lines (59 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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()