Debian Codegraph @

tests/test_helper.py

raw
1 #!/usr/bin/env python
2 import pytest
3 import datetime
4 import numpy as np
5 from enum import Enum
6 from decimal import Decimal
7 from deepdiff.helper import (
8 short_repr, number_to_string, get_numpy_ndarray_rows,
9 cartesian_product_of_shape, literal_eval_extended,
10 not_found, diff_numpy_array, cartesian_product_numpy,
11 get_truncate_datetime, datetime_normalize,
12 detailed__dict__, ENUM_INCLUDE_KEYS, add_root_to_paths,
13 get_semvar_as_integer,
14 )
15
16
17 class MyEnum(Enum):
18 A = 1
19 B = 2
20
21
22 class TestHelper:
23 """Helper Tests."""
24
25 def test_short_repr_when_short(self):
26
27 item = {1: 2}
28 output = short_repr(item)
29 assert output == '{1: 2}'
30
31 def test_short_repr_when_long(self):
32
33 item = {'Eat more': 'burritos'}
34 output = short_repr(item)
35 assert output == "{'Eat more':...}"
36
37 @pytest.mark.parametrize("t1, t2, significant_digits, number_format_notation, expected_result",
38 [
39 (10, 10.0, 5, "f", True),
40 (10, 10.0, 5, "e", True),
41 (10, 10.2, 5, "f", ('10.00000', '10.20000')),
42 (10, 10.2, 5, "e", ('1.00000e+1', '1.02000e+1')),
43 (10, 10.2, 0, "f", True),
44 (10, 10.2, 0, "e", True),
45 (Decimal(10), 10, 0, "f", True),
46 (Decimal(10), 10, 0, "e", True),
47 (Decimal(10), 10, 10, "f", True),
48 (Decimal(10), 10, 10, "e", True),
49 (Decimal(10), 10.0, 0, "f", True),
50 (Decimal(10), 10.0, 0, "e", True),
51 (Decimal(10), 10.0, 10, "f", True),
52 (Decimal(10), 10.0, 10, "e", True),
53 (Decimal('10.0'), 10.0, 5, "f", True),
54 (Decimal('10.0'), 10.0, 5, "e", True),
55 (Decimal('10.01'), 10.01, 1, "f", True),
56 (Decimal('10.01'), 10.01, 1, "e", True),
57 (Decimal('10.01'), 10.01, 2, "f", True),
58 (Decimal('10.01'), 10.01, 2, "e", True),
59 (Decimal('10.01'), 10.01, 5, "f", True),
60 (Decimal('10.01'), 10.01, 5, "e", True),
61 (Decimal('10.01'), 10.01, 8, "f", True),
62 (Decimal('10.01'), 10.01, 8, "e", True),
63 (Decimal('10.010'), 10.01, 3, "f", True),
64 (Decimal('10.010'), 10.01, 3, "e", True),
65 (Decimal('100000.1'), 100000.1, 0, "f", True),
66 (Decimal('100000.1'), 100000.1, 0, "e", True),
67 (Decimal('100000.1'), 100000.1, 1, "f", True),
68 (Decimal('100000.1'), 100000.1, 1, "e", True),
69 (Decimal('100000.1'), 100000.1, 5, "f", True),
70 (Decimal('100000.1'), 100000.1, 5, "e", True),
71 (Decimal('100000'), 100000.1, 0, "f", True),
72 (Decimal('100000'), 100000.1, 0, "e", True),
73 (Decimal('100000'), 100000.1, 1, "f", ('100000.0', '100000.1')),
74 (Decimal('100000'), 100000.1, 1, "e", True),
75 (Decimal('-100000'), 100000.1, 1, "f", ('-100000.0', '100000.1')),
76 (Decimal('-100000'), 100000.1, 1, "e", ("-1.0e+5","1.0e+5")),
77 (0, 0.0, 5, "f", True),
78 (0, 0.0, 5, "e", True),
79 (0, 0.2, 5, "f", ('0.00000', '0.20000')),
80 (0, 0.2, 5, "e", ('0.00000e+0', '2.00000e-1')),
81 (0, 0.2, 0, "f", True),
82 (0, 0.2, 0, "e", True),
83 (Decimal(0), 0, 0, "f", True),
84 (Decimal(0), 0, 0, "e", True),
85 (Decimal(0), 0, 10, "f", True),
86 (Decimal(0), 0, 10, "e", True),
87 (Decimal(0), 0.0, 0, "f", True),
88 (Decimal(0), 0.0, 0, "e", True),
89 (Decimal(0), 0.0, 10, "f", True),
90 (Decimal(0), 0.0, 10, "e", True),
91 (Decimal('0.0'), 0.0, 5, "f", True),
92 (Decimal('0.0'), 0.0, 5, "e", True),
93 (Decimal('0.01'), 0.01, 1, "f", True),
94 (Decimal('0.01'), 0.01, 1, "e", True),
95 (Decimal('0.01'), 0.01, 2, "f", True),
96 (Decimal('0.01'), 0.01, 2, "e", True),
97 (Decimal('0.01'), 0.01, 5, "f", True),
98 (Decimal('0.01'), 0.01, 5, "e", True),
99 (Decimal('0.01'), 0.01, 8, "f", True),
100 (Decimal('0.01'), 0.01, 8, "e", True),
101 (Decimal('0.010'), 0.01, 3, "f", True),
102 (Decimal('0.010'), 0.01, 3, "e", True),
103 (Decimal('0.00002'), 0.00001, 0, "f", True),
104 (Decimal('0.00002'), 0.00001, 0, "e", True),
105 (Decimal('0.00002'), 0.00001, 1, "f", True),
106 (Decimal('0.00002'), 0.00001, 1, "e", True),
107 (Decimal('0.00002'), 0.00001, 5, "f", ('0.00002', '0.00001')),
108 (Decimal('0.00002'), 0.00001, 5, "e", ('2.00000e-5', '1.00000e-5')),
109 (Decimal('0.00002'), 0.00001, 6, "f", ('0.000020', '0.000010')),
110 (Decimal('0.00002'), 0.00001, 6, "e", ('2.000000e-5', '1.000000e-5')),
111 (Decimal('0'), 0.1, 0, "f", True),
112 (Decimal('0'), 0.1, 0, "e", True),
113 (Decimal('0'), 0.1, 1, "f", ('0.0', '0.1')),
114 (Decimal('0'), 0.1, 1, "e", ('0.0e+0', '1.0e-1')),
115 (-0, 0.0, 5, "f", True),
116 (-0, 0.0, 5, "e", True),
117 (-0, 0.2, 5, "f", ('0.00000', '0.20000')),
118 (-0, 0.2, 5, "e", ('0.00000e+0', '2.00000e-1')),
119 (-0, 0.2, 0, "f", True),
120 (-0, 0.2, 0, "e", True),
121 (Decimal(-0), 0, 0, "f", True),
122 (Decimal(-0), 0, 0, "e", True),
123 (Decimal(-0), 0, 10, "f", True),
124 (Decimal(-0), 0, 10, "e", True),
125 (Decimal(-0), 0.0, 0, "f", True),
126 (Decimal(-0), 0.0, 0, "e", True),
127 (Decimal(-0), 0.0, 10, "f", True),
128 (Decimal(-0), 0.0, 10, "e", True),
129 (Decimal('-0.0'), 0.0, 5, "f", True),
130 (Decimal('-0.0'), 0.0, 5, "e", True),
131 (Decimal('-0.01'), 0.01, 1, "f", True),
132 (Decimal('-0.01'), 0.01, 1, "e", True),
133 (Decimal('-0.01'), 0.01, 2, "f", ('-0.01', '0.01')),
134 (Decimal('-0.01'), 0.01, 2, "e", ('-1.00e-2', '1.00e-2')),
135 (Decimal('-0.00002'), 0.00001, 0, "f", True),
136 (Decimal('-0.00002'), 0.00001, 0, "e", True),
137 (Decimal('-0.00002'), 0.00001, 1, "f", True),
138 (Decimal('-0.00002'), 0.00001, 1, "e", True),
139 (Decimal('-0.00002'), 0.00001, 5, "f", ('-0.00002', '0.00001')),
140 (Decimal('-0.00002'), 0.00001, 5, "e", ('-2.00000e-5', '1.00000e-5')),
141 (Decimal('-0.00002'), 0.00001, 6, "f", ('-0.000020', '0.000010')),
142 (Decimal('-0.00002'), 0.00001, 6, "e", ('-2.000000e-5', '1.000000e-5')),
143 (Decimal('-0'), 0.1, 0, "f", True),
144 (Decimal('-0'), 0.1, 0, "e", True),
145 (Decimal('-0'), 0.1, 1, "f", ('0.0', '0.1')),
146 (Decimal('-0'), 0.1, 1, "e", ('0.0e+0', '1.0e-1')),
147 ])
148 def test_number_to_string_decimal_digits(self, t1, t2, significant_digits, number_format_notation, expected_result):
149 st1 = number_to_string(t1, significant_digits=significant_digits, number_format_notation=number_format_notation)
150 st2 = number_to_string(t2, significant_digits=significant_digits, number_format_notation=number_format_notation)
151 if expected_result is True:
152 assert st1 == st2
153 else:
154 assert st1 == expected_result[0]
155 assert st2 == expected_result[1]
156
157 @pytest.mark.parametrize("t1, t2, significant_digits, number_format_notation, expected_result",
158 [
159 (10j, 10.0j, 5, "f", True),
160 (10j, 10.0j, 5, "e", True),
161 (4+10j, 4.0000002+10.0000002j, 5, "f", True),
162 (4+10j, 4.0000002+10.0000002j, 5, "e", True),
163 (4+10j, 4.0000002+10.0000002j, 7, "f", ('4.0000000+10.0000000j', '4.0000002+10.0000002j')),
164 (4+10j, 4.0000002+10.0000002j, 7, "e", ('4.0000000e+0+1.0000000e+1j', '4.0000002e+0+1.0000000e+1j')),
165 (0.00002+0.00002j, 0.00001+0.00001j, 0, "f", True),
166 (0.00002+0.00002j, 0.00001+0.00001j, 0, "e", True),
167 (0.00002+0.00002j, 0.00001+0.00001j, 5, "f", ('0.00002+0.00002j', '0.00001+0.00001j')),
168 (0.00002+0.00002j, 0.00001+0.00001j, 5, "e", ('2.00000e-5+2.00000e-5j', '1.00000e-5+1.00000e-5j')),
169 (-0.00002-0.00002j, 0.00001+0.00001j, 0, "f", True),
170 (-0.00002-0.00002j, 0.00001+0.00001j, 0, "e", True),
171 (10j, 10.2j, 5, "f", ('0.00000+10.00000j', '0.00000+10.20000j')),
172 (10j, 10.2j, 5, "e", ('0.00000e+0+1.00000e+1j', '0.00000e+0+1.02000e+1j')),
173 (10j, 10.2j, 0, "f", True),
174 (10j, 10.2j, 0, "e", True),
175 (0j, 0.0j, 5, "f", True),
176 (0j, 0.0j, 5, "e", True),
177 (0j, 0.2j, 5, "f", ('0.00000', '0.00000+0.20000j')),
178 (0j, 0.2j, 5, "e", ('0.00000e+0', '0.00000e+0+2.00000e-1j')),
179 (0j, 0.2j, 0, "f", True),
180 (0j, 0.2j, 0, "e", True),
181 (-0j, 0.0j, 5, "f", True),
182 (-0j, 0.0j, 5, "e", True),
183 (-0j, 0.2j, 5, "f", ('0.00000', '0.00000+0.20000j')),
184 (-0j, 0.2j, 5, "e", ('0.00000e+0', '0.00000e+0+2.00000e-1j')),
185 (-0j, 0.2j, 0, "f", True),
186 (-0j, 0.2j, 0, "e", True),
187 ])
188 def test_number_to_string_complex_digits(self, t1, t2, significant_digits, number_format_notation, expected_result):
189 st1 = number_to_string(t1, significant_digits=significant_digits, number_format_notation=number_format_notation)
190 st2 = number_to_string(t2, significant_digits=significant_digits, number_format_notation=number_format_notation)
191 if expected_result is True:
192 assert st1 == st2
193 else:
194 assert st1 == expected_result[0]
195 assert st2 == expected_result[1]
196
197 def test_number_to_string_with_invalid_notation(self):
198 with pytest.raises(ValueError):
199 number_to_string(10, significant_digits=4, number_format_notation='blah')
200
201 def test_cartesian_product_of_shape(self):
202 result = list(cartesian_product_of_shape([2, 1, 3]))
203 assert [(0, 0, 0), (0, 0, 1), (0, 0, 2), (1, 0, 0), (1, 0, 1), (1, 0, 2)] == result
204
205 def test_get_numpy_ndarray_rows(self):
206 obj = np.array([[[1, 2, 3], [4, 5, 6]]], np.int32)
207 path0 = (0, 0)
208 row0 = np.array([1, 2, 3], dtype=np.int32)
209 (path0, row0) = next(get_numpy_ndarray_rows(obj))
210
211 path1 = (0, 1)
212 row1 = np.array([4, 5, 6], dtype=np.int32)
213 (path1, row1) = next(get_numpy_ndarray_rows(obj))
214
215 @pytest.mark.parametrize('item, expected', [
216 ('10', 10),
217 ("Decimal('10.1')", Decimal('10.1')),
218 ("datetime.datetime(2021, 10, 13, 4, 54, 48, 959835)", datetime.datetime(2021, 10, 13, 4, 54, 48, 959835)),
219 ("datetime.date(2021, 10, 13)", datetime.date(2021, 10, 13)),
220 ])
221 def test_literal_eval_extended(self, item, expected):
222 result = literal_eval_extended(item)
223 assert expected == result
224
225 def test_not_found_inequality(self):
226 assert not_found != not_found
227
228 @pytest.mark.parametrize('array1, array2, expected', [
229 (np.array([3, 1, 2, 4, 3]), np.array([5, 2, 4]), [3, 1, 3]),
230 (np.array([5, 2, 4]), np.array([3, 1, 2, 4, 3]), [5]),
231 ])
232 def test_diff_numpy_array(self, array1, array2, expected):
233 result = diff_numpy_array(array1, array2)
234 assert expected == result.tolist()
235
236 def test_cartesian_product_numpy(self):
237 result = cartesian_product_numpy(np.array([3, 1, 2, 4, 3]), np.array([5, 2, 4]))
238 expected = [
239 [3, 5],
240 [3, 2],
241 [3, 4],
242 [1, 5],
243 [1, 2],
244 [1, 4],
245 [2, 5],
246 [2, 2],
247 [2, 4],
248 [4, 5],
249 [4, 2],
250 [4, 4],
251 [3, 5],
252 [3, 2],
253 [3, 4]]
254 assert expected == result.tolist()
255
256 def test_get_truncate_datetime(self):
257 result = get_truncate_datetime('hour')
258 assert 'hour' == result
259 with pytest.raises(ValueError):
260 get_truncate_datetime('blah')
261
262 @pytest.mark.parametrize('truncate_datetime, obj, expected', [
263 ('hour',
264 datetime.datetime(2020, 5, 30, 7, 28, 51, 698308),
265 datetime.datetime(2020, 5, 30, 7, 0, tzinfo=datetime.timezone.utc)),
266 ('day',
267 datetime.datetime(2020, 5, 30, 7, 28, 51, 698308),
268 datetime.datetime(2020, 5, 30, 0, 0, tzinfo=datetime.timezone.utc)),
269 ])
270 def test_datetime_normalize(self, truncate_datetime, obj, expected):
271 result = datetime_normalize(truncate_datetime, obj)
272 assert expected == result
273
274 @pytest.mark.parametrize('obj, include_keys, expected', [
275 (
276 MyEnum.A,
277 ENUM_INCLUDE_KEYS,
278 {'__objclass__': MyEnum, 'name': 'A', 'value': 1},
279 )
280 ])
281 def test_detailed__dict__(self, obj, include_keys, expected):
282 result = detailed__dict__(obj, ignore_private_variables=True, include_keys=include_keys)
283 assert expected == result, f"test_detailed__dict__ failed for {obj}"
284
285 @pytest.mark.parametrize('test_num, value, expected', [
286 (1, ['ab'], {'root.ab', "root['ab']"}),
287 (2, ['11'], {"root['11']", 'root[11]'}),
288 (3, ['1a'], {"root['1a']"}),
289 ])
290 def test_add_root_to_paths(self, test_num, value, expected):
291 result = add_root_to_paths(value)
292 assert expected == result, f"test_add_root_to_paths #{test_num} failed."
293
294 @pytest.mark.parametrize('test_num, value, expected', [
295 (1, '1.2.3', 1002003),
296 (2, '1.22.3', 1022003),
297 (3, '1.22.3c', 1022003),
298 (4, '2.4', 2004000),
299 (5, '1.19.0', 1019000),
300 ])
301 def test_get_semvar_as_integer(self, test_num, value, expected):
302 result = get_semvar_as_integer(value)
303 assert expected == result, f"test_get_semvar_as_integer #{test_num} failed."