Skip to content

Commit 597b76e

Browse files
committed
Extend test suite
1 parent 2cb66b9 commit 597b76e

File tree

1 file changed

+42
-24
lines changed

1 file changed

+42
-24
lines changed

tests.py

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -325,32 +325,33 @@ def json_loader(obj):
325325

326326

327327
class MakePatchTestCase(unittest.TestCase):
328+
generate_test_ops = False
328329

329330
def test_apply_patch_to_copy(self):
330331
src = {'foo': 'bar', 'boo': 'qux'}
331332
dst = {'baz': 'qux', 'foo': 'boo'}
332-
patch = jsonpatch.make_patch(src, dst)
333+
patch = jsonpatch.make_patch(src, dst, generate_test_ops=self.generate_test_ops)
333334
res = patch.apply(src)
334335
self.assertTrue(src is not res)
335336

336337
def test_apply_patch_to_same_instance(self):
337338
src = {'foo': 'bar', 'boo': 'qux'}
338339
dst = {'baz': 'qux', 'foo': 'boo'}
339-
patch = jsonpatch.make_patch(src, dst)
340+
patch = jsonpatch.make_patch(src, dst, generate_test_ops=self.generate_test_ops)
340341
res = patch.apply(src, in_place=True)
341342
self.assertTrue(src is res)
342343

343344
def test_objects(self):
344345
src = {'foo': 'bar', 'boo': 'qux'}
345346
dst = {'baz': 'qux', 'foo': 'boo'}
346-
patch = jsonpatch.make_patch(src, dst)
347+
patch = jsonpatch.make_patch(src, dst, generate_test_ops=self.generate_test_ops)
347348
res = patch.apply(src)
348349
self.assertEqual(res, dst)
349350

350351
def test_arrays(self):
351352
src = {'numbers': [1, 2, 3], 'other': [1, 3, 4, 5]}
352353
dst = {'numbers': [1, 3, 4, 5], 'other': [1, 3, 4]}
353-
patch = jsonpatch.make_patch(src, dst)
354+
patch = jsonpatch.make_patch(src, dst, generate_test_ops=self.generate_test_ops)
354355
res = patch.apply(src)
355356
self.assertEqual(res, dst)
356357

@@ -361,15 +362,15 @@ def test_complex_object(self):
361362
dst = {'data': [
362363
{'foo': [42]}, {'bar': []}, {'baz': {'boo': 'oom!'}}
363364
]}
364-
patch = jsonpatch.make_patch(src, dst)
365+
patch = jsonpatch.make_patch(src, dst, generate_test_ops=self.generate_test_ops)
365366
res = patch.apply(src)
366367
self.assertEqual(res, dst)
367368

368369
def test_array_add_remove(self):
369370
# see https://github.com/stefankoegl/python-json-patch/issues/4
370371
src = {'numbers': [], 'other': [1, 5, 3, 4]}
371372
dst = {'numbers': [1, 3, 4, 5], 'other': []}
372-
patch = jsonpatch.make_patch(src, dst)
373+
patch = jsonpatch.make_patch(src, dst, generate_test_ops=self.generate_test_ops)
373374
res = patch.apply(src)
374375
self.assertEqual(res, dst)
375376

@@ -388,7 +389,7 @@ def test_add_nested(self):
388389
def _test_should_just_add_new_item_not_rebuild_all_list(self):
389390
src = {'foo': [1, 2, 3]}
390391
dst = {'foo': [3, 1, 2, 3]}
391-
patch = list(jsonpatch.make_patch(src, dst))
392+
patch = list(jsonpatch.make_patch(src, dst, generate_test_ops=self.generate_test_ops))
392393
self.assertEqual(len(patch), 1)
393394
self.assertEqual(patch[0]['op'], 'add')
394395
res = jsonpatch.apply_patch(src, patch)
@@ -397,24 +398,30 @@ def _test_should_just_add_new_item_not_rebuild_all_list(self):
397398
def test_escape(self):
398399
src = {"x/y": 1}
399400
dst = {"x/y": 2}
400-
patch = jsonpatch.make_patch(src, dst)
401-
self.assertEqual([{"path": "/x~1y", "value": 2, "op": "replace"}], patch.patch)
401+
patch = jsonpatch.make_patch(src, dst, generate_test_ops=self.generate_test_ops)
402+
403+
if self.generate_test_ops:
404+
self.assertEqual([{"path": "/x~1y", "value": 1, "op": "test"},
405+
{"path": "/x~1y", "value": 2, "op": "replace"}], patch.patch)
406+
else:
407+
self.assertEqual([{"path": "/x~1y", "value": 2, "op": "replace"}], patch.patch)
408+
402409
res = patch.apply(src)
403410
self.assertEqual(res, dst)
404411

