| 1 |
#!/usr/bin/env python |
| 2 |
import pytest |
| 3 |
from deepdiff import DeepDiff |
| 4 |
from deepdiff.helper import pypy3, notpresent |
| 5 |
from deepdiff.model import DictRelationship, NonSubscriptableIterableRelationship |
| 6 |
|
| 7 |
import logging |
| 8 |
logging.disable(logging.CRITICAL) |
| 9 |
|
| 10 |
|
| 11 |
class TestDeepDiffTree: |
| 12 |
"""DeepDiff Tests.""" |
| 13 |
|
| 14 |
def test_same_objects(self): |
| 15 |
t1 = {1: 1, 2: 2, 3: 3} |
| 16 |
t2 = t1 |
| 17 |
ddiff = DeepDiff(t1, t2) |
| 18 |
res = ddiff.tree |
| 19 |
assert res == {} |
| 20 |
|
| 21 |
def test_significant_digits_signed_zero(self): |
| 22 |
t1 = 0.00001 |
| 23 |
t2 = -0.0001 |
| 24 |
ddiff = DeepDiff(t1, t2, significant_digits=2) |
| 25 |
res = ddiff.tree |
| 26 |
assert res == {} |
| 27 |
t1 = 1 * 10**-12 |
| 28 |
t2 = -1 * 10**-12 |
| 29 |
ddiff = DeepDiff(t1, t2, significant_digits=10) |
| 30 |
res = ddiff.tree |
| 31 |
assert res == {} |
| 32 |
|
| 33 |
def test_item_added_extensive(self): |
| 34 |
t1 = {'one': 1, 'two': 2, 'three': 3, 'four': 4} |
| 35 |
t2 = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'new': 1337} |
| 36 |
ddiff = DeepDiff(t1, t2) |
| 37 |
res = ddiff.tree |
| 38 |
(key, ) = res.keys() |
| 39 |
assert key == 'dictionary_item_added' |
| 40 |
assert len(res['dictionary_item_added']) == 1 |
| 41 |
|
| 42 |
(added1, ) = res['dictionary_item_added'] |
| 43 |
|
| 44 |
# assert added1 DiffLevel chain is valid at all |
| 45 |
assert added1.up.down == added1 |
| 46 |
assert added1.down is None |
| 47 |
assert added1.up.up is None |
| 48 |
assert added1.all_up == added1.up |
| 49 |
assert added1.up.all_down == added1 |
| 50 |
assert added1.report_type == 'dictionary_item_added' |
| 51 |
|
| 52 |
# assert DiffLevel chain points to the objects we entered |
| 53 |
assert added1.up.t1 == t1 |
| 54 |
assert added1.up.t2 == t2 |
| 55 |
|
| 56 |
assert added1.t1 is notpresent |
| 57 |
assert added1.t2 == 1337 |
| 58 |
|
| 59 |
# assert DiffLevel child relationships are correct |
| 60 |
assert added1.up.t1_child_rel is None |
| 61 |
assert isinstance(added1.up.t2_child_rel, DictRelationship) |
| 62 |
assert added1.up.t2_child_rel.parent == added1.up.t2 |
| 63 |
assert added1.up.t2_child_rel.child == added1.t2 |
| 64 |
assert added1.up.t2_child_rel.param == 'new' |
| 65 |
|
| 66 |
assert added1.up.path() == "root" |
| 67 |
assert added1.path() == "root['new']" |
| 68 |
|
| 69 |
def test_item_added_and_removed(self): |
| 70 |
t1 = {'one': 1, 'two': 2, 'three': 3, 'four': 4} |
| 71 |
t2 = {'one': 1, 'two': 4, 'three': 3, 'five': 5, 'six': 6} |
| 72 |
ddiff = DeepDiff(t1, t2, view='tree') |
| 73 |
assert set(ddiff.keys()) == { |
| 74 |
'dictionary_item_added', 'dictionary_item_removed', |
| 75 |
'values_changed' |
| 76 |
} |
| 77 |
assert len(ddiff['dictionary_item_added']) == 2 |
| 78 |
assert len(ddiff['dictionary_item_removed']) == 1 |
| 79 |
|
| 80 |
def test_item_added_and_removed2(self): |
| 81 |
t1 = {2: 2, 4: 4} |
| 82 |
t2 = {2: "b", 5: 5} |
| 83 |
ddiff = DeepDiff(t1, t2, view='tree') |
| 84 |
assert set(ddiff.keys()), { |
| 85 |
'dictionary_item_added', 'dictionary_item_removed', |
| 86 |
'type_changes' |
| 87 |
} |
| 88 |
assert len(ddiff['dictionary_item_added']) == 1 |
| 89 |
assert len(ddiff['dictionary_item_removed']) == 1 |
| 90 |
|
| 91 |
def test_non_subscriptable_iterable(self): |
| 92 |
t1 = (i for i in [42, 1337, 31337]) |
| 93 |
t2 = (i for i in [ |
| 94 |
42, |
| 95 |
1337, |
| 96 |
]) |
| 97 |
ddiff = DeepDiff(t1, t2, view='tree') |
| 98 |
(change, ) = ddiff['iterable_item_removed'] |
| 99 |
|
| 100 |
assert set(ddiff.keys()) == {'iterable_item_removed'} |
| 101 |
assert len(ddiff['iterable_item_removed']) == 1 |
| 102 |
|
| 103 |
assert change.up.t1 == t1 |
| 104 |
assert change.up.t2 == t2 |
| 105 |
assert change.report_type == 'iterable_item_removed' |
| 106 |
assert change.t1 == 31337 |
| 107 |
assert change.t2 is notpresent |
| 108 |
|
| 109 |
assert isinstance(change.up.t1_child_rel, |
| 110 |
NonSubscriptableIterableRelationship) |
| 111 |
assert change.up.t2_child_rel is None |
| 112 |
|
| 113 |
def test_non_subscriptable_iterable_path(self): |
| 114 |
t1 = (i for i in [42, 1337, 31337]) |
| 115 |
t2 = (i for i in [42, 1337, ]) |
| 116 |
ddiff = DeepDiff(t1, t2, view='tree') |
| 117 |
(change, ) = ddiff['iterable_item_removed'] |
| 118 |
|
| 119 |
# testing path |
| 120 |
assert change.path() is None |
| 121 |
assert change.path(force='yes') == 'root(unrepresentable)' |
| 122 |
assert change.path(force='fake') == 'root[2]' |
| 123 |
|
| 124 |
def test_report_type_in_iterable(self): |
| 125 |
a = {"temp": ["a"]} |
| 126 |
b = {"temp": ["b"]} |
| 127 |
|
| 128 |
ddiff = DeepDiff(a, b, ignore_order=True, view="tree") |
| 129 |
report_type = ddiff['values_changed'][0].report_type |
| 130 |
assert 'values_changed' == report_type |
| 131 |
|
| 132 |
def test_significant_digits(self): |
| 133 |
ddiff = DeepDiff( |
| 134 |
[0.012, 0.98], |
| 135 |
[0.013, 0.99], |
| 136 |
significant_digits=1, |
| 137 |
view='tree') |
| 138 |
assert ddiff == {} |
| 139 |
|
| 140 |
def test_significant_digits_with_sets(self): |
| 141 |
ddiff = DeepDiff( |
| 142 |
{0.012, 0.98}, |
| 143 |
{0.013, 0.99}, |
| 144 |
significant_digits=1, |
| 145 |
view='tree') |
| 146 |
assert ddiff == {} |
| 147 |
|
| 148 |
def test_significant_digits_with_ignore_order(self): |
| 149 |
ddiff = DeepDiff( |
| 150 |
[0.012, 0.98], [0.013, 0.99], |
| 151 |
significant_digits=1, |
| 152 |
ignore_order=True, |
| 153 |
view='tree') |
| 154 |
assert ddiff == {} |
| 155 |
|
| 156 |
def test_repr(self): |
| 157 |
t1 = {1, 2, 8} |
| 158 |
t2 = {1, 2, 3, 5} |
| 159 |
ddiff = DeepDiff(t1, t2, view='tree') |
| 160 |
str(ddiff) |
| 161 |
|
| 162 |
|
| 163 |
class TestDeepDiffTreeWithNumpy: |
| 164 |
"""DeepDiff Tests with Numpy.""" |
| 165 |
|
| 166 |
@pytest.mark.skipif(pypy3, reason="Numpy is not compatible with pypy3") |
| 167 |
def test_diff_with_numpy(self): |
| 168 |
import numpy as np |
| 169 |
a1 = np.array([1.23, 1.66, 1.98]) |
| 170 |
a2 = np.array([1.23, 1.66, 1.98]) |
| 171 |
d1 = {'np': a1} |
| 172 |
d2 = {'np': a2} |
| 173 |
ddiff = DeepDiff(d1, d2) |
| 174 |
res = ddiff.tree |
| 175 |
assert res == {} |
| 176 |
|
| 177 |
@pytest.mark.skipif(pypy3, reason="Numpy is not compatible with pypy3") |
| 178 |
def test_diff_with_empty_seq(self): |
| 179 |
a1 = {"empty": []} |
| 180 |
a2 = {"empty": []} |
| 181 |
ddiff = DeepDiff(a1, a2) |
| 182 |
assert ddiff == {} |
| 183 |
|
| 184 |
|
| 185 |
class TestDeepAdditions: |
| 186 |
"""Tests for Additions and Subtractions.""" |
| 187 |
|
| 188 |
@pytest.mark.skip(reason="Not currently implemented") |
| 189 |
def test_adding_list_diff(self): |
| 190 |
t1 = [1, 2] |
| 191 |
t2 = [1, 2, 3, 5] |
| 192 |
ddiff = DeepDiff(t1, t2, view='tree') |
| 193 |
addition = ddiff + t1 |
| 194 |
assert addition == t2 |