| 1 |
import pytest |
| 2 |
import datetime |
| 3 |
from time import sleep |
| 4 |
from unittest import mock |
| 5 |
from functools import partial |
| 6 |
from collections import namedtuple |
| 7 |
from deepdiff import DeepHash |
| 8 |
from deepdiff.helper import pypy3 |
| 9 |
from deepdiff.model import DiffLevel |
| 10 |
from deepdiff.diff import ( |
| 11 |
DeepDiff, PROGRESS_MSG, INVALID_VIEW_MSG, VERBOSE_LEVEL_RANGE_MSG, |
| 12 |
PURGE_LEVEL_RANGE_MSG) |
| 13 |
from concurrent.futures.process import ProcessPoolExecutor |
| 14 |
from concurrent.futures import as_completed |
| 15 |
from ipaddress import IPv4Address |
| 16 |
|
| 17 |
# Only the prep part of DeepHash. We don't need to test the actual hash function. |
| 18 |
DeepHashPrep = partial(DeepHash, apply_hash=False) |
| 19 |
|
| 20 |
|
| 21 |
def prep_str(obj, ignore_string_type_changes=True): |
| 22 |
return obj if ignore_string_type_changes else 'str:{}'.format(obj) |
| 23 |
|
| 24 |
|
| 25 |
Point = namedtuple('Point', ["x"]) |
| 26 |
point_obj = Point(x=11) |
| 27 |
|
| 28 |
|
| 29 |
class SlowDiffLevel(DiffLevel): |
| 30 |
is_run = False |
| 31 |
|
| 32 |
def __init__(self, *args, **kwargs): |
| 33 |
sleep(.1) |
| 34 |
super().__init__(*args, **kwargs) |
| 35 |
|
| 36 |
|
| 37 |
class TestDiffOther: |
| 38 |
|
| 39 |
@mock.patch('deepdiff.diff.DiffLevel', side_effect=SlowDiffLevel) |
| 40 |
def test_repeated_timer(self, MockDiffLevel): |
| 41 |
t1 = [1, [2]] |
| 42 |
t2 = [1, [3]] |
| 43 |
|
| 44 |
progress_logger = mock.Mock() |
| 45 |
DeepDiff(t1, t2, log_frequency_in_sec=0.02, progress_logger=progress_logger) |
| 46 |
assert PROGRESS_MSG.format(0, 0, 0) == progress_logger.call_args[0][0] |
| 47 |
|
| 48 |
def test_invalid_view(self): |
| 49 |
t1 = [1] |
| 50 |
t2 = [2] |
| 51 |
with pytest.raises(ValueError) as excinfo: |
| 52 |
DeepDiff(t1, t2, view='blah') |
| 53 |
assert str(excinfo.value) == INVALID_VIEW_MSG.format('blah') |
| 54 |
|
| 55 |
def test_truncate_datetime(self): |
| 56 |
d1 = {'a': datetime.datetime(2020, 5, 17, 22, 15, 34, 913070)} |
| 57 |
d2 = {'a': datetime.datetime(2020, 5, 17, 22, 15, 39, 296583)} |
| 58 |
res = DeepDiff(d1, d2, truncate_datetime='minute') |
| 59 |
assert res == {} |
| 60 |
|
| 61 |
res = DeepDiff(d1, d2, truncate_datetime='second') |
| 62 |
expected = datetime.datetime(2020, 5, 17, 22, 15, 39, tzinfo=datetime.timezone.utc) |
| 63 |
assert res['values_changed']["root['a']"]['new_value'] == expected |
| 64 |
|
| 65 |
d1 = {'a': datetime.time(22, 15, 34, 913070)} |
| 66 |
d2 = {'a': datetime.time(22, 15, 39, 296583)} |
| 67 |
|
| 68 |
res = DeepDiff(d1, d2, truncate_datetime='minute') |
| 69 |
assert res == {} |
| 70 |
|
| 71 |
res = DeepDiff(d1, d2, truncate_datetime='second') |
| 72 |
assert res['values_changed']["root['a']"]['new_value'] == 80139 |
| 73 |
|
| 74 |
def test_invalid_verbose_level(self): |
| 75 |
with pytest.raises(ValueError) as excinfo: |
| 76 |
DeepDiff(1, 2, verbose_level=5) |
| 77 |
assert str(excinfo.value) == VERBOSE_LEVEL_RANGE_MSG |
| 78 |
|
| 79 |
def test_invalid_cache_purge_level(self): |
| 80 |
with pytest.raises(ValueError) as excinfo: |
| 81 |
DeepDiff(1, 2, cache_purge_level=5) |
| 82 |
assert str(excinfo.value) == PURGE_LEVEL_RANGE_MSG |
| 83 |
|
| 84 |
def test_cache_purge_level_max(self): |
| 85 |
diff = DeepDiff([1], [2], cache_purge_level=1) |
| 86 |
assert len(diff.__dict__.keys()) > 10 |
| 87 |
diff2 = DeepDiff([1], [2], cache_purge_level=2) |
| 88 |
assert not diff2.__dict__ |
| 89 |
expected = {'values_changed': {'root[0]': {'new_value': 2, 'old_value': 1}}} |
| 90 |
assert expected == diff2 |
| 91 |
|
| 92 |
diff2 = DeepDiff([1], [2], cache_purge_level=2, view='tree') |
| 93 |
assert not diff2.__dict__ |
| 94 |
assert list(diff2.keys()) == ['values_changed'] |
| 95 |
|
| 96 |
def test_path_cache(self): |
| 97 |
diff = DeepDiff([1], [2], cache_purge_level=2, view='tree') |
| 98 |
path1 = diff['values_changed'][0].path() |
| 99 |
path2 = diff['values_changed'][0].path() |
| 100 |
assert 'root[0]' == path1 == path2 |
| 101 |
|
| 102 |
def test_bool_str1(self): |
| 103 |
t1 = {'key1': True} |
| 104 |
t2 = {'key1': 'Yes'} |
| 105 |
diff = DeepDiff(t1, t2, ignore_type_in_groups=[(bool, str)], |
| 106 |
ignore_numeric_type_changes=True) |
| 107 |
expected = { |
| 108 |
'values_changed': { |
| 109 |
"root['key1']": { |
| 110 |
'new_value': 'Yes', |
| 111 |
'old_value': True |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
assert diff == expected |
| 116 |
|
| 117 |
def test_bool_str2(self): |
| 118 |
t1 = {"default": True} |
| 119 |
t2 = {"default": "true"} |
| 120 |
|
| 121 |
diff = DeepDiff( |
| 122 |
t1, |
| 123 |
t2, |
| 124 |
ignore_type_in_groups=[(bool, str)], |
| 125 |
ignore_string_type_changes=True) |
| 126 |
expected = {'values_changed': {"root['default']": {'new_value': 'true', |
| 127 |
'old_value': True}}} |
| 128 |
assert diff == expected |
| 129 |
|
| 130 |
diff2 = DeepDiff( |
| 131 |
t2, |
| 132 |
t1, |
| 133 |
ignore_type_in_groups=[(bool, str)], |
| 134 |
ignore_string_type_changes=True) |
| 135 |
expected2 = {'values_changed': {"root['default']": {'new_value': True, 'old_value': 'true'}}} |
| 136 |
assert diff2 == expected2 |
| 137 |
|
| 138 |
def test_get_distance_cache_key(self): |
| 139 |
result = DeepDiff._get_distance_cache_key(added_hash=5, removed_hash=20) |
| 140 |
assert b'0x14--0x5dc' == result |
| 141 |
|
| 142 |
def test_multi_processing1(self): |
| 143 |
|
| 144 |
t1 = [[1, 2, 3, 9], [1, 2, 4, 10]] |
| 145 |
t2 = [[1, 2, 4, 10], [1, 2, 3, 10]] |
| 146 |
|
| 147 |
futures = [] |
| 148 |
expected_result = { |
| 149 |
'values_changed': { |
| 150 |
'root[0][2]': { |
| 151 |
'new_value': 4, |
| 152 |
'old_value': 3 |
| 153 |
}, |
| 154 |
'root[0][3]': { |
| 155 |
'new_value': 10, |
| 156 |
'old_value': 9 |
| 157 |
}, |
| 158 |
'root[1][2]': { |
| 159 |
'new_value': 3, |
| 160 |
'old_value': 4 |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
with ProcessPoolExecutor(max_workers=1) as executor: |
| 166 |
futures.append(executor.submit(DeepDiff, t1, t2)) |
| 167 |
|
| 168 |
for future in as_completed(futures, timeout=10): |
| 169 |
assert not future._exception |
| 170 |
assert expected_result == future._result |
| 171 |
|
| 172 |
def test_multi_processing2_with_ignore_order(self): |
| 173 |
|
| 174 |
t1 = [[1, 2, 3, 9], [1, 2, 4, 10]] |
| 175 |
t2 = [[1, 2, 4, 10], [1, 2, 3, 10]] |
| 176 |
|
| 177 |
futures = [] |
| 178 |
expected_result = {'values_changed': {'root[0][3]': {'new_value': 10, 'old_value': 9}}} |
| 179 |
|
| 180 |
with ProcessPoolExecutor(max_workers=1) as executor: |
| 181 |
futures.append(executor.submit(DeepDiff, t1, t2, ignore_order=True)) |
| 182 |
|
| 183 |
for future in as_completed(futures, timeout=10): |
| 184 |
assert not future._exception |
| 185 |
assert expected_result == future._result |
| 186 |
|
| 187 |
@pytest.mark.skipif(pypy3, reason="pypy3 expected results are different") |
| 188 |
def test_multi_processing3_deephash(self): |
| 189 |
x = "x" |
| 190 |
x_prep = prep_str(x) |
| 191 |
expected_result = { |
| 192 |
x: x_prep, |
| 193 |
point_obj: "ntPoint:{%s:int:11}" % x, |
| 194 |
11: 'int:11', |
| 195 |
} |
| 196 |
|
| 197 |
futures = [] |
| 198 |
with ProcessPoolExecutor(max_workers=1) as executor: |
| 199 |
futures.append(executor.submit(DeepHashPrep, point_obj, ignore_string_type_changes=True)) |
| 200 |
|
| 201 |
for future in as_completed(futures, timeout=10): |
| 202 |
assert not future._exception |
| 203 |
assert expected_result == future._result |
| 204 |
|
| 205 |
def test_ip_address_diff(self): |
| 206 |
ip1 = IPv4Address("128.0.0.1") |
| 207 |
ip2 = IPv4Address("128.0.0.2") |
| 208 |
diff = DeepDiff(ip1, ip2) |
| 209 |
assert {'values_changed': {'root': {'new_value': IPv4Address('128.0.0.2'), 'old_value': IPv4Address('128.0.0.1')}}} == diff |