| 1 |
from deepdiff import DeepDiff |
| 2 |
from deepdiff.helper import COLORED_VIEW, COLORED_COMPACT_VIEW |
| 3 |
from deepdiff.colored_view import RED, GREEN, RESET, ColoredView |
| 4 |
|
| 5 |
|
| 6 |
def test_colored_view_basic(): |
| 7 |
t1 = { |
| 8 |
"name": "John", |
| 9 |
"age": 30, |
| 10 |
"gender": "male", |
| 11 |
"scores": [1, 2, 3], |
| 12 |
"address": { |
| 13 |
"city": "New York", |
| 14 |
"zip": "10001", |
| 15 |
}, |
| 16 |
} |
| 17 |
|
| 18 |
t2 = { |
| 19 |
"name": "John", |
| 20 |
"age": 31, # Changed |
| 21 |
"scores": [1, 2, 4], # Changed |
| 22 |
"address": { |
| 23 |
"city": "Boston", # Changed |
| 24 |
"zip": "10001", |
| 25 |
}, |
| 26 |
"team": "abc", # Added |
| 27 |
} |
| 28 |
|
| 29 |
diff = DeepDiff(t1, t2, view=COLORED_VIEW) |
| 30 |
result = str(diff) |
| 31 |
|
| 32 |
expected = f'''{{ |
| 33 |
"name": "John", |
| 34 |
"age": {RED}30{RESET} -> {GREEN}31{RESET}, |
| 35 |
"scores": [ |
| 36 |
1, |
| 37 |
2, |
| 38 |
{RED}3{RESET} -> {GREEN}4{RESET} |
| 39 |
], |
| 40 |
"address": {{ |
| 41 |
"city": {RED}"New York"{RESET} -> {GREEN}"Boston"{RESET}, |
| 42 |
"zip": "10001" |
| 43 |
}}, |
| 44 |
{GREEN}"team": {GREEN}"abc"{RESET}{RESET}, |
| 45 |
{RED}"gender": {RED}"male"{RESET}{RESET} |
| 46 |
}}''' |
| 47 |
assert result == expected |
| 48 |
|
| 49 |
|
| 50 |
def test_colored_view_nested_changes(): |
| 51 |
t1 = { |
| 52 |
"level1": { |
| 53 |
"level2": { |
| 54 |
"level3": { |
| 55 |
"level4": True |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
t2 = { |
| 62 |
"level1": { |
| 63 |
"level2": { |
| 64 |
"level3": { |
| 65 |
"level4": False |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
diff = DeepDiff(t1, t2, view=COLORED_VIEW) |
| 72 |
result = str(diff) |
| 73 |
|
| 74 |
expected = f'''{{ |
| 75 |
"level1": {{ |
| 76 |
"level2": {{ |
| 77 |
"level3": {{ |
| 78 |
"level4": {RED}true{RESET} -> {GREEN}false{RESET} |
| 79 |
}} |
| 80 |
}} |
| 81 |
}} |
| 82 |
}}''' |
| 83 |
assert result == expected |
| 84 |
|
| 85 |
|
| 86 |
def test_colored_view_list_changes(): |
| 87 |
t1 = [1, 2, 3, 4] |
| 88 |
t2 = [1, 5, 3, 6] |
| 89 |
|
| 90 |
diff = DeepDiff(t1, t2, view=COLORED_VIEW) |
| 91 |
result = str(diff) |
| 92 |
|
| 93 |
expected = f'''[ |
| 94 |
1, |
| 95 |
{RED}2{RESET} -> {GREEN}5{RESET}, |
| 96 |
3, |
| 97 |
{RED}4{RESET} -> {GREEN}6{RESET} |
| 98 |
]''' |
| 99 |
assert result == expected |
| 100 |
|
| 101 |
|
| 102 |
def test_colored_view_list_deletions(): |
| 103 |
t1 = [1, 2, 3, 4, 5, 6] |
| 104 |
t2 = [2, 4] |
| 105 |
|
| 106 |
diff = DeepDiff(t1, t2, view=COLORED_VIEW) |
| 107 |
result = str(diff) |
| 108 |
|
| 109 |
expected = f'''[ |
| 110 |
{RED}1{RESET}, |
| 111 |
2, |
| 112 |
{RED}3{RESET}, |
| 113 |
4, |
| 114 |
{RED}5{RESET}, |
| 115 |
{RED}6{RESET} |
| 116 |
]''' |
| 117 |
assert result == expected |
| 118 |
|
| 119 |
|
| 120 |
def test_colored_view_list_additions(): |
| 121 |
t1 = [2, 4] |
| 122 |
t2 = [1, 2, 3, 4, 5] |
| 123 |
|
| 124 |
diff = DeepDiff(t1, t2, view=COLORED_VIEW) |
| 125 |
result = str(diff) |
| 126 |
|
| 127 |
expected = f'''[ |
| 128 |
{GREEN}1{RESET}, |
| 129 |
2, |
| 130 |
{GREEN}3{RESET}, |
| 131 |
4, |
| 132 |
{GREEN}5{RESET} |
| 133 |
]''' |
| 134 |
assert result == expected |
| 135 |
|
| 136 |
|
| 137 |
def test_colored_view_list_all_items_removed(): |
| 138 |
"""Test that all removed items are displayed when list becomes empty.""" |
| 139 |
t1 = [2, 4] |
| 140 |
t2 = [] |
| 141 |
|
| 142 |
diff = DeepDiff(t1, t2, view=COLORED_VIEW) |
| 143 |
result = str(diff) |
| 144 |
|
| 145 |
expected = f'''[ |
| 146 |
{RED}2{RESET}, |
| 147 |
{RED}4{RESET} |
| 148 |
]''' |
| 149 |
assert result == expected |
| 150 |
|
| 151 |
|
| 152 |
def test_colored_compact_view_list_all_items_removed(): |
| 153 |
"""Test that all removed items are displayed when list becomes empty with compact view.""" |
| 154 |
t1 = [2, 4] |
| 155 |
t2 = [] |
| 156 |
|
| 157 |
diff = DeepDiff(t1, t2, view=COLORED_COMPACT_VIEW) |
| 158 |
result = str(diff) |
| 159 |
|
| 160 |
expected = f'''[ |
| 161 |
{RED}2{RESET}, |
| 162 |
{RED}4{RESET} |
| 163 |
]''' |
| 164 |
assert result == expected |
| 165 |
|
| 166 |
|
| 167 |
def test_colored_view_list_changes_deletions(): |
| 168 |
t1 = [1, 5, 7, 3, 6] |
| 169 |
t2 = [1, 2, 3, 4] |
| 170 |
|
| 171 |
diff = DeepDiff(t1, t2, view=COLORED_VIEW) |
| 172 |
result = str(diff) |
| 173 |
|
| 174 |
expected = f'''[ |
| 175 |
1, |
| 176 |
{RED}5{RESET} -> {GREEN}2{RESET}, |
| 177 |
{RED}7{RESET}, |
| 178 |
3, |
| 179 |
{RED}6{RESET} -> {GREEN}4{RESET} |
| 180 |
]''' |
| 181 |
assert result == expected |
| 182 |
|
| 183 |
|
| 184 |
def test_colored_view_list_changes_additions(): |
| 185 |
t1 = [1, 2, 3, 4] |
| 186 |
t2 = [1, 5, 7, 3, 6] |
| 187 |
|
| 188 |
diff = DeepDiff(t1, t2, view=COLORED_VIEW) |
| 189 |
result = str(diff) |
| 190 |
|
| 191 |
expected = f'''[ |
| 192 |
1, |
| 193 |
{RED}2{RESET} -> {GREEN}5{RESET}, |
| 194 |
{GREEN}7{RESET}, |
| 195 |
3, |
| 196 |
{RED}4{RESET} -> {GREEN}6{RESET} |
| 197 |
]''' |
| 198 |
assert result == expected |
| 199 |
|
| 200 |
|
| 201 |
def test_colored_view_list_no_changes_with_ignore_order(): |
| 202 |
t1 = [1, 2, 3] |
| 203 |
t2 = [3, 2, 1] |
| 204 |
|
| 205 |
diff = DeepDiff(t1, t2, view=COLORED_VIEW, ignore_order=True) |
| 206 |
result = str(diff) |
| 207 |
|
| 208 |
expected = '''[ |
| 209 |
3, |
| 210 |
2, |
| 211 |
1 |
| 212 |
]''' |
| 213 |
assert result == expected |
| 214 |
|
| 215 |
|
| 216 |
def test_colored_view_list_with_ignore_order(): |
| 217 |
t1 = { |
| 218 |
"hobbies": [ |
| 219 |
"reading", |
| 220 |
"hiking" |
| 221 |
] |
| 222 |
} |
| 223 |
|
| 224 |
t2 = { |
| 225 |
"hobbies": [ |
| 226 |
"hiking", |
| 227 |
"painting", |
| 228 |
"coding" |
| 229 |
] |
| 230 |
} |
| 231 |
|
| 232 |
diff = DeepDiff(t1, t2, view=COLORED_VIEW, ignore_order=True) |
| 233 |
result = str(diff) |
| 234 |
|
| 235 |
expected = f'''{{ |
| 236 |
"hobbies": [ |
| 237 |
{RED}"reading"{RESET}, |
| 238 |
"hiking", |
| 239 |
{GREEN}"painting"{RESET}, |
| 240 |
{GREEN}"coding"{RESET} |
| 241 |
] |
| 242 |
}}''' |
| 243 |
assert result == expected |
| 244 |
|
| 245 |
|
| 246 |
def test_colored_view_no_changes(): |
| 247 |
t1 = {"a": 1, "b": 2} |
| 248 |
t2 = {"a": 1, "b": 2} |
| 249 |
|
| 250 |
diff = DeepDiff(t1, t2, view=COLORED_VIEW) |
| 251 |
result = str(diff) |
| 252 |
|
| 253 |
expected = '''{ |
| 254 |
"a": 1, |
| 255 |
"b": 2 |
| 256 |
}''' |
| 257 |
assert result == expected |
| 258 |
|
| 259 |
|
| 260 |
def test_compact_view_basic(): |
| 261 |
t1 = { |
| 262 |
"name": "John", |
| 263 |
"age": 30, |
| 264 |
"gender": "male", |
| 265 |
"scores": [1, 2, 3], |
| 266 |
"address": { |
| 267 |
"city": "New York", |
| 268 |
"zip": "10001", |
| 269 |
"details": { |
| 270 |
"type": "apartment", |
| 271 |
"floor": 5 |
| 272 |
} |
| 273 |
}, |
| 274 |
"hobbies": ["reading", {"sport": "tennis", "level": "advanced"}] |
| 275 |
} |
| 276 |
|
| 277 |
t2 = { |
| 278 |
"name": "John", |
| 279 |
"age": 31, # Changed |
| 280 |
"scores": [1, 2, 4], # Changed |
| 281 |
"address": { |
| 282 |
"city": "Boston", # Changed |
| 283 |
"zip": "10001", |
| 284 |
"details": { |
| 285 |
"type": "apartment", |
| 286 |
"floor": 5 |
| 287 |
} |
| 288 |
}, |
| 289 |
"team": "abc", # Added |
| 290 |
"hobbies": ["reading", {"sport": "tennis", "level": "advanced"}] |
| 291 |
} |
| 292 |
|
| 293 |
diff = DeepDiff(t1, t2, view=COLORED_COMPACT_VIEW) |
| 294 |
result = str(diff) |
| 295 |
|
| 296 |
expected = f'''{{ |
| 297 |
"name": "John", |
| 298 |
"age": {RED}30{RESET} -> {GREEN}31{RESET}, |
| 299 |
"scores": [ |
| 300 |
1, |
| 301 |
2, |
| 302 |
{RED}3{RESET} -> {GREEN}4{RESET} |
| 303 |
], |
| 304 |
"address": {{ |
| 305 |
"city": {RED}"New York"{RESET} -> {GREEN}"Boston"{RESET}, |
| 306 |
"zip": "10001", |
| 307 |
"details": {{...}} |
| 308 |
}}, |
| 309 |
{GREEN}"team": {GREEN}"abc"{RESET}{RESET}, |
| 310 |
"hobbies": [...], |
| 311 |
{RED}"gender": {RED}"male"{RESET}{RESET} |
| 312 |
}}''' |
| 313 |
assert result == expected |
| 314 |
|
| 315 |
|
| 316 |
def test_compact_view_nested_changes(): |
| 317 |
t1 = { |
| 318 |
"level1": { |
| 319 |
"unchanged1": { |
| 320 |
"deep1": True, |
| 321 |
"deep2": [1, 2, 3] |
| 322 |
}, |
| 323 |
"level2": { |
| 324 |
"a": 1, |
| 325 |
"b": "test", |
| 326 |
"c": [1, 2, 3], |
| 327 |
"d": {"x": 1, "y": 2} |
| 328 |
}, |
| 329 |
"unchanged2": [1, 2, {"a": 1}] |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
t2 = { |
| 334 |
"level1": { |
| 335 |
"unchanged1": { |
| 336 |
"deep1": True, |
| 337 |
"deep2": [1, 2, 3] |
| 338 |
}, |
| 339 |
"level2": { |
| 340 |
"a": 2, # Changed |
| 341 |
"b": "test", |
| 342 |
"c": [1, 2, 4], # Changed |
| 343 |
"d": {"x": 1, "y": 3} # Changed |
| 344 |
}, |
| 345 |
"unchanged2": [1, 2, {"a": 1}] |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
diff = DeepDiff(t1, t2, view=COLORED_COMPACT_VIEW) |
| 350 |
result = str(diff) |
| 351 |
|
| 352 |
expected = f'''{{ |
| 353 |
"level1": {{ |
| 354 |
"unchanged1": {{...}}, |
| 355 |
"level2": {{ |
| 356 |
"a": {RED}1{RESET} -> {GREEN}2{RESET}, |
| 357 |
"b": "test", |
| 358 |
"c": [ |
| 359 |
1, |
| 360 |
2, |
| 361 |
{RED}3{RESET} -> {GREEN}4{RESET} |
| 362 |
], |
| 363 |
"d": {{ |
| 364 |
"x": 1, |
| 365 |
"y": {RED}2{RESET} -> {GREEN}3{RESET} |
| 366 |
}} |
| 367 |
}}, |
| 368 |
"unchanged2": [...] |
| 369 |
}} |
| 370 |
}}''' |
| 371 |
assert result == expected |
| 372 |
|
| 373 |
|
| 374 |
def test_compact_view_no_changes(): |
| 375 |
# Test with dict |
| 376 |
t1 = {"a": 1, "b": [1, 2], "c": {"x": True}} |
| 377 |
t2 = {"a": 1, "b": [1, 2], "c": {"x": True}} |
| 378 |
diff = DeepDiff(t1, t2, view=COLORED_COMPACT_VIEW) |
| 379 |
assert str(diff) == "{...}" |
| 380 |
|
| 381 |
# Test with list |
| 382 |
t1 = [1, {"a": 1}, [1, 2]] |
| 383 |
t2 = [1, {"a": 1}, [1, 2]] |
| 384 |
diff = DeepDiff(t1, t2, view=COLORED_COMPACT_VIEW) |
| 385 |
assert str(diff) == "[...]" |
| 386 |
|
| 387 |
|
| 388 |
def test_compact_view_list_changes(): |
| 389 |
t1 = [1, {"a": 1, "b": {"x": 1, "y": 2}}, [1, 2, {"z": 3}]] |
| 390 |
t2 = [1, {"a": 2, "b": {"x": 1, "y": 2}}, [1, 2, {"z": 3}]] |
| 391 |
|
| 392 |
diff = DeepDiff(t1, t2, view=COLORED_COMPACT_VIEW) |
| 393 |
result = str(diff) |
| 394 |
|
| 395 |
expected = f'''[ |
| 396 |
1, |
| 397 |
{{ |
| 398 |
"a": {RED}1{RESET} -> {GREEN}2{RESET}, |
| 399 |
"b": {{...}} |
| 400 |
}}, |
| 401 |
[...] |
| 402 |
]''' |
| 403 |
assert result == expected |
| 404 |
|
| 405 |
|
| 406 |
def test_compact_view_primitive_siblings(): |
| 407 |
t1 = { |
| 408 |
"changed": 1, |
| 409 |
"str_sibling": "hello", |
| 410 |
"int_sibling": 42, |
| 411 |
"bool_sibling": True, |
| 412 |
"nested_sibling": {"a": 1, "b": 2} |
| 413 |
} |
| 414 |
|
| 415 |
t2 = { |
| 416 |
"changed": 2, |
| 417 |
"str_sibling": "hello", |
| 418 |
"int_sibling": 42, |
| 419 |
"bool_sibling": True, |
| 420 |
"nested_sibling": {"a": 1, "b": 2} |
| 421 |
} |
| 422 |
|
| 423 |
diff = DeepDiff(t1, t2, view=COLORED_COMPACT_VIEW) |
| 424 |
result = str(diff) |
| 425 |
|
| 426 |
expected = f'''{{ |
| 427 |
"changed": {RED}1{RESET} -> {GREEN}2{RESET}, |
| 428 |
"str_sibling": "hello", |
| 429 |
"int_sibling": 42, |
| 430 |
"bool_sibling": true, |
| 431 |
"nested_sibling": {{...}} |
| 432 |
}}''' |
| 433 |
assert result == expected |
| 434 |
|
| 435 |
|
| 436 |
def test_colored_view_bool_evaluation(): |
| 437 |
# Test COLORED_VIEW |
| 438 |
# Scenario 1: No differences |
| 439 |
t1_no_diff = {"a": 1, "b": 2} |
| 440 |
t2_no_diff = {"a": 1, "b": 2} |
| 441 |
diff_no_diff_colored = DeepDiff(t1_no_diff, t2_no_diff, view=COLORED_VIEW) |
| 442 |
assert not bool(diff_no_diff_colored), "bool(diff) should be False when no diffs (colored view)" |
| 443 |
|
| 444 |
# Scenario 2: With differences |
| 445 |
t1_with_diff = {"a": 1, "b": 2} |
| 446 |
t2_with_diff = {"a": 1, "b": 3} |
| 447 |
diff_with_diff_colored = DeepDiff(t1_with_diff, t2_with_diff, view=COLORED_VIEW) |
| 448 |
assert bool(diff_with_diff_colored), "bool(diff) should be True when diffs exist (colored view)" |
| 449 |
|
| 450 |
# Test COLORED_COMPACT_VIEW |
| 451 |
# Scenario 1: No differences |
| 452 |
diff_no_diff_compact = DeepDiff(t1_no_diff, t2_no_diff, view=COLORED_COMPACT_VIEW) |
| 453 |
assert not bool(diff_no_diff_compact), "bool(diff) should be False when no diffs (compact view)" |
| 454 |
|
| 455 |
# Scenario 2: With differences |
| 456 |
diff_with_diff_compact = DeepDiff(t1_with_diff, t2_with_diff, view=COLORED_COMPACT_VIEW) |
| 457 |
assert bool(diff_with_diff_compact), "bool(diff) should be True when diffs exist (compact view)" |
| 458 |
|
| 459 |
|
| 460 |
def test_colored_view_with_empty_list_shows_removals(): |
| 461 |
""" |
| 462 |
Tests ColoredView correctly shows about an empty list. |
| 463 |
This covers the bug where it would just show '[]'. |
| 464 |
""" |
| 465 |
t1 = [1, 2, 3] |
| 466 |
t2 = [] |
| 467 |
ddiff = DeepDiff(t1, t2) |
| 468 |
view = ColoredView(t2, ddiff.tree) |
| 469 |
result = str(view) |
| 470 |
|
| 471 |
# The output should contain the removed items, colored in red. |
| 472 |
assert f"{RED}1{RESET}" in result |
| 473 |
assert f"{RED}2{RESET}" in result |
| 474 |
assert f"{RED}3{RESET}" in result |
| 475 |
assert result.strip().startswith('[') |
| 476 |
assert result.strip().endswith(']') |
| 477 |
assert result != '[]' |
| 478 |
|
| 479 |
|
| 480 |
def test_colored_view_with_empty_dict_shows_removals(): |
| 481 |
""" |
| 482 |
Tests ColoredView correctly shows about an empty dict. |
| 483 |
This covers the bug where it would just show '{}'. |
| 484 |
""" |
| 485 |
t1 = {'a': 1, 'b': 2} |
| 486 |
t2 = {} |
| 487 |
ddiff = DeepDiff(t1, t2) |
| 488 |
view = ColoredView(t2, ddiff.tree) |
| 489 |
result = str(view) |
| 490 |
|
| 491 |
assert f'{RED}{{"a": 1' in result |
| 492 |
assert f'"b": 2}}{RESET}' in result |
| 493 |
assert result.strip().startswith(RED) |
| 494 |
assert result.strip().endswith(RESET) |
| 495 |
assert result != '{}' |