aboutsummaryrefslogtreecommitdiffstats
path: root/test_json_diff.py
diff options
context:
space:
mode:
authorMatěj Cepl <mcepl@redhat.com>2011-10-15 00:55:52 +0200
committerMatěj Cepl <mcepl@redhat.com>2011-10-24 23:25:49 +0200
commit2b225c87deb8ceebfdeac2c2be56eaf0ca9caee3 (patch)
tree64c78f2b74b79d4dc82e78dee22d0908a587ad50 /test_json_diff.py
parent11144b854286fa8fa49a7f770cf995f75d3ccc6f (diff)
downloadjson_diff-2b225c87deb8ceebfdeac2c2be56eaf0ca9caee3.tar.gz
First working (w/unit tests) version.
Includes also tests for results of piglit run.
Diffstat (limited to 'test_json_diff.py')
-rw-r--r--test_json_diff.py122
1 files changed, 122 insertions, 0 deletions
diff --git a/test_json_diff.py b/test_json_diff.py
new file mode 100644
index 0000000..3a514eb
--- /dev/null
+++ b/test_json_diff.py
@@ -0,0 +1,122 @@
+# -*- coding: utf-8 -*-
+"""
+PyUnit unit tests
+"""
+import unittest
+import json
+import json_diff
+from StringIO import StringIO
+
+SIMPLE_OLD = u"""
+{
+ "a": 1,
+ "b": true,
+ "c": "Janošek"
+}
+"""
+
+SIMPLE_NEW = u"""
+{
+ "b": false,
+ "c": "Maruška",
+ "d": "přidáno"
+}
+"""
+
+SIMPLE_DIFF = u"""
+{
+ "append": {
+ "d": "přidáno"
+ },
+ "remove": {
+ "a": 1
+ },
+ "update": {
+ "c": "Maruška",
+ "b": false
+ }
+}
+"""
+
+NESTED_OLD = u"""
+{
+ "a": 1,
+ "b": 2,
+ "son": {
+ "name": "Janošek"
+ }
+}
+"""
+
+NESTED_NEW = u"""
+{
+ "a": 2,
+ "c": 3,
+ "daughter": {
+ "name": "Maruška"
+ }
+}
+"""
+
+NESTED_DIFF = u"""
+{
+ "append": {
+ "c": 3,
+ "daughter": {
+ "name": "Maruška"
+ }
+ },
+ "remove": {
+ "b": 2,
+ "son": {
+ "name": "Janošek"
+ }
+ },
+ "update": {
+ "a": 2
+ }
+}
+"""
+
+class TestXorgAnalyze(unittest.TestCase):
+ def test_empty(self):
+ diffator = json_diff.Comparator({}, {})
+ diff = diffator.compare_dicts()
+ self.assertEqual(json.dumps(diff).strip(), "{}", \
+ "Empty objects diff.\n\nexpected = %s\n\nobserved = %s" % \
+ (str({}), str(diff)))
+
+ def test_simple(self):
+ diffator = json_diff.Comparator(StringIO(SIMPLE_OLD), StringIO(SIMPLE_NEW))
+ diff = diffator.compare_dicts()
+ expected = json.loads(SIMPLE_DIFF)
+ self.assertEqual(diff, expected, "All-scalar objects diff." + \
+ "\n\nexpected = %s\n\nobserved = %s" % \
+ (str(expected), str(diff)))
+
+ def test_realFile(self):
+ diffator = json_diff.Comparator(open("test/old.json"), open("test/new.json"))
+ diff = diffator.compare_dicts()
+ expected = json.load(open("test/diff.json"))
+ self.assertEqual(diff, expected, "Simply nested objects (from file) diff." + \
+ "\n\nexpected = %s\n\nobserved = %s" % \
+ (str(expected), str(diff)))
+
+ def test_nested(self):
+ diffator = json_diff.Comparator(StringIO(NESTED_OLD), StringIO(NESTED_NEW))
+ diff = diffator.compare_dicts()
+ expected = json.loads(NESTED_DIFF)
+ self.assertEqual(diff, expected, "Nested objects diff. " + \
+ "\n\nexpected = %s\n\nobserved = %s" % \
+ (str(expected), str(diff)))
+ def test_large_with_exclusions(self):
+ diffator = json_diff.Comparator(open("test/old-testing-data.json"), \
+ open("test/new-testing-data.json"), ('command', 'time'))
+ diff = diffator.compare_dicts()
+ expected = json.load(open("test/diff-testing-data.json"))
+ self.assertEqual(diff, expected, "Large objects with exclusions diff." + \
+ "\n\nexpected = %s\n\nobserved = %s" % \
+ (str(expected), str(diff)))
+
+if __name__ == "__main__":
+ unittest.main() \ No newline at end of file