| 1 |
#!/usr/bin/env python |
| 2 |
import datetime |
| 3 |
import logging |
| 4 |
import pytest |
| 5 |
from tests import CustomClass, CustomClassMisleadingRepr |
| 6 |
from deepdiff import DeepDiff |
| 7 |
from deepdiff.model import (DiffLevel, ChildRelationship, DictRelationship, |
| 8 |
SubscriptableIterableRelationship, |
| 9 |
AttributeRelationship) |
| 10 |
|
| 11 |
logging.disable(logging.CRITICAL) |
| 12 |
|
| 13 |
|
| 14 |
class WorkingChildRelationship(ChildRelationship): |
| 15 |
pass |
| 16 |
|
| 17 |
|
| 18 |
class TestDictRelationship: |
| 19 |
def setup_class(cls): |
| 20 |
cls.customkey = CustomClass(a=13, b=37) |
| 21 |
cls.customkey_misleading = CustomClassMisleadingRepr(a=11, b=20) |
| 22 |
cls.d = { |
| 23 |
42: 'answer', |
| 24 |
'vegan': 'for life', |
| 25 |
cls.customkey: 1337, |
| 26 |
cls.customkey_misleading: 'banana' |
| 27 |
} |
| 28 |
|
| 29 |
def test_numkey(self): |
| 30 |
rel = DictRelationship(parent=self.d, child=self.d[42], param=42) |
| 31 |
assert rel.get_param_repr() == "[42]" |
| 32 |
|
| 33 |
def test_strkey(self): |
| 34 |
rel = ChildRelationship.create( |
| 35 |
klass=DictRelationship, |
| 36 |
parent=self.d, |
| 37 |
child=self.d['vegan'], |
| 38 |
param='vegan') |
| 39 |
result = rel.get_param_repr() |
| 40 |
assert result == "['vegan']" |
| 41 |
|
| 42 |
def test_objkey(self): |
| 43 |
rel = DictRelationship( |
| 44 |
parent=self.d, child=self.d[self.customkey], param=self.customkey) |
| 45 |
assert rel.get_param_repr() is None |
| 46 |
|
| 47 |
def test_objkey_misleading_repr(self): |
| 48 |
rel = DictRelationship( |
| 49 |
parent=self.d, |
| 50 |
child=self.d[self.customkey_misleading], |
| 51 |
param=self.customkey_misleading) |
| 52 |
assert rel.get_param_repr() is None |
| 53 |
|
| 54 |
|
| 55 |
class TestListRelationship: |
| 56 |
def setup_class(cls): |
| 57 |
cls.custom = CustomClass(13, 37) |
| 58 |
cls.l = [1337, 'vegan', cls.custom] |
| 59 |
|
| 60 |
def test_min(self): |
| 61 |
rel = SubscriptableIterableRelationship(self.l, self.l[0], 0) |
| 62 |
result = rel.get_param_repr() |
| 63 |
assert result == "[0]" |
| 64 |
|
| 65 |
def test_max(self): |
| 66 |
rel = ChildRelationship.create(SubscriptableIterableRelationship, |
| 67 |
self.l, self.custom, 2) |
| 68 |
assert rel.get_param_repr() == "[2]" |
| 69 |
|
| 70 |
|
| 71 |
class TestAttributeRelationship: |
| 72 |
def setup_class(cls): |
| 73 |
cls.custom = CustomClass(13, 37) |
| 74 |
|
| 75 |
def test_a(self): |
| 76 |
rel = AttributeRelationship(self.custom, 13, "a") |
| 77 |
result = rel.get_param_repr() |
| 78 |
assert result == ".a" |
| 79 |
|
| 80 |
|
| 81 |
class TestDiffLevel: |
| 82 |
def setup_class(cls): |
| 83 |
# Test data |
| 84 |
cls.custom1 = CustomClass(a='very long text here, much longer than you can ever imagine. The longest text here.', b=37) |
| 85 |
cls.custom2 = CustomClass(a=313, b=37) |
| 86 |
cls.t1 = {42: 'answer', 'vegan': 'for life', 1337: cls.custom1} |
| 87 |
cls.t2 = { |
| 88 |
42: 'answer', |
| 89 |
'vegan': 'for the animals', |
| 90 |
1337: cls.custom2 |
| 91 |
} |
| 92 |
|
| 93 |
# Manually build diff, bottom up |
| 94 |
cls.lowest = DiffLevel( |
| 95 |
cls.custom1.a, cls.custom2.a, report_type='values_changed') |
| 96 |
|
| 97 |
# Test manual child relationship |
| 98 |
rel_int_low_t1 = AttributeRelationship( |
| 99 |
parent=cls.custom1, child=cls.custom1.a, param="a") |
| 100 |
rel_int_low_t2 = AttributeRelationship( |
| 101 |
parent=cls.custom2, child=cls.custom2.a, param="a") |
| 102 |
cls.intermediate = DiffLevel( |
| 103 |
cls.custom1, |
| 104 |
cls.custom2, |
| 105 |
down=cls.lowest, |
| 106 |
child_rel1=rel_int_low_t1, |
| 107 |
child_rel2=rel_int_low_t2) |
| 108 |
cls.lowest.up = cls.intermediate |
| 109 |
|
| 110 |
# Test automatic child relationship |
| 111 |
t1_child_rel = ChildRelationship.create( |
| 112 |
klass=DictRelationship, |
| 113 |
parent=cls.t1, |
| 114 |
child=cls.intermediate.t1, |
| 115 |
param=1337) |
| 116 |
t2_child_rel = ChildRelationship.create( |
| 117 |
klass=DictRelationship, |
| 118 |
parent=cls.t2, |
| 119 |
child=cls.intermediate.t2, |
| 120 |
param=1337) |
| 121 |
cls.highest = DiffLevel( |
| 122 |
cls.t1, |
| 123 |
cls.t2, |
| 124 |
down=cls.intermediate, |
| 125 |
child_rel1=t1_child_rel, |
| 126 |
child_rel2=t2_child_rel) |
| 127 |
cls.intermediate.up = cls.highest |
| 128 |
|
| 129 |
def test_all_up(self): |
| 130 |
assert self.lowest.all_up == self.highest |
| 131 |
|
| 132 |
def test_all_down(self): |
| 133 |
assert self.highest.all_down == self.lowest |
| 134 |
|
| 135 |
def test_automatic_child_rel(self): |
| 136 |
assert isinstance(self.highest.t1_child_rel, DictRelationship) |
| 137 |
assert isinstance(self.highest.t2_child_rel, DictRelationship) |
| 138 |
|
| 139 |
assert self.highest.t1_child_rel.parent == self.highest.t1 |
| 140 |
assert self.highest.t2_child_rel.parent == self.highest.t2 |
| 141 |
assert self.highest.t1_child_rel.parent == self.highest.t1 |
| 142 |
assert self.highest.t2_child_rel.parent == self.highest.t2 |
| 143 |
|
| 144 |
# Provides textual relationship from t1 to t1[1337] |
| 145 |
assert '[1337]' == self.highest.t2_child_rel.get_param_repr() |
| 146 |
|
| 147 |
def test_path(self): |
| 148 |
# Provides textual path all the way through |
| 149 |
assert self.lowest.path("self.t1") == "self.t1[1337].a" |
| 150 |
|
| 151 |
def test_path_output_list(self): |
| 152 |
# Provides textual path all the way through |
| 153 |
assert self.lowest.path(output_format="list") == [1337, 'a'] |
| 154 |
|
| 155 |
def test_change_of_path_root(self): |
| 156 |
assert self.lowest.path("root") == "root[1337].a" |
| 157 |
assert self.lowest.path("") == "[1337].a" |
| 158 |
|
| 159 |
def test_path_when_both_children_empty(self): |
| 160 |
""" |
| 161 |
This is a situation that should never happen. |
| 162 |
But we are creating it artificially. |
| 163 |
""" |
| 164 |
t1 = {1: 1} |
| 165 |
t2 = {1: 2} |
| 166 |
child_t1 = {} |
| 167 |
child_t2 = {} |
| 168 |
up = DiffLevel(t1, t2) |
| 169 |
down = up.down = DiffLevel(child_t1, child_t2) |
| 170 |
path = down.path() |
| 171 |
assert path == 'root' |
| 172 |
assert down.path(output_format='list') == [] |
| 173 |
|
| 174 |
def test_t2_path_when_nested(self): |
| 175 |
t1 = { |
| 176 |
"type": "struct", |
| 177 |
"fields": [ |
| 178 |
{"name": "Competition", "metadata": {}, "nullable": True, "type": "string"}, |
| 179 |
{"name": "TeamName", "metadata": {}, "nullable": True, "type": "string"}, |
| 180 |
{ |
| 181 |
"name": "Contents", |
| 182 |
"metadata": {}, |
| 183 |
"nullable": True, |
| 184 |
"type": { |
| 185 |
"type": "struct", |
| 186 |
"fields": [ |
| 187 |
{"name": "Date", "metadata": {}, "nullable": True, "type": "string"}, |
| 188 |
{"name": "Player1", "metadata": {}, "nullable": True, "type": "string"} |
| 189 |
] |
| 190 |
} |
| 191 |
} |
| 192 |
] |
| 193 |
} |
| 194 |
|
| 195 |
t2 = { |
| 196 |
"type": "struct", |
| 197 |
"fields": [ |
| 198 |
{"name": "Competition", "metadata": {}, "nullable": True, "type": "string"}, |
| 199 |
{"name": "GlobalId", "metadata": {}, "nullable": True, "type": "string"}, |
| 200 |
{"name": "TeamName", "metadata": {}, "nullable": True, "type": "string"}, |
| 201 |
{ |
| 202 |
"name": "Contents", |
| 203 |
"metadata": {}, |
| 204 |
"nullable": True, |
| 205 |
"type": { |
| 206 |
"type": "struct", |
| 207 |
"fields": [ |
| 208 |
{"name": "Date", "metadata": {}, "nullable": True, "type": "string"}, |
| 209 |
{"name": "Player1", "metadata": {}, "nullable": True, "type": "string"}, |
| 210 |
{"name": "Player2", "metadata": {}, "nullable": True, "type": "string"} |
| 211 |
] |
| 212 |
} |
| 213 |
} |
| 214 |
] |
| 215 |
} |
| 216 |
|
| 217 |
diff = DeepDiff(t1=t1, t2=t2, ignore_order=True, verbose_level=2, view='tree') |
| 218 |
|
| 219 |
expected_diff = { |
| 220 |
"iterable_item_added": { |
| 221 |
"root['fields'][1]": { |
| 222 |
"name": "GlobalId", |
| 223 |
"metadata": {}, |
| 224 |
"nullable": True, |
| 225 |
"type": "string", |
| 226 |
}, |
| 227 |
"root['fields'][2]['type']['fields'][2]": { |
| 228 |
"name": "Player2", |
| 229 |
"metadata": {}, |
| 230 |
"nullable": True, |
| 231 |
"type": "string", |
| 232 |
}, |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
path = diff['iterable_item_added'][1].path() |
| 237 |
assert "root['fields'][2]['type']['fields'][2]" == path |
| 238 |
|
| 239 |
path_t2 = diff['iterable_item_added'][1].path(use_t2=True) |
| 240 |
assert "root['fields'][3]['type']['fields'][2]" == path_t2 |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
def test_repr_short(self): |
| 245 |
level = self.lowest.verbose_level |
| 246 |
try: |
| 247 |
self.lowest.verbose_level = 0 |
| 248 |
item_repr = repr(self.lowest) |
| 249 |
finally: |
| 250 |
self.lowest.verbose_level = level |
| 251 |
assert item_repr == '<root[1337].a>' |
| 252 |
|
| 253 |
def test_repr_long(self): |
| 254 |
level = self.lowest.verbose_level |
| 255 |
try: |
| 256 |
self.lowest.verbose_level = 1 |
| 257 |
item_repr = repr(self.lowest) |
| 258 |
finally: |
| 259 |
self.lowest.verbose_level = level |
| 260 |
assert item_repr == '<root[1337].a t1:"very long text here, much long...e.", t2:313>' |
| 261 |
|
| 262 |
def test_repr_very_long(self): |
| 263 |
level = self.lowest.verbose_level |
| 264 |
try: |
| 265 |
self.lowest.verbose_level = 2 |
| 266 |
item_repr = repr(self.lowest) |
| 267 |
finally: |
| 268 |
self.lowest.verbose_level = level |
| 269 |
assert item_repr == '<root[1337].a t1:"very long text here, much long...e.", t2:313>' |
| 270 |
|
| 271 |
def test_repetition_attribute_and_repr(self): |
| 272 |
t1 = [1, 1] |
| 273 |
t2 = [1] |
| 274 |
some_repetition = 'some repetition' |
| 275 |
node = DiffLevel(t1, t2) |
| 276 |
node.additional['repetition'] = some_repetition |
| 277 |
assert node.repetition == some_repetition |
| 278 |
assert repr(node) == '<root {"repetition":"some repetition"}>' |
| 279 |
|
| 280 |
|
| 281 |
class TestChildRelationship: |
| 282 |
def test_create_invalid_klass(self): |
| 283 |
with pytest.raises(TypeError): |
| 284 |
ChildRelationship.create(DiffLevel, "hello", 42) |
| 285 |
|
| 286 |
def test_rel_repr_short(self): |
| 287 |
rel = WorkingChildRelationship(parent="that parent", child="this child", param="some param") |
| 288 |
rel_repr = repr(rel) |
| 289 |
expected = '<WorkingChildRelationship parent:"that parent", child:"this child", param:"some param">' |
| 290 |
assert rel_repr == expected |
| 291 |
|
| 292 |
def test_rel_repr_long(self): |
| 293 |
rel = WorkingChildRelationship( |
| 294 |
parent="that parent who has a long path, still going on. Yes, a very long path indeed.", |
| 295 |
child="this child", |
| 296 |
param="some param") |
| 297 |
rel_repr = repr(rel) |
| 298 |
expected = '<WorkingChildRelationship parent:"that parent who has a long pat...d.", child:"this child", param:"some param">' |
| 299 |
assert rel_repr == expected |