405412
def test_root_list(self):
406413
""" Test making and applying a patch of the root is a list """
407414
src = [{'foo': 'bar', 'boo': 'qux'}]
408415
dst = [{'baz': 'qux', 'foo': 'boo'}]
409-
patch = jsonpatch.make_patch(src, dst)
416+
patch = jsonpatch.make_patch(src, dst, generate_test_ops=self.generate_test_ops)
410417
res = patch.apply(src)
411418
self.assertEqual(res, dst)
412419

413420
def test_make_patch_unicode(self):
414421
""" Test if unicode keys and values are handled correctly """
415422
src = {}
416423
dst = {'\xee': '\xee'}
417-
patch = jsonpatch.make_patch(src, dst)
424+
patch = jsonpatch.make_patch(src, dst, generate_test_ops=self.generate_test_ops)
418425
res = patch.apply(src)
419426
self.assertEqual(res, dst)
420427

@@ -423,15 +430,21 @@ def test_issue40(self):
423430

424431
src = [8, 7, 2, 1, 0, 9, 4, 3, 5, 6]
425432
dest = [7, 2, 1, 0, 9, 4, 3, 6, 5, 8]
426-
jsonpatch.make_patch(src, dest)
433+
jsonpatch.make_patch(src, dest, generate_test_ops=self.generate_test_ops)
427434

428435
def test_issue76(self):
429436
""" Make sure op:remove does not include a 'value' field """
430437

431438
src = { "name": "fred", "friend": "barney", "spouse": "wilma" }
432439
dst = { "name": "fred", "spouse": "wilma" }
433-
expected = [{"path": "/friend", "op": "remove"}]
434-
patch = jsonpatch.make_patch(src, dst)
440+
441+
if self.generate_test_ops:
442+
expected = [{"path": "/friend", "op": "test", "value": "barney"},
443+
{"path": "/friend", "op": "remove"}]
444+
else:
445+
expected = [{"path": "/friend", "op": "remove"}]
446+
447+
patch = jsonpatch.make_patch(src, dst, generate_test_ops=self.generate_test_ops)
435448
self.assertEqual(patch.patch, expected)
436449
res = jsonpatch.apply_patch(src, patch)
437450
self.assertEqual(res, dst)
@@ -443,7 +456,7 @@ def test_json_patch(self):
443456
new = {
444457
'queue': {'teams_out': [{'id': 5, 'reason': 'If lose'}]}
445458
}
446-
patch = jsonpatch.make_patch(old, new)
459+
patch = jsonpatch.make_patch(old, new, generate_test_ops=self.generate_test_ops)
447460
new_from_patch = jsonpatch.apply_patch(old, patch)
448461
self.assertEqual(new, new_from_patch)
449462

@@ -452,7 +465,7 @@ def test_arrays_one_element_sequences(self):
452465
# see https://github.com/stefankoegl/python-json-patch/issues/30#issuecomment-155070128
453466
src = [1,2,3]
454467
dst = [3,1,4,2]
455-
patch = jsonpatch.make_patch(src, dst)
468+
patch = jsonpatch.make_patch(src, dst, generate_test_ops=self.generate_test_ops)
456469
res = jsonpatch.apply_patch(src, patch)
457470
self.assertEqual(res, dst)
458471

@@ -462,7 +475,7 @@ def test_list_in_dict(self):
462475
https://github.com/stefankoegl/python-json-patch/issues/74 """
463476
old = {'key': [{'someNumber': 0, 'someArray': [1, 2, 3]}]}
464477
new = {'key': [{'someNumber': 0, 'someArray': [1, 2, 3, 4]}]}
465-
patch = jsonpatch.make_patch(old, new)
478+
patch = jsonpatch.make_patch(old, new, generate_test_ops=self.generate_test_ops)
466479
new_from_patch = jsonpatch.apply_patch(old, patch)
467480
self.assertEqual(new, new_from_patch)
468481

@@ -472,23 +485,23 @@ def test_nested(self):
472485
https://github.com/stefankoegl/python-json-patch/issues/41 """
473486
old = {'school':{'names':['Kevin','Carl']}}
474487
new = {'school':{'names':['Carl','Kate','Kevin','Jake']}}
475-
patch = jsonpatch.JsonPatch.from_diff(old, new)
488+
patch = jsonpatch.JsonPatch.from_diff(old, new, generate_test_ops=self.generate_test_ops)
476489
new_from_patch = jsonpatch.apply_patch(old, patch)
477490
self.assertEqual(new, new_from_patch)
478491

479492
def test_move_from_numeric_to_alpha_dict_key(self):
480493
#https://github.com/stefankoegl/python-json-patch/issues/97
481494
src = {'13': 'x'}
482495
dst = {'A': 'a', 'b': 'x'}
483-
patch = jsonpatch.make_patch(src, dst)
496+
patch = jsonpatch.make_patch(src, dst, generate_test_ops=self.generate_test_ops)
484497
res = jsonpatch.apply_patch(src, patch)
485498
self.assertEqual(res, dst)
486499

487500
def test_issue90(self):
488501
"""In JSON 1 is different from True even though in python 1 == True"""
489502
src = {'A': 1}
490503
dst = {'A': True}
491-
patch = jsonpatch.make_patch(src, dst)
504+
patch = jsonpatch.make_patch(src, dst, generate_test_ops=self.generate_test_ops)
492505
res = jsonpatch.apply_patch(src, patch)
493506
self.assertEqual(res, dst)
494507
self.assertIsInstance(res['A'], bool)
@@ -497,7 +510,7 @@ def test_issue129(self):
497510
"""In JSON 1 is different from True even though in python 1 == True Take Two"""
498511
src = {'A': {'D': 1.0}, 'B': {'E': 'a'}}
499512
dst = {'A': {'C': 'a'}, 'B': {'C': True}}
500-
patch = jsonpatch.make_patch(src, dst)
513+
patch = jsonpatch.make_patch(src, dst, generate_test_ops=self.generate_test_ops)
501514
res = jsonpatch.apply_patch(src, patch)
502515
self.assertEqual(res, dst)
503516
self.assertIsInstance(res['B']['C'], bool)
@@ -506,7 +519,7 @@ def test_issue103(self):
506519
"""In JSON 1 is different from 1.0 even though in python 1 == 1.0"""
507520
src = {'A': 1}
508521
dst = {'A': 1.0}
509-
patch = jsonpatch.make_patch(src, dst)
522+
patch = jsonpatch.make_patch(src, dst, generate_test_ops=self.generate_test_ops)
510523
res = jsonpatch.apply_patch(src, patch)
511524
self.assertEqual(res, dst)
512525
self.assertIsInstance(res['A'], float)
@@ -522,7 +535,7 @@ def test_issue119(self):
522535
{'foobar': {u'1': [u'lettuce', u'cabbage', u'bok choy', u'broccoli'], u'3': [u'ibex'], u'2': [u'apple'], u'5': [], u'4': [u'gerenuk', u'duiker'], u'10_1576156603109': [], u'6': [], u'8_1572034252560': [u'thompson', u'gravie', u'mango', u'coconut'], u'7_1572034204585': []}},
523536
{'foobar': {u'description': u'', u'title': u''}}
524537
]
525-
patch = jsonpatch.make_patch(src, dst)
538+
patch = jsonpatch.make_patch(src, dst, generate_test_ops=self.generate_test_ops)
526539
res = jsonpatch.apply_patch(src, patch)
527540
self.assertEqual(res, dst)
528541

@@ -562,7 +575,7 @@ def test_issue120(self):
562575
'16': 'service',
563576
'2': ['zero', 'enable']}}
564577
]
565-
patch = jsonpatch.make_patch(src, dst)
578+
patch = jsonpatch.make_patch(src, dst, generate_test_ops=self.generate_test_ops)
566579
res = jsonpatch.apply_patch(src, patch)
567580
self.assertEqual(res, dst)
568581

@@ -597,6 +610,10 @@ def test_custom_types_subclass_load(self):
597610
self.assertEqual(new, new_from_patch)
598611

599612

613+
class MakePatchWithTestOpsTestCase(MakePatchTestCase):
614+
generate_test_ops = True
615+
616+
600617
class OptimizationTests(unittest.TestCase):
601618
def test_use_replace_instead_of_remove_add(self):
602619
src = {'foo': [1, 2, 3]}
@@ -1051,6 +1068,7 @@ def get_suite():
10511068
suite.addTest(unittest.makeSuite(ApplyPatchTestCase))
10521069
suite.addTest(unittest.makeSuite(EqualityTestCase))
10531070
suite.addTest(unittest.makeSuite(MakePatchTestCase))
1071+
suite.addTest(unittest.makeSuite(MakePatchWithTestOpsTestCase))
10541072
suite.addTest(unittest.makeSuite(ListTests))
10551073
suite.addTest(unittest.makeSuite(InvalidInputTests))
10561074
suite.addTest(unittest.makeSuite(ConflictTests))

0 commit comments

Comments
 (0)