aboutsummaryrefslogtreecommitdiffstats
path: root/test_json_diff.py
blob: caffe954d8ec371719a3df426472131ef5f10d88 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# -*- coding: utf-8 -*-
"""
PyUnit unit tests
"""
from __future__ import division, absolute_import
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
    }
}
"""

SIMPLE_ARRAY_OLD = u"""
{
   "a": [ 1 ]
}
"""

SIMPLE_ARRAY_NEW = u"""
{
   "a": [ 1, 2 ]
}
"""

SIMPLE_ARRAY_DIFF = u"""
{
    "_append": {
        "a": {
            "1": 2
        }
    }
}
"""

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
    }
}
"""

ARRAY_OLD = u"""
{
    "a": 1,
    "b": 2,
    "children": [
        "Pepíček", "Anička", "Maruška"
    ]
}
"""

ARRAY_NEW = u"""
{
    "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):
    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_nested_formatted(self):
        diffator = json_diff.Comparator(open("test/old.json"), open("test/new.json"))
        diff = "\n".join([line.strip() \
                for line in str(json_diff.HTMLFormatter(diffator.compare_dicts())).split("\n")])
        expected = "\n".join([line.strip() for line in open("test/nested_html_output.html").readlines()])
        self.assertEqual(diff, expected, "Simply nested objects (from file) diff formatted as HTML." +
                         "\n\nexpected = %s\n\nobserved = %s" %
                         (expected, diff))


NO_JSON_OLD = u"""
THIS IS NOT A JSON STRING
"""

NO_JSON_NEW = u"""
AND THIS NEITHER
"""


class TestSadPath(unittest.TestCase):
    def test_no_JSON(self):
        self.assertRaises(json_diff.BadJSONError,
                json_diff.Comparator, StringIO(NO_JSON_OLD), StringIO(NO_JSON_NEW))


if __name__ == "__main__":
    unittest.main()