GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — deepdiff/tests/test_serialization.py at master · benthomasson/deepdiff

Expand file tree

/

test_serialization.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
269 lines (222 loc) · 11.6 KB
/

test_serialization.py

Copy path

File metadata and controls

269 lines (222 loc) · 11.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#!/usr/bin/env python
import json
import sys
import pytest
import datetime
from pickle import UnpicklingError
from decimal import Decimal
from deepdiff import DeepDiff
from deepdiff.helper import py3_5, pypy3
from deepdiff.serialization import (
pickle_load, pickle_dump, ForbiddenModule, ModuleNotFoundError,
MODULE_NOT_FOUND_MSG, FORBIDDEN_MODULE_MSG, pretty_print_diff)
from ordered_set import OrderedSet
from tests import PicklableClass
import logging
logging.disable(logging.CRITICAL)
t1 = {1: 1, 2: 2, 3: 3, 4: {"a": "hello", "b": [1, 2, 3]}}
t2 = {1: 1, 2: 2, 3: 3, 4: {"a": "hello", "b": "world\n\n\nEnd"}}
class TestSerialization:
"""Tests for Serializations."""
def test_serialization_text(self):
ddiff = DeepDiff(t1, t2)
assert "builtins.list" in ddiff.to_json_pickle()
jsoned = ddiff.to_json()
assert "world" in jsoned
@pytest.mark.skipif(py3_5, reason='json pickle does not work well with py 3.5')
def test_deserialization(self):
ddiff = DeepDiff(t1, t2)
jsoned = ddiff.to_json_pickle()
ddiff2 = DeepDiff.from_json_pickle(jsoned)
assert ddiff == ddiff2
def test_serialization_tree(self):
ddiff = DeepDiff(t1, t2, view='tree')
pickle_jsoned = ddiff.to_json_pickle()
assert "world" in pickle_jsoned
jsoned = ddiff.to_json()
assert "world" in jsoned
@pytest.mark.skipif(py3_5, reason='json pickle does not work well with py 3.5')
def test_deserialization_tree(self):
ddiff = DeepDiff(t1, t2, view='tree')
jsoned = ddiff.to_json_pickle()
ddiff2 = DeepDiff.from_json_pickle(jsoned)
assert 'type_changes' in ddiff2
def test_serialize_custom_objects_throws_error(self):
class A:
pass
class B:
pass
t1 = A()
t2 = B()
ddiff = DeepDiff(t1, t2)
with pytest.raises(TypeError):
ddiff.to_json()
def test_serialize_custom_objects_with_default_mapping(self):
class A:
pass
class B:
pass
t1 = A()
t2 = B()
ddiff = DeepDiff(t1, t2)
default_mapping = {A: lambda x: 'obj A', B: lambda x: 'obj B'}
result = ddiff.to_json(default_mapping=default_mapping)
expected_result = {"type_changes": {"root": {"old_type": "A", "new_type": "B", "old_value": "obj A", "new_value": "obj B"}}}
assert expected_result == json.loads(result)
# These lines are long but make it easier to notice the difference:
@pytest.mark.parametrize('verbose_level, expected', [
(0, {"type_changes": {"root[0]": {"old_type": str, "new_type": int}}, "dictionary_item_added": ["root[1][5]"], "dictionary_item_removed": ["root[1][3]"], "values_changed": {"root[1][1]": {"new_value": 2, "old_value": 1}}, "iterable_item_added": {"root[2]": "d"}}),
(1, {"type_changes": {"root[0]": {"old_type": str, "new_type": int, "old_value": "a", "new_value": 10}}, "dictionary_item_added": ["root[1][5]"], "dictionary_item_removed": ["root[1][3]"], "values_changed": {"root[1][1]": {"new_value": 2, "old_value": 1}}, "iterable_item_added": {"root[2]": "d"}}),
(2, {"type_changes": {"root[0]": {"old_type": str, "new_type": int, "old_value": "a", "new_value": 10}}, "dictionary_item_added": {"root[1][5]": 6}, "dictionary_item_removed": {"root[1][3]": 4}, "values_changed": {"root[1][1]": {"new_value": 2, "old_value": 1}}, "iterable_item_added": {"root[2]": "d"}}),
])
def test_to_dict_at_different_verbose_level(self, verbose_level, expected):
t1 = ['a', {1: 1, 3: 4}, ]
t2 = [10, {1: 2, 5: 6}, 'd']
ddiff = DeepDiff(t1, t2, verbose_level=verbose_level)
assert expected == ddiff.to_dict()
class TestPickling:
def test_serialize(self):
obj = [1, 2, 3, None, {10: 11E2}, frozenset(['a', 'c']), OrderedSet([2, 1]),
datetime.datetime.utcnow(), datetime.time(11), Decimal('11.2'), 123.11]
serialized = pickle_dump(obj)
loaded = pickle_load(serialized)
assert obj == loaded
@pytest.mark.skipif(pypy3, reason='short pickle not supported in pypy3')
def test_pickle_that_is_string(self):
serialized_str = 'DeepDiff Delta Payload v0-0-1\nBlah'
with pytest.raises(UnpicklingError):
pickle_load(serialized_str)
def test_custom_object_deserialization_fails_without_explicit_permission(self):
obj = PicklableClass(10)
module_dot_name = 'tests.{}'.format(PicklableClass.__name__)
serialized = pickle_dump(obj)
expected_msg = FORBIDDEN_MODULE_MSG.format(module_dot_name)
with pytest.raises(ForbiddenModule) as excinfo:
pickle_load(serialized)
assert expected_msg == str(excinfo.value)
# Explicitly allowing the module to be loaded
loaded = pickle_load(serialized, safe_to_import={module_dot_name})
assert obj == loaded
# Explicitly allowing the module to be loaded. It can take a list instead of a set.
loaded2 = pickle_load(serialized, safe_to_import=[module_dot_name])
assert obj == loaded2
def test_unpickling_object_that_is_not_imported_raises_error(self):
def get_the_pickle():
import wave
obj = wave.Error
return pickle_dump(obj)
serialized = get_the_pickle()
# Making sure that the module is unloaded.
del sys.modules['wave']
module_dot_name = 'wave.Error'
expected_msg = MODULE_NOT_FOUND_MSG.format(module_dot_name)
with pytest.raises(ModuleNotFoundError) as excinfo:
pickle_load(serialized, safe_to_import=module_dot_name)
assert expected_msg == str(excinfo.value)
class TestDeepDiffPretty:
"""Tests for pretty() method of DeepDiff"""
class TestingClass:
one = 1
testing_class = TestingClass
@pytest.mark.parametrize('t1, t2, item_path, old_type, new_type, old_val_displayed, new_val_displayed',
[
[{2: 2, 4: 4}, {2: 'b', 4: 4}, 'root[2]', 'int', 'str', '2', '"b"'],
[[1, 2, 3], [1, '2', 3], 'root[1]', 'int', 'str', '2', '"2"'],
[[1, 2, 3], {1, 2, 3}, 'root', 'list', 'set', '[1, 2, 3]', '{1, 2, 3}']
])
def test_pretty_print_diff_type_changes(self, t1, t2, item_path, old_type, new_type, old_val_displayed,
new_val_displayed):
ddiff = DeepDiff(t1, t2, view='tree')
result = pretty_print_diff(ddiff.tree['type_changes'].items[0])
assert result == 'Type of {} changed from {} to {} and value changed from {} to {}.'.format(item_path, old_type, new_type, old_val_displayed, new_val_displayed)
@pytest.mark.parametrize('t1, t2, item_path',
[
[{2: 2, 4: 4}, {2: 2, 4: 4, 5: 5}, 'root[5]'],
])
def test_pretty_print_diff_dictionary_item_added(self, t1, t2, item_path):
ddiff = DeepDiff(t1, t2, view='tree')
result = pretty_print_diff(ddiff.tree['dictionary_item_added'].items[0])
assert result == 'Item {} added to dictionary.'.format(item_path)
@pytest.mark.parametrize('t1, t2, item_path',
[
[{2: 2, 4: 4}, {2: 2}, 'root[4]'],
])
def test_pretty_print_diff_dictionary_item_removed(self, t1, t2, item_path):
ddiff = DeepDiff(t1, t2, view='tree')
result = pretty_print_diff(ddiff.tree['dictionary_item_removed'].items[0])
assert result == 'Item {} removed from dictionary.'.format(item_path)
@pytest.mark.parametrize('t1, t2, item_path, old_val_displayed, new_val_displayed',
[
[{2: 2, 4: 4}, {2: 3, 4: 4}, 'root[2]', '2', '3'],
[['a', 'b', 'c'], ['a', 'b', 'd'], 'root[2]', '"c"', '"d"']
])
def test_pretty_print_diff_values_changed(self, t1, t2, item_path, old_val_displayed, new_val_displayed):
ddiff = DeepDiff(t1, t2, view='tree')
result = pretty_print_diff(ddiff.tree['values_changed'].items[0])
assert result == 'Value of {} changed from {} to {}.'.format(item_path, old_val_displayed, new_val_displayed)
@pytest.mark.parametrize('t1, t2, item_path',
[
[[1, 2, 3], [1, 2, 3, 4], 'root[3]'],
])
def test_pretty_print_diff_iterable_item_added(self, t1, t2, item_path):
ddiff = DeepDiff(t1, t2, view='tree')
result = pretty_print_diff(ddiff.tree['iterable_item_added'].items[0])
assert result == 'Item {} added to iterable.'.format(item_path)
@pytest.mark.parametrize('t1, t2, item_path',
[
[[1, 2, 3], [1, 2], 'root[2]'],
])
def test_pretty_print_diff_iterable_item_removed(self, t1, t2, item_path):
ddiff = DeepDiff(t1, t2, view='tree')
result = pretty_print_diff(ddiff.tree['iterable_item_removed'].items[0])
assert result == 'Item {} removed from iterable.'.format(item_path)
def test_pretty_print_diff_attribute_added(self):
t1 = self.testing_class()
t2 = self.testing_class()
t2.two = 2
ddiff = DeepDiff(t1, t2, view='tree')
result = pretty_print_diff(ddiff.tree['attribute_added'].items[0])
assert result == 'Attribute root.two added.'
def test_pretty_print_diff_attribute_removed(self):
t1 = self.testing_class()
t1.two = 2
t2 = self.testing_class()
ddiff = DeepDiff(t1, t2, view='tree')
result = pretty_print_diff(ddiff.tree['attribute_removed'].items[0])
assert result == 'Attribute root.two removed.'
@pytest.mark.parametrize('t1, t2, item_path',
[
[{1, 2}, {1, 2, 3}, 'root[3]'],
])
def test_pretty_print_diff_set_item_added(self, t1, t2, item_path):
ddiff = DeepDiff(t1, t2, view='tree')
result = pretty_print_diff(ddiff.tree['set_item_added'].items[0])
assert result == 'Item {} added to set.'.format(item_path)
@pytest.mark.parametrize('t1, t2, item_path',
[
[{1, 2, 3}, {1, 2}, 'root[3]'],
])
def test_pretty_print_diff_set_item_removed(self, t1, t2, item_path):
ddiff = DeepDiff(t1, t2, view='tree')
result = pretty_print_diff(ddiff.tree['set_item_removed'].items[0])
assert result == 'Item {} removed from set.'.format(item_path)
@pytest.mark.parametrize('t1, t2, item_path',
[
[[1, 2, 3, 2], [1, 2, 3], 'root[1]'],
])
def test_pretty_print_diff_repetition_change(self, t1, t2, item_path):
ddiff = DeepDiff(t1, t2, view='tree', ignore_order=True, report_repetition=True)
result = pretty_print_diff(ddiff.tree['repetition_change'].items[0])
assert result == 'Repetition change for item {}.'.format(item_path)
def test_pretty_form_method(self):
t1 = {2: 2, 3: 3, 4: 4}
t2 = {2: 'b', 4: 5, 5: 5}
ddiff = DeepDiff(t1, t2, view='tree')
result = ddiff.pretty()
expected = (
'Item root[5] added to dictionary.'
'\nItem root[3] removed from dictionary.'
'\nType of root[2] changed from int to str and value changed from 2 to "b".'
'\nValue of root[4] changed from 4 to 5.'
)
assert result == expected
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.