- 
                Notifications
    You must be signed in to change notification settings 
- Fork 97
          Add option to generate test operations in diff
          #154
        
          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
base: master
Are you sure you want to change the base?
Changes from all commits
2cb66b9
              597b76e
              13e9ab0
              35734fb
              e1de7e4
              821ed0a
              ee5040f
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|  | @@ -157,7 +157,7 @@ def apply_patch(doc, patch, in_place=False, pointer_cls=JsonPointer): | |||||
| return patch.apply(doc, in_place) | ||||||
|  | ||||||
|  | ||||||
| def make_patch(src, dst, pointer_cls=JsonPointer): | ||||||
| def make_patch(src, dst, generate_test_ops=False, pointer_cls=JsonPointer): | ||||||
| """Generates patch by comparing two document objects. Actually is | ||||||
| a proxy to :meth:`JsonPatch.from_diff` method. | ||||||
|  | ||||||
|  | @@ -170,6 +170,12 @@ def make_patch(src, dst, pointer_cls=JsonPointer): | |||||
| :param pointer_cls: JSON pointer class to use. | ||||||
| :type pointer_cls: Type[JsonPointer] | ||||||
|  | ||||||
| :param generate_test_ops: While :const:`True` generate test operations | ||||||
| capturing previous values of `replace`/`remove` | ||||||
| operations. By default do not generate these | ||||||
| operations. | ||||||
| :type generate_test_ops: bool | ||||||
|  | ||||||
| >>> src = {'foo': 'bar', 'numbers': [1, 3, 4, 8]} | ||||||
| >>> dst = {'baz': 'qux', 'numbers': [1, 4, 7]} | ||||||
| >>> patch = make_patch(src, dst) | ||||||
|  | @@ -178,7 +184,7 @@ def make_patch(src, dst, pointer_cls=JsonPointer): | |||||
| True | ||||||
| """ | ||||||
|  | ||||||
| return JsonPatch.from_diff(src, dst, pointer_cls=pointer_cls) | ||||||
| return JsonPatch.from_diff(src, dst, generate_test_ops=generate_test_ops, pointer_cls=pointer_cls) | ||||||
|  | ||||||
|  | ||||||
| class PatchOperation(object): | ||||||
|  | @@ -475,6 +481,11 @@ def apply(self, obj): | |||||
|  | ||||||
| return obj | ||||||
|  | ||||||
| def _on_undo_add(self, path, key): | ||||||
| 
     | ||||||
| return key | ||||||
|  | ||||||
| def _on_undo_remove(self, path, key): | ||||||
| return key | ||||||
|  | ||||||
| class CopyOperation(PatchOperation): | ||||||
| """ Copies an object property or an array element to a new location """ | ||||||
|  | @@ -628,7 +639,7 @@ def from_string(cls, patch_str, loads=None, pointer_cls=JsonPointer): | |||||
|  | ||||||
| @classmethod | ||||||
| def from_diff( | ||||||
| cls, src, dst, optimization=True, dumps=None, | ||||||
| cls, src, dst, generate_test_ops=False, dumps=None, | ||||||
| pointer_cls=JsonPointer, | ||||||
| ): | ||||||
| """Creates JsonPatch instance based on comparison of two document | ||||||
|  | @@ -641,6 +652,12 @@ def from_diff( | |||||
| :param dst: Data source document object. | ||||||
| :type dst: dict | ||||||
|  | ||||||
| :param generate_test_ops: While :const:`True` generate test operations | ||||||
| 
     | ||||||
| :param generate_test_ops: While :const:`True` generate test operations | |
| :param generate_test_ops: If :const:`True`, generate test operations | 
    
      
    
      Copilot
AI
    
    
    
      Aug 8, 2025 
    
  
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The code accesses prev_op_index[2] directly using a magic index. Consider using a named constant or a more descriptive way to access the operation object to improve code readability.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The docstring uses 'While :const:
True' which is grammatically incorrect. It should be 'When :const:True' or 'If :const:True'.