diff options
author | Alexander Shorin <kxepal@gmail.com> | 2011-12-25 19:41:46 +0400 |
---|---|---|
committer | Alexander Shorin <kxepal@gmail.com> | 2011-12-25 19:41:46 +0400 |
commit | 477a1996a55834f8513fb7494714f11cf88af4dd (patch) | |
tree | 0955bf95d5caa659d27e7f0fdef320ee8e240951 /tests.py | |
parent | 6dd658934ac7c6d1eba7b726d73552c13e2ebc1d (diff) | |
download | python-json-patch-477a1996a55834f8513fb7494714f11cf88af4dd.tar.gz |
Rework tests.
Diffstat (limited to 'tests.py')
-rwxr-xr-x | tests.py | 87 |
1 files changed, 58 insertions, 29 deletions
@@ -1,44 +1,73 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -from __future__ import print_function import doctest import unittest -import sys +import jsonpatch -modules = ['jsonpatch'] -coverage_modules = [] -suite = unittest.TestSuite() +class ApplyPatchTestCase(unittest.TestCase): -for module in modules: - m = __import__(module, fromlist=[module]) - coverage_modules.append(m) - suite.addTest(doctest.DocTestSuite(m)) + def test_add_object_key(self): + obj = {'foo': 'bar'} + jsonpatch.apply_patch(obj, [{'add': '/baz', 'value': 'qux'}]) + self.assertTrue('baz' in obj) + self.assertEqual(obj['baz'], 'qux') -runner = unittest.TextTestRunner(verbosity=2) + def test_add_array_item(self): + obj = {'foo': ['bar', 'baz']} + jsonpatch.apply_patch(obj, [{'add': '/foo/1', 'value': 'qux'}]) + self.assertEqual(obj['foo'], ['bar', 'qux', 'baz']) -try: - import coverage -except ImportError: - coverage = None + def test_remove_object_key(self): + obj = {'foo': 'bar', 'baz': 'qux'} + jsonpatch.apply_patch(obj, [{'remove': '/baz'}]) + self.assertTrue('baz' not in obj) -if coverage is not None: - coverage.erase() - coverage.start() + def test_remove_array_item(self): + obj = {'foo': ['bar', 'qux', 'baz']} + jsonpatch.apply_patch(obj, [{'remove': '/foo/1'}]) + self.assertEqual(obj['foo'], ['bar', 'baz']) -result = runner.run(suite) + def test_replace_object_key(self): + obj = {'foo': 'bar', 'baz': 'qux'} + jsonpatch.apply_patch(obj, [{'replace': '/baz', 'value': 'boo'}]) + self.assertTrue(obj['baz'], 'boo') -if not result.wasSuccessful(): - sys.exit(1) + def test_replace_array_item(self): + obj = {'foo': ['bar', 'qux', 'baz']} + jsonpatch.apply_patch(obj, [{'replace': '/foo/1', 'value': 'boo'}]) + self.assertEqual(obj['foo'], ['bar', 'boo', 'baz']) -if coverage is not None: - coverage.stop() - coverage.report(coverage_modules) - coverage.erase() + def test_move_object_key(self): + obj = {'foo': {'bar': 'baz', 'waldo': 'fred'}, + 'qux': {'corge': 'grault'}} + jsonpatch.apply_patch(obj, [{'move': '/foo/waldo', 'to': '/qux/thud'}]) + self.assertEqual(obj, {'qux': {'thud': 'fred', 'corge': 'grault'}, + 'foo': {'bar': 'baz'}}) -if coverage is None: - print(""" - No coverage reporting done (Python module "coverage" is missing) - Please install the python-coverage package to get coverage reporting. - """, file=sys.stderr) + def test_move_array_item(self): + obj = {'foo': ['all', 'grass', 'cows', 'eat']} + jsonpatch.apply_patch(obj, [{'move': '/foo/1', 'to': '/foo/3'}]) + self.assertEqual(obj, {'foo': ['all', 'cows', 'eat', 'grass']}) + + def test_test_success(self): + obj = {'baz': 'qux', 'foo': ['a', 2, 'c']} + jsonpatch.apply_patch(obj, [{'test': '/baz', 'value': 'qux'}, + {'test': '/foo/1', 'value': 2}]) + + def test_test_error(self): + obj = {'bar': 'qux'} + self.assertRaises(AssertionError, + jsonpatch.apply_patch, + obj, [{'test': '/bar', 'value': 'bar'}]) + + +def suite(): + suite = unittest.TestSuite() + suite.addTest(doctest.DocTestSuite(jsonpatch)) + suite.addTest(unittest.makeSuite(ApplyPatchTestCase)) + return suite + +if __name__ == '__main__': + unittest.main(defaultTest='suite') |