Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for UUIDs #280

Merged
merged 2 commits into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ Authors in order of the timeline of their contributions:
- Sun Ao [eggachecat](https://github.com/eggachecat) for adding custom operators.
- Sun Ao [eggachecat](https://github.com/eggachecat) for adding ignore_order_func.
- [SlavaSkvortsov](https://github.com/SlavaSkvortsov) for fixing unprocessed key error.
- [havardthom](https://github.com/havardthom) for adding UUID support.
10 changes: 9 additions & 1 deletion deepdiff/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from collections import defaultdict
from itertools import zip_longest
from ordered_set import OrderedSet
from deepdiff.helper import (strings, bytes_type, numbers, times, ListItemRemovedOrAdded, notpresent,
from deepdiff.helper import (strings, bytes_type, numbers, uuids, times, ListItemRemovedOrAdded, notpresent,
IndexedHash, unprocessed, add_to_frozen_set,
convert_item_or_items_into_set_else_none, get_type,
convert_item_or_items_into_compiled_regexes_else_none,
Expand Down Expand Up @@ -1148,6 +1148,11 @@ def _diff_datetimes(self, level):
if level.t1 != level.t2:
self._report_result('values_changed', level)

def _diff_uuids(self, level):
"""Diff UUIDs"""
if level.t1.int != level.t2.int:
self._report_result('values_changed', level)

def _diff_numpy_array(self, level, parents_ids=frozenset()):
"""Diff numpy arrays"""
if level.path() not in self._numpy_paths:
Expand Down Expand Up @@ -1318,6 +1323,9 @@ def _diff(self, level, parents_ids=frozenset(), _original_type=None):
elif isinstance(level.t1, times):
self._diff_datetimes(level)

elif isinstance(level.t1, uuids):
self._diff_uuids(level)

elif isinstance(level.t1, numbers):
self._diff_numbers(level)

Expand Down
2 changes: 2 additions & 0 deletions deepdiff/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import re
import os
import datetime
import uuid
import logging
import warnings
import time
Expand Down Expand Up @@ -126,6 +127,7 @@ def copy(self): # pragma: no cover. Only used in pypy3 and py3.5
bytes_type = bytes
only_numbers = (int, float, complex, Decimal) + numpy_numbers
datetimes = (datetime.datetime, datetime.date, datetime.timedelta, datetime.time)
uuids = (uuid.UUID)
times = (datetime.datetime, datetime.time)
numbers = only_numbers + datetimes
booleans = (bool, np_bool_)
Expand Down
69 changes: 69 additions & 0 deletions tests/test_diff_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import datetime
import pytest
import logging
import uuid
from decimal import Decimal
from deepdiff import DeepDiff
from deepdiff.helper import pypy3
Expand Down Expand Up @@ -157,6 +158,74 @@ def test_diffs_times(self):
assert DeepDiff(t1, t2) == expected_result
assert DeepDiff(t1, t3) == {}

def test_diffs_uuid1(self):
t1 = uuid.uuid1()
t2 = uuid.uuid1()
ddiff = DeepDiff(t1, t2)
result = {
'values_changed': {
'root': {
'new_value': t2,
'old_value': t1
}
}
}
assert result == ddiff
ddiff = DeepDiff(t1, t1)
result = {}
assert result == ddiff

def test_diffs_uuid3(self):
t1 = uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org')
t2 = uuid.uuid3(uuid.NAMESPACE_DNS, 'stackoverflow.com')
ddiff = DeepDiff(t1, t2)
result = {
'values_changed': {
'root': {
'new_value': t2,
'old_value': t1
}
}
}
assert result == ddiff
ddiff = DeepDiff(t1, t1)
result = {}
assert result == ddiff

def test_diffs_uuid4(self):
t1 = uuid.uuid4()
t2 = uuid.uuid4()
ddiff = DeepDiff(t1, t2)
result = {
'values_changed': {
'root': {
'new_value': t2,
'old_value': t1
}
}
}
assert result == ddiff
ddiff = DeepDiff(t1, t1)
result = {}
assert result == ddiff

def test_diffs_uuid5(self):
t1 = uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org')
t2 = uuid.uuid5(uuid.NAMESPACE_DNS, 'stackoverflow.com')
ddiff = DeepDiff(t1, t2)
result = {
'values_changed': {
'root': {
'new_value': t2,
'old_value': t1
}
}
}
assert result == ddiff
ddiff = DeepDiff(t1, t1)
result = {}
assert result == ddiff

def test_string_difference(self):
t1 = {1: 1, 2: 2, 3: 3, 4: {"a": "hello", "b": "world"}}
t2 = {1: 1, 2: 4, 3: 3, 4: {"a": "hello", "b": "world!"}}
Expand Down