Skip to content

Commit

Permalink
Fix identification of datetimes
Browse files Browse the repository at this point in the history
  • Loading branch information
amsb committed Jul 5, 2023
1 parent 75e1edd commit a08ba65
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
4 changes: 2 additions & 2 deletions deepdiff/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from collections import defaultdict
from itertools import zip_longest
from ordered_set import OrderedSet
from deepdiff.helper import (strings, bytes_type, numbers, uuids, times, ListItemRemovedOrAdded, notpresent,
from deepdiff.helper import (strings, bytes_type, numbers, uuids, datetimes, ListItemRemovedOrAdded, notpresent,
IndexedHash, unprocessed, add_to_frozen_set, basic_types,
convert_item_or_items_into_set_else_none, get_type,
convert_item_or_items_into_compiled_regexes_else_none,
Expand Down Expand Up @@ -1529,7 +1529,7 @@ def _diff(self, level, parents_ids=frozenset(), _original_type=None, local_tree=
if isinstance(level.t1, strings):
self._diff_str(level, local_tree=local_tree)

elif isinstance(level.t1, times):
elif isinstance(level.t1, datetimes):
self._diff_datetimes(level, local_tree=local_tree)

elif isinstance(level.t1, uuids):
Expand Down
75 changes: 75 additions & 0 deletions tests/test_diff_datetime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from datetime import date, datetime, time
from deepdiff import DeepDiff


class TestDiffDatetime:
def test_datetime_diff(self):
"""Testing for the correct setting and usage of epsilon."""
d1 = {"a": datetime(2023, 7, 5, 10, 11, 12)}
d2 = {"a": datetime(2023, 7, 5, 10, 11, 12)}
res = DeepDiff(d1, d2)
assert res == {}

res = DeepDiff(d1, d2, ignore_numeric_type_changes=True)
assert res == {}

d1 = {"a": datetime(2023, 7, 5, 10, 11, 12)}
d2 = {"a": datetime(2023, 7, 5, 11, 11, 12)}
res = DeepDiff(d1, d2)
expected = {
"values_changed": {
"root['a']": {
"new_value": datetime(2023, 7, 5, 11, 11, 12),
"old_value": datetime(2023, 7, 5, 10, 11, 12),
}
}
}
assert res == expected


def test_date_diff(self):
"""Testing for the correct setting and usage of epsilon."""
d1 = {"a": date(2023, 7, 5)}
d2 = {"a": date(2023, 7, 5)}
res = DeepDiff(d1, d2)
assert res == {}

# this usage failed in version >=6.0, <=6.3.0
res = DeepDiff(d1, d2, ignore_numeric_type_changes=True)
assert res == {}

d1 = {"a": date(2023, 7, 5)}
d2 = {"a": date(2023, 7, 6)}
res = DeepDiff(d1, d2)
expected = {
"values_changed": {
"root['a']": {
"new_value": date(2023, 7, 6),
"old_value": date(2023, 7, 5),
}
}
}
assert res == expected

def test_time_diff(self):
"""Testing for the correct setting and usage of epsilon."""
d1 = {"a": time(10, 11, 12)}
d2 = {"a": time(10, 11, 12)}
res = DeepDiff(d1, d2)
assert res == {}

res = DeepDiff(d1, d2, ignore_numeric_type_changes=True)
assert res == {}

d1 = {"a": time(10, 11, 12)}
d2 = {"a": time(11, 11, 12)}
res = DeepDiff(d1, d2)
expected = {
"values_changed": {
"root['a']": {
"new_value": time(11, 11, 12),
"old_value": time(10, 11, 12),
}
}
}
assert res == expected

0 comments on commit a08ba65

Please sign in to comment.