| 1 |
import pytz |
| 2 |
from datetime import date, datetime, time, timezone |
| 3 |
from deepdiff import DeepDiff |
| 4 |
|
| 5 |
|
| 6 |
class TestDiffDatetime: |
| 7 |
def test_datetime_diff(self): |
| 8 |
"""Testing for the correct setting and usage of epsilon.""" |
| 9 |
d1 = {"a": datetime(2023, 7, 5, 10, 11, 12)} |
| 10 |
d2 = {"a": datetime(2023, 7, 5, 10, 11, 12)} |
| 11 |
res = DeepDiff(d1, d2) |
| 12 |
assert res == {} |
| 13 |
|
| 14 |
res = DeepDiff(d1, d2, ignore_numeric_type_changes=True) |
| 15 |
assert res == {} |
| 16 |
|
| 17 |
d1 = {"a": datetime(2023, 7, 5, 10, 11, 12)} |
| 18 |
d2 = {"a": datetime(2023, 7, 5, 11, 11, 12)} |
| 19 |
res = DeepDiff(d1, d2) |
| 20 |
expected = { |
| 21 |
"values_changed": { |
| 22 |
"root['a']": { |
| 23 |
"new_value": datetime(2023, 7, 5, 11, 11, 12, tzinfo=timezone.utc), |
| 24 |
"old_value": datetime(2023, 7, 5, 10, 11, 12, tzinfo=timezone.utc), |
| 25 |
} |
| 26 |
} |
| 27 |
} |
| 28 |
assert res == expected |
| 29 |
|
| 30 |
|
| 31 |
def test_date_diff(self): |
| 32 |
"""Testing for the correct setting and usage of epsilon.""" |
| 33 |
d1 = {"a": date(2023, 7, 5)} |
| 34 |
d2 = {"a": date(2023, 7, 5)} |
| 35 |
res = DeepDiff(d1, d2) |
| 36 |
assert res == {} |
| 37 |
|
| 38 |
# this usage failed in version >=6.0, <=6.3.0 |
| 39 |
res = DeepDiff(d1, d2, ignore_numeric_type_changes=True) |
| 40 |
assert res == {} |
| 41 |
|
| 42 |
d1 = {"a": date(2023, 7, 5)} |
| 43 |
d2 = {"a": date(2023, 7, 6)} |
| 44 |
res = DeepDiff(d1, d2) |
| 45 |
expected = { |
| 46 |
"values_changed": { |
| 47 |
"root['a']": { |
| 48 |
"new_value": date(2023, 7, 6), |
| 49 |
"old_value": date(2023, 7, 5), |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
assert res == expected |
| 54 |
|
| 55 |
def test_time_diff(self): |
| 56 |
"""Testing for the correct setting and usage of epsilon.""" |
| 57 |
d1 = {"a": time(10, 11, 12)} |
| 58 |
d2 = {"a": time(10, 11, 12)} |
| 59 |
res = DeepDiff(d1, d2) |
| 60 |
assert res == {} |
| 61 |
|
| 62 |
res = DeepDiff(d1, d2, ignore_numeric_type_changes=True) |
| 63 |
assert res == {} |
| 64 |
|
| 65 |
d1 = {"a": time(10, 11, 12)} |
| 66 |
d2 = {"a": time(11, 11, 12)} |
| 67 |
res = DeepDiff(d1, d2) |
| 68 |
expected = { |
| 69 |
"values_changed": { |
| 70 |
"root['a']": { |
| 71 |
"new_value": time(11, 11, 12), |
| 72 |
"old_value": time(10, 11, 12), |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
assert res == expected |
| 77 |
|
| 78 |
def test_diffs_datetimes_different_timezones(self): |
| 79 |
dt_utc = datetime(2025, 2, 3, 12, 0, 0, tzinfo=pytz.utc) # UTC timezone |
| 80 |
# Convert it to another timezone (e.g., New York) |
| 81 |
dt_ny = dt_utc.astimezone(pytz.timezone('America/New_York')) |
| 82 |
assert dt_utc == dt_ny |
| 83 |
diff = DeepDiff(dt_utc, dt_ny) |
| 84 |
assert not diff |
| 85 |
|
| 86 |
t1 = [dt_utc, dt_ny] |
| 87 |
t2 = [dt_ny, dt_utc] |
| 88 |
assert not DeepDiff(t1, t2) |
| 89 |
assert not DeepDiff(t1, t2, ignore_order=True) |
| 90 |
|
| 91 |
t2 = [dt_ny, dt_utc, dt_ny] |
| 92 |
assert not DeepDiff(t1, t2, ignore_order=True) |
| 93 |
|
| 94 |
def test_diffs_datetimes_in_different_timezones(self): |
| 95 |
dt_utc = datetime(2025, 2, 3, 12, 0, 0, tzinfo=pytz.utc) # UTC timezone |
| 96 |
dt_utc2 = datetime(2025, 2, 3, 11, 0, 0, tzinfo=pytz.utc) # UTC timezone |
| 97 |
dt_ny = dt_utc.astimezone(pytz.timezone('America/New_York')) |
| 98 |
dt_ny2 = dt_utc2.astimezone(pytz.timezone('America/New_York')) |
| 99 |
diff = DeepDiff(dt_ny, dt_ny2) |
| 100 |
assert { |
| 101 |
"values_changed": { |
| 102 |
"root": { |
| 103 |
"new_value": dt_utc2, |
| 104 |
"old_value": dt_utc, |
| 105 |
} |
| 106 |
} |
| 107 |
} == diff |
| 108 |
diff2 = DeepDiff(dt_ny, dt_ny2, default_timezone=pytz.timezone('America/New_York')) |
| 109 |
assert { |
| 110 |
"values_changed": { |
| 111 |
"root": { |
| 112 |
"new_value": dt_ny2, |
| 113 |
"old_value": dt_ny, |
| 114 |
} |
| 115 |
} |
| 116 |
} == diff2 |
| 117 |
|
| 118 |
def test_datetime_within_array_with_timezone_diff(self): |
| 119 |
d1 = [datetime(2020, 8, 31, 13, 14, 1)] |
| 120 |
d2 = [datetime(2020, 8, 31, 13, 14, 1, tzinfo=timezone.utc)] |
| 121 |
|
| 122 |
assert d1 != d2, "Python doesn't think these are the same datetimes" |
| 123 |
assert not DeepDiff(d1, d2) |
| 124 |
assert not DeepDiff(d1, d2, ignore_order=True) |
| 125 |
assert not DeepDiff(d1, d2, truncate_datetime='second') |