diff options
author | Matěj Cepl <mcepl@redhat.com> | 2011-11-19 09:24:17 +0100 |
---|---|---|
committer | Matěj Cepl <mcepl@redhat.com> | 2011-11-19 19:52:42 +0100 |
commit | 536c112a1c7b808729cb6e7f1103e1a541f10ce7 (patch) | |
tree | 76240c2a1cf4aea9de807f6717e6824a90fd036b /test_json_diff.py | |
parent | 5cc8f8468fcba6e0cc6855d064c45be20968fbda (diff) | |
download | json_diff-536c112a1c7b808729cb6e7f1103e1a541f10ce7.tar.gz |
Add tons of elementary tests.
Tests now should cover all basics of JSON format.
Also added a method _run_test_strings for simple tests.
Created a parent class OurTestCase containing the above helper methods.
Moved static strings to special module.
Diffstat (limited to 'test_json_diff.py')
-rw-r--r-- | test_json_diff.py | 278 |
1 files changed, 73 insertions, 205 deletions
diff --git a/test_json_diff.py b/test_json_diff.py index 453eba4..2d073ea 100644 --- a/test_json_diff.py +++ b/test_json_diff.py @@ -3,195 +3,16 @@ PyUnit unit tests """ from __future__ import division, absolute_import, unicode_literals -import unittest +import unittest, sys +if sys.version_info[0] == 3: unicode = str import json import json_diff from io import StringIO import codecs -SIMPLE_OLD = """ -{ - "a": 1, - "b": true, - "c": "Janošek" -} -""" - -SIMPLE_NEW = """ -{ - "b": false, - "c": "Maruška", - "d": "přidáno" -} -""" - -SIMPLE_DIFF = """ -{ - "_append": { - "d": "přidáno" - }, - "_remove": { - "a": 1 - }, - "_update": { - "c": "Maruška", - "b": false - } -} -""" - -SIMPLE_DIFF_HTML=""" -<!DOCTYPE html> -<html lang='en'> -<meta charset="utf-8" /> -<title>json_diff result</title> -<style> -td { -text-align: center; -} -.append_class { -color: green; -} -.remove_class { -color: red; -} -.update_class { -color: navy; -} -</style> -<body> -<h1>json_diff result</h1> -<table> -<tr> -<td class='remove_class'>a = 1</td> -</tr><tr> -<td class='update_class'>c = Maruška</td> -</tr><tr> -<td class='update_class'>b = False</td> -</tr><tr> -<td class='append_class'>d = přidáno</td> -</tr> -</table> -</body> -</html> -""" - -SIMPLE_ARRAY_OLD = """ -{ - "a": [ 1 ] -} -""" - -SIMPLE_ARRAY_NEW = """ -{ - "a": [ 1, 2 ] -} -""" - -SIMPLE_ARRAY_DIFF = """ -{ - "_update": { - "a": { - "_append": { - "1": 2 - } - } - } -} -""" +from test_strings import * #@UnusedWildImport -NESTED_OLD = """ -{ - "a": 1, - "b": 2, - "son": { - "name": "Janošek" - } -} -""" - -NESTED_NEW = """ -{ - "a": 2, - "c": 3, - "daughter": { - "name": "Maruška" - } -} -""" - -NESTED_DIFF = """ -{ - "_append": { - "c": 3, - "daughter": { - "name": "Maruška" - } - }, - "_remove": { - "b": 2, - "son": { - "name": "Janošek" - } - }, - "_update": { - "a": 2 - } -} -""" -NESTED_DIFF_EXCL = """ -{ - "_append": { - "c": 3 - }, - "_remove": { - "b": 2 - }, - "_update": { - "a": 2 - } -} -""" - -ARRAY_OLD = """ -{ - "a": 1, - "b": 2, - "children": [ - "Pepíček", "Anička", "Maruška" - ] -} -""" - -ARRAY_NEW = """ -{ - "a": 1, - "children": [ - "Pepíček", "Tonička", "Maruška" - ], - "c": 3 -} -""" - -ARRAY_DIFF = """ -{ - "_remove": { - "b": 2 - }, - "_append": { - "c": 3 - }, - "_update": { - "children": [ - "Pepíček", - "Tonička", - "Maruška" - ] - } -} -""" - -class TestHappyPath(unittest.TestCase): +class OurTestCase(unittest.TestCase): def _run_test(self, oldf, newf, difff, msg="", inc=(), exc=()): diffator = json_diff.Comparator(oldf, newf, inc, exc) diff = diffator.compare_dicts() @@ -201,15 +22,32 @@ class TestHappyPath(unittest.TestCase): (json.dumps(expected, sort_keys=True, indent=4, ensure_ascii=False), json.dumps(diff, sort_keys=True, indent=4, ensure_ascii=False))) + def _run_test_strings(self, olds, news, diffs, msg="", inc=(), exc=()): + self._run_test(StringIO(olds), StringIO(news), StringIO(diffs), msg, inc, exc) + def _run_test_formatted(self, oldf, newf, difff, msg=""): diffator = json_diff.Comparator(oldf, newf) diff = ("\n".join([line.strip() \ - for line in unicode(json_diff.HTMLFormatter(diffator.compare_dicts())).split("\n")])).strip() + for line in unicode( \ + json_diff.HTMLFormatter(diffator.compare_dicts())).split("\n")])).strip() expected = ("\n".join([line.strip() for line in difff if line])).strip() self.assertEqual(diff, expected, msg + "\n\nexpected = %s\n\nobserved = %s" % (expected, diff)) +#class TestUtilities(unittest.TestCase): +# def test_is_dict_interesting(self): +# diffator = json_diff.Comparator(StringIO(NESTED_OLD), StringIO(NESTED_NEW), +# included_attrs=('nome',)) +# old_res = diffator.dict_no_key_included(diffator.obj1) +# self.assertFalse(old_res, +# "check whether the old dict should be excluded or not") # or True? FIXME +# new_res = diffator.dict_no_key_included(diffator.obj2) +# self.assertFalse(new_res, +# "check whether the new dict should be excluded or not") # or True? FIXME + + +class TestBasicJSONHappyPath(OurTestCase): def test_empty(self): diffator = json_diff.Comparator({}, {}) diff = diffator.compare_dicts() @@ -217,8 +55,32 @@ class TestHappyPath(unittest.TestCase): "Empty objects diff.\n\nexpected = %s\n\nobserved = %s" % ({}, diff)) + def test_null(self): + self._run_test_strings('{"a": null}', '{"a": null}', + '{}', "Nulls") + + def test_null_to_string(self): + self._run_test_strings('{"a": null}', '{"a": "something"}', + '{"_update": {"a": "something"}}', "Null changed to string") + + def test_boolean(self): + self._run_test_strings('{"a": true}', '{"a": false}', + '{"_update": {"a": false}}', "Booleans") + + def test_integer(self): + self._run_test_strings('{"a": 1}', '{"a": 2}', + '{"_update": {"a": 2}}', "Integers") + + def test_float(self): + self._run_test_strings('{"a": 1.0}', '{"a": 1.1}', + '{"_update": {"a": 1.1}}', "Floats") + + def test_int_to_float(self): + self._run_test_strings('{"a": 1}', '{"a": 1.0}', + '{"_update": {"a": 1.0}}', "Integer changed to float") + def test_simple(self): - self._run_test(StringIO(SIMPLE_OLD), StringIO(SIMPLE_NEW), StringIO(SIMPLE_DIFF), + self._run_test_strings(SIMPLE_OLD, SIMPLE_NEW, SIMPLE_DIFF, "All-scalar objects diff.") def test_simple_formatted(self): @@ -227,44 +89,50 @@ class TestHappyPath(unittest.TestCase): "All-scalar objects diff (formatted).") def test_simple_array(self): - self._run_test(StringIO(SIMPLE_ARRAY_OLD), StringIO(SIMPLE_ARRAY_NEW), - StringIO(SIMPLE_ARRAY_DIFF), "Simple array objects diff.") + self._run_test_strings(SIMPLE_ARRAY_OLD, SIMPLE_ARRAY_NEW, + SIMPLE_ARRAY_DIFF, "Simple array objects diff.") def test_realFile(self): self._run_test(open("test/old.json"), open("test/new.json"), open("test/diff.json"), "Simply nested objects (from file) diff.") def test_nested(self): - self._run_test(StringIO(NESTED_OLD), StringIO(NESTED_NEW), - StringIO(NESTED_DIFF), "Nested objects diff.") - - # def test_nested_excluded(self): - # self._run_test(StringIO(NESTED_OLD), StringIO(NESTED_NEW), - # StringIO(NESTED_DIFF_EXCL), "Nested objects diff.", exc=("name")) - -# def test_piglit_results(self): -# self._run_test(open("test/old-testing-data.json"), open("test/new-testing-data.json"), -# open("test/diff-testing-data.json"), "Large piglit results diff.") + self._run_test_strings(NESTED_OLD, NESTED_NEW, NESTED_DIFF, "Nested objects diff.") def test_nested_formatted(self): self._run_test_formatted(open("test/old.json"), open("test/new.json"), codecs.open("test/nested_html_output.html", "r", "utf-8"), "Simply nested objects (from file) diff formatted as HTML.") -NO_JSON_OLD = """ -THIS IS NOT A JSON STRING -""" - -NO_JSON_NEW = """ -AND THIS NEITHER -""" + def test_nested_excluded(self): + self._run_test_strings(NESTED_OLD, NESTED_NEW, NESTED_DIFF_EXCL, + "Nested objects diff.", exc=("nome",)) +# def test_nested_included(self): +# self._run_test_strings(NESTED_OLD, NESTED_NEW, NESTED_DIFF_INCL, +# "Nested objects diff.", inc=("nome",)) -class TestSadPath(unittest.TestCase): +class TestBasicJSONSadPath(OurTestCase): def test_no_JSON(self): self.assertRaises(json_diff.BadJSONError, json_diff.Comparator, StringIO(NO_JSON_OLD), StringIO(NO_JSON_NEW)) + def test_bad_JSON_no_hex(self): + self.assertRaises(json_diff.BadJSONError, self._run_test_strings, + '{"a": 0x1}', '{"a": 2}', '{"_update": {"a": 2}}', + "Hex numbers not supported") + + def test_bad_JSON_no_octal(self): + self.assertRaises(json_diff.BadJSONError, self._run_test_strings, + '{"a": 01}', '{"a": 2}', '{"_update": {"a": 2}}', + "Octal numbers not supported") + + +#class TestPiglitData(OurTestCase): +# pass +# def test_piglit_results(self): +# self._run_test(open("test/old-testing-data.json"), open("test/new-testing-data.json"), +# open("test/diff-testing-data.json"), "Large piglit results diff.") if __name__ == "__main__": unittest.main() |