| 1 |
import pytest |
| 2 |
from decimal import Decimal |
| 3 |
from deepdiff import DeepDiff |
| 4 |
from deepdiff.helper import py_current_version |
| 5 |
|
| 6 |
|
| 7 |
class TestCache: |
| 8 |
|
| 9 |
@pytest.mark.slow |
| 10 |
def test_cache_deeply_nested_a1(self, nested_a_t1, nested_a_t2, nested_a_result, nested_a_affected_paths, benchmark): |
| 11 |
benchmark(self._test_cache_deeply_nested_a1, nested_a_t1, nested_a_t2, nested_a_result, nested_a_affected_paths) |
| 12 |
|
| 13 |
def _test_cache_deeply_nested_a1(self, nested_a_t1, nested_a_t2, nested_a_result, nested_a_affected_paths): |
| 14 |
diff = DeepDiff(nested_a_t1, nested_a_t2, ignore_order=True, |
| 15 |
cache_size=5000, cache_tuning_sample_size=280, |
| 16 |
cutoff_intersection_for_pairs=1) |
| 17 |
|
| 18 |
stats = diff.get_stats() |
| 19 |
expected_stats = { |
| 20 |
"PASSES COUNT": 1671, |
| 21 |
"DIFF COUNT": 8556, |
| 22 |
"DISTANCE CACHE HIT COUNT": 3445, |
| 23 |
"MAX PASS LIMIT REACHED": False, |
| 24 |
"MAX DIFF LIMIT REACHED": False, |
| 25 |
} |
| 26 |
# assert expected_stats == stats |
| 27 |
assert nested_a_result == diff |
| 28 |
diff_of_diff = DeepDiff(nested_a_result, diff.to_dict(), ignore_order=False) |
| 29 |
assert not diff_of_diff |
| 30 |
assert nested_a_affected_paths == diff.affected_paths |
| 31 |
assert [0, 1] == diff.affected_root_keys |
| 32 |
|
| 33 |
@pytest.mark.slow |
| 34 |
def test_cache_deeply_nested_a2(self, nested_a_t1, nested_a_t2, nested_a_result): |
| 35 |
|
| 36 |
diff = DeepDiff(nested_a_t1, nested_a_t2, ignore_order=True, |
| 37 |
cache_size=500, cache_tuning_sample_size=500, |
| 38 |
cutoff_intersection_for_pairs=1) |
| 39 |
|
| 40 |
stats = diff.get_stats() |
| 41 |
# Somehow just in python 3.5 the cache stats are different. Weird. |
| 42 |
expected_stats = { |
| 43 |
'PASSES COUNT': 3960, |
| 44 |
'DIFF COUNT': 19469, |
| 45 |
'DISTANCE CACHE HIT COUNT': 11847, |
| 46 |
'MAX PASS LIMIT REACHED': False, |
| 47 |
'MAX DIFF LIMIT REACHED': False |
| 48 |
} |
| 49 |
assert not DeepDiff(expected_stats, stats, use_log_scale=True) |
| 50 |
assert nested_a_result == diff |
| 51 |
diff_of_diff = DeepDiff(nested_a_result, diff.to_dict(), ignore_order=False) |
| 52 |
assert not diff_of_diff |
| 53 |
|
| 54 |
def test_cache_deeply_nested_b(self, nested_b_t1, nested_b_t2, nested_b_result): |
| 55 |
|
| 56 |
diff = DeepDiff(nested_b_t1, nested_b_t2, ignore_order=True, |
| 57 |
cache_size=5000, cache_tuning_sample_size=0, |
| 58 |
cutoff_intersection_for_pairs=1) |
| 59 |
|
| 60 |
stats = diff.get_stats() |
| 61 |
expected_stats = { |
| 62 |
'PASSES COUNT': 110, |
| 63 |
'DIFF COUNT': 306, |
| 64 |
'DISTANCE CACHE HIT COUNT': 0, |
| 65 |
'MAX PASS LIMIT REACHED': False, |
| 66 |
'MAX DIFF LIMIT REACHED': False |
| 67 |
} |
| 68 |
stats_diff = DeepDiff(expected_stats, stats, use_log_scale=True, log_scale_similarity_threshold=0.15) |
| 69 |
assert not stats_diff |
| 70 |
assert nested_b_result == diff |
| 71 |
|
| 72 |
diff_of_diff = DeepDiff(nested_b_result, diff.to_dict(), ignore_order=False) |
| 73 |
assert not diff_of_diff |
| 74 |
|
| 75 |
def test_cache_1D_array_of_numbers_that_do_not_overlap(self): |
| 76 |
ignore_order = True |
| 77 |
cache_size = 5000 |
| 78 |
max_diffs = 30000 |
| 79 |
max_passes = 40000 |
| 80 |
|
| 81 |
t1 = list(range(1, 30)) |
| 82 |
|
| 83 |
t2 = list(range(100, 120)) |
| 84 |
|
| 85 |
diff = DeepDiff(t1, t2, ignore_order=ignore_order, max_passes=max_passes, |
| 86 |
max_diffs=max_diffs, cache_size=cache_size, cache_tuning_sample_size=100) |
| 87 |
|
| 88 |
stats = diff.get_stats() |
| 89 |
# Since there was no overlap between the 2 arrays, even though ignore_order=True, |
| 90 |
# the algorithm switched to comparing by order. |
| 91 |
expected_stats = { |
| 92 |
'PASSES COUNT': 0, |
| 93 |
'DIFF COUNT': 50, |
| 94 |
'DISTANCE CACHE HIT COUNT': 0, |
| 95 |
'MAX PASS LIMIT REACHED': False, |
| 96 |
'MAX DIFF LIMIT REACHED': False |
| 97 |
} |
| 98 |
assert expected_stats == stats |
| 99 |
|
| 100 |
expected = {'values_changed': {'root[0]': {'new_value': 100, 'old_value': 1}, 'root[1]': {'new_value': 101, 'old_value': 2}, 'root[2]': {'new_value': 102, 'old_value': 3}, 'root[3]': {'new_value': 103, 'old_value': 4}, 'root[4]': {'new_value': 104, 'old_value': 5}, 'root[5]': {'new_value': 105, 'old_value': 6}, 'root[6]': {'new_value': 106, 'old_value': 7}, 'root[7]': {'new_value': 107, 'old_value': 8}, 'root[8]': {'new_value': 108, 'old_value': 9}, 'root[9]': {'new_value': 109, 'old_value': 10}, 'root[10]': {'new_value': 110, 'old_value': 11}, 'root[11]': {'new_value': 111, 'old_value': 12}, 'root[12]': {'new_value': 112, 'old_value': 13}, 'root[13]': {'new_value': 113, 'old_value': 14}, 'root[14]': {'new_value': 114, 'old_value': 15}, 'root[15]': {'new_value': 115, 'old_value': 16}, 'root[16]': {'new_value': 116, 'old_value': 17}, 'root[17]': {'new_value': 117, 'old_value': 18}, 'root[18]': {'new_value': 118, 'old_value': 19}, 'root[19]': {'new_value': 119, 'old_value': 20}}, 'iterable_item_removed': {'root[20]': 21, 'root[21]': 22, 'root[22]': 23, 'root[23]': 24, 'root[24]': 25, 'root[25]': 26, 'root[26]': 27, 'root[27]': 28, 'root[28]': 29}} |
| 101 |
assert expected == diff |
| 102 |
|
| 103 |
def test_cache_1D_array_of_numbers_that_overlap(self): |
| 104 |
ignore_order = True |
| 105 |
cache_size = 5000 |
| 106 |
max_diffs = 30000 |
| 107 |
max_passes = 40000 |
| 108 |
|
| 109 |
t1 = list(range(1, 30)) |
| 110 |
|
| 111 |
t2 = list(range(13, 33)) |
| 112 |
|
| 113 |
diff = DeepDiff(t1, t2, ignore_order=ignore_order, max_passes=max_passes, |
| 114 |
max_diffs=max_diffs, cache_size=cache_size, cache_tuning_sample_size=100) |
| 115 |
|
| 116 |
stats = diff.get_stats() |
| 117 |
# In this case an optimization happened to compare numbers. Behind the scene we converted the python lists |
| 118 |
# into Numpy arrays and cached the distances between numbers. |
| 119 |
# So the distance cache was heavily used and stayed ON but the level cache was turned off due to low cache hit |
| 120 |
|
| 121 |
expected_stats = { |
| 122 |
'PASSES COUNT': 1, |
| 123 |
'DIFF COUNT': 16, |
| 124 |
'DISTANCE CACHE HIT COUNT': 0, |
| 125 |
'MAX PASS LIMIT REACHED': False, |
| 126 |
'MAX DIFF LIMIT REACHED': False |
| 127 |
} |
| 128 |
assert expected_stats == stats |
| 129 |
|
| 130 |
expected = { |
| 131 |
'values_changed': { |
| 132 |
'root[11]': { |
| 133 |
'new_value': 30, |
| 134 |
'old_value': 12 |
| 135 |
}, |
| 136 |
'root[10]': { |
| 137 |
'new_value': 31, |
| 138 |
'old_value': 11 |
| 139 |
}, |
| 140 |
'root[9]': { |
| 141 |
'new_value': 32, |
| 142 |
'old_value': 10 |
| 143 |
} |
| 144 |
}, |
| 145 |
'iterable_item_removed': { |
| 146 |
'root[0]': 1, |
| 147 |
'root[1]': 2, |
| 148 |
'root[2]': 3, |
| 149 |
'root[3]': 4, |
| 150 |
'root[4]': 5, |
| 151 |
'root[5]': 6, |
| 152 |
'root[6]': 7, |
| 153 |
'root[7]': 8, |
| 154 |
'root[8]': 9 |
| 155 |
} |
| 156 |
} |
| 157 |
assert expected == diff |
| 158 |
|
| 159 |
def test_cache_does_not_affect_final_results(self): |
| 160 |
t1 = [[['b'], 8, 1, 2, 3, 4]] |
| 161 |
t2 = [[['a'], 5, 1, 2, 3, 4]] |
| 162 |
|
| 163 |
diff1 = DeepDiff(t1, t2, ignore_order=True, cache_size=0, |
| 164 |
cache_tuning_sample_size=0, cutoff_intersection_for_pairs=1) |
| 165 |
diff2 = DeepDiff(t1, t2, ignore_order=True, cache_size=5000, |
| 166 |
cache_tuning_sample_size=0, cutoff_intersection_for_pairs=1) |
| 167 |
print(diff1.get_stats()) |
| 168 |
print(diff1) |
| 169 |
assert diff1 == diff2 |