aboutsummaryrefslogtreecommitdiffstats
path: root/test_json_diff.py
blob: 453eba4576f59046989f6e75245f6a94df22a08c (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# -*- coding: utf-8 -*-
"""
PyUnit unit tests
"""
from __future__ import division, absolute_import, unicode_literals
import unittest
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
            }
        }
    }
}
"""

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):
    def _run_test(self, oldf, newf, difff, msg="", inc=(), exc=()):
        diffator = json_diff.Comparator(oldf, newf, inc, exc)
        diff = diffator.compare_dicts()
        expected = json.load(difff)
        self.assertEqual(json.dumps(diff, sort_keys=True), json.dumps(expected, sort_keys=True),
                         msg + "\n\nexpected = %s\n\nobserved = %s" %
                         (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_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()
        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))

    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" %
             ({}, diff))

    def test_simple(self):
        self._run_test(StringIO(SIMPLE_OLD), StringIO(SIMPLE_NEW), StringIO(SIMPLE_DIFF),
                "All-scalar objects diff.")

    def test_simple_formatted(self):
        self._run_test_formatted(StringIO(SIMPLE_OLD), StringIO(SIMPLE_NEW),
            StringIO(SIMPLE_DIFF_HTML),
            "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.")

    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.")

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


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()