Debian Codegraph @

tests/test_diff_math.py

raw
1 from decimal import Decimal
2 from deepdiff import DeepDiff
3
4
5 class TestDiffMath:
6 def test_math_diff(self):
7 """Testing for the correct setting and usage of epsilon."""
8 d1 = {"a": Decimal("3.5")}
9 d2 = {"a": Decimal("4")}
10 ep = 0.5
11 res = DeepDiff(d1, d2, math_epsilon=ep)
12 assert res == {}
13 d1 = {"a": Decimal("2.5")}
14 d2 = {"a": Decimal("3")}
15 ep = 0.5
16 res = DeepDiff(d1, d2, math_epsilon=ep)
17 assert res == {}
18 d1 = {"a": Decimal("2.5")}
19 d2 = {"a": Decimal("2")}
20 ep = 0.5
21 res = DeepDiff(d1, d2, math_epsilon=ep)
22 assert res == {}
23 d1 = {"a": Decimal("7.175")}
24 d2 = {"a": Decimal("7.174")}
25 ep = 0.1
26 res = DeepDiff(d1, d2, math_epsilon=ep)
27 assert res == {}
28 d1 = {"a": Decimal("7.175")}
29 d2 = {"a": Decimal("7.174")}
30 ep = 0.01
31 res = DeepDiff(d1, d2, math_epsilon=ep)
32 assert res == {}
33 d1 = {"a": Decimal("7.175")}
34 d2 = {"a": Decimal("7.174")}
35 ep = 0.001
36 res = DeepDiff(d1, d2, math_epsilon=ep)
37 assert res == {}
38 d1 = {"a": Decimal("7.175")}
39 d2 = {"a": Decimal("7.174")}
40 ep = 0.0001
41 expected = {
42 "values_changed": {
43 "root['a']": {
44 "new_value": Decimal("7.174"),
45 "old_value": Decimal("7.175"),
46 }
47 }
48 }
49 res = DeepDiff(d1, d2, math_epsilon=ep)
50 assert res == expected
51
52 def test_math_diff_special_case(self):
53 """Testing epsilon on a special Decimal case.
54
55 Even though the Decimal looks different, math will evaluate it for us."""
56 d1 = {"a": Decimal("9.709999618320632")}
57 d2 = {"a": Decimal("9.710000038146973")}
58 ep = 0.001
59 res = DeepDiff(d1, d2, math_epsilon=ep)
60 assert res == {}
61 d1 = {"a": Decimal("9.709999618320632")}
62 d2 = {"a": Decimal("9.710000038146973")}
63 ep = 0.0001
64 res = DeepDiff(d1, d2, math_epsilon=ep)
65 assert res == {}
66 d1 = {"a": Decimal("9.709999618320632")}
67 d2 = {"a": Decimal("9.710000038146973")}
68 ep = 0.00001
69 res = DeepDiff(d1, d2, math_epsilon=ep)
70 assert res == {}
71 d1 = {"a": Decimal("9.709999618320632")}
72 d2 = {"a": Decimal("9.710000038146973")}
73 ep = 0.000001
74 res = DeepDiff(d1, d2, math_epsilon=ep)
75 assert res == {}
76 d1 = {"a": Decimal("9.709999618320632")}
77 d2 = {"a": Decimal("9.710000038146973")}
78 ep = 0.0000001
79 res = DeepDiff(d1, d2, math_epsilon=ep)
80 expected = {
81 "values_changed": {
82 "root['a']": {
83 "new_value": Decimal("9.710000038146973"),
84 "old_value": Decimal("9.709999618320632"),
85 }
86 }
87 }
88 assert res == expected
89
90 def test_math_diff_ignore_order(self):
91 """math_close will not work with ignore_order=true.
92
93 Items are hashed if order is ignored, that will not work."""
94 d1 = {"a": [Decimal("9.709999618320632"), Decimal("9.709999618320632")]}
95 d2 = {"a": [Decimal("9.710000038146973"), Decimal("9.709999618320632")]}
96 ep = 0.0001
97 res = DeepDiff(d1, d2, ignore_order=False, math_epsilon=ep)
98 assert res == {}
99
100 def test_math_diff_ignore_order_warning(self, caplog):
101 """math_close will not work with ignore_order=true.
102
103 Items are hashed if order is ignored, that will not work."""
104 d1 = {"a": [Decimal("9.709999618320632"), Decimal("9.709999618320632")]}
105 d2 = {"a": [Decimal("9.710000038146973"), Decimal("9.709999618320632")]}
106 ep = 0.0001
107 res = DeepDiff(d1, d2, ignore_order=True, math_epsilon=ep)
108 expected = {
109 "iterable_item_added": {"root['a'][0]": Decimal("9.710000038146973")}
110 }
111 assert res == expected
112 # assert "math_epsilon will be ignored." in caplog.text
113
114 def test_ignore_numeric_type_changes_with_numeric_keys_and_no_significant_digits(self):
115 """Test that ignore_numeric_type_changes works with numeric keys when significant_digits is None.
116
117 This test covers the bug fix in _get_clean_to_keys_mapping where significant_digits=None
118 caused a crash when number_to_string was called without the required parameter.
119 """
120 # Test case with numeric keys and ignore_numeric_type_changes=True, significant_digits=None
121 d1 = {1: "value1", 2.5: "value2"}
122 d2 = {1.0: "value1", 2.5: "value2"} # int vs float keys
123
124 # This should not crash and should treat 1 and 1.0 as the same key
125 result = DeepDiff(d1, d2, ignore_numeric_type_changes=True, significant_digits=None)
126 assert result == {}