File: test_diff_math.py

package info (click to toggle)
deepdiff 8.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,976 kB
  • sloc: python: 16,739; makefile: 167
file content (126 lines) | stat: -rw-r--r-- 4,886 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
from decimal import Decimal
from deepdiff import DeepDiff


class TestDiffMath:
    def test_math_diff(self):
        """Testing for the correct setting and usage of epsilon."""
        d1 = {"a": Decimal("3.5")}
        d2 = {"a": Decimal("4")}
        ep = 0.5
        res = DeepDiff(d1, d2, math_epsilon=ep)
        assert res == {}
        d1 = {"a": Decimal("2.5")}
        d2 = {"a": Decimal("3")}
        ep = 0.5
        res = DeepDiff(d1, d2, math_epsilon=ep)
        assert res == {}
        d1 = {"a": Decimal("2.5")}
        d2 = {"a": Decimal("2")}
        ep = 0.5
        res = DeepDiff(d1, d2, math_epsilon=ep)
        assert res == {}
        d1 = {"a": Decimal("7.175")}
        d2 = {"a": Decimal("7.174")}
        ep = 0.1
        res = DeepDiff(d1, d2, math_epsilon=ep)
        assert res == {}
        d1 = {"a": Decimal("7.175")}
        d2 = {"a": Decimal("7.174")}
        ep = 0.01
        res = DeepDiff(d1, d2, math_epsilon=ep)
        assert res == {}
        d1 = {"a": Decimal("7.175")}
        d2 = {"a": Decimal("7.174")}
        ep = 0.001
        res = DeepDiff(d1, d2, math_epsilon=ep)
        assert res == {}
        d1 = {"a": Decimal("7.175")}
        d2 = {"a": Decimal("7.174")}
        ep = 0.0001
        expected = {
            "values_changed": {
                "root['a']": {
                    "new_value": Decimal("7.174"),
                    "old_value": Decimal("7.175"),
                }
            }
        }
        res = DeepDiff(d1, d2, math_epsilon=ep)
        assert res == expected

    def test_math_diff_special_case(self):
        """Testing epsilon on a special Decimal case.
        
        Even though the Decimal looks different, math will evaluate it for us."""
        d1 = {"a": Decimal("9.709999618320632")}
        d2 = {"a": Decimal("9.710000038146973")}
        ep = 0.001
        res = DeepDiff(d1, d2, math_epsilon=ep)
        assert res == {}
        d1 = {"a": Decimal("9.709999618320632")}
        d2 = {"a": Decimal("9.710000038146973")}
        ep = 0.0001
        res = DeepDiff(d1, d2, math_epsilon=ep)
        assert res == {}
        d1 = {"a": Decimal("9.709999618320632")}
        d2 = {"a": Decimal("9.710000038146973")}
        ep = 0.00001
        res = DeepDiff(d1, d2, math_epsilon=ep)
        assert res == {}
        d1 = {"a": Decimal("9.709999618320632")}
        d2 = {"a": Decimal("9.710000038146973")}
        ep = 0.000001
        res = DeepDiff(d1, d2, math_epsilon=ep)
        assert res == {}
        d1 = {"a": Decimal("9.709999618320632")}
        d2 = {"a": Decimal("9.710000038146973")}
        ep = 0.0000001
        res = DeepDiff(d1, d2, math_epsilon=ep)
        expected = {
            "values_changed": {
                "root['a']": {
                    "new_value": Decimal("9.710000038146973"),
                    "old_value": Decimal("9.709999618320632"),
                }
            }
        }
        assert res == expected

    def test_math_diff_ignore_order(self):
        """math_close will not work with ignore_order=true.
        
        Items are hashed if order is ignored, that will not work."""
        d1 = {"a": [Decimal("9.709999618320632"), Decimal("9.709999618320632")]}
        d2 = {"a": [Decimal("9.710000038146973"), Decimal("9.709999618320632")]}
        ep = 0.0001
        res = DeepDiff(d1, d2, ignore_order=False, math_epsilon=ep)
        assert res == {}

    def test_math_diff_ignore_order_warning(self, caplog):
        """math_close will not work with ignore_order=true.
        
        Items are hashed if order is ignored, that will not work."""
        d1 = {"a": [Decimal("9.709999618320632"), Decimal("9.709999618320632")]}
        d2 = {"a": [Decimal("9.710000038146973"), Decimal("9.709999618320632")]}
        ep = 0.0001
        res = DeepDiff(d1, d2, ignore_order=True, math_epsilon=ep)
        expected = {
            "iterable_item_added": {"root['a'][0]": Decimal("9.710000038146973")}
        }
        assert res == expected
        # assert "math_epsilon will be ignored." in caplog.text

    def test_ignore_numeric_type_changes_with_numeric_keys_and_no_significant_digits(self):
        """Test that ignore_numeric_type_changes works with numeric keys when significant_digits is None.
        
        This test covers the bug fix in _get_clean_to_keys_mapping where significant_digits=None
        caused a crash when number_to_string was called without the required parameter.
        """
        # Test case with numeric keys and ignore_numeric_type_changes=True, significant_digits=None
        d1 = {1: "value1", 2.5: "value2"}  
        d2 = {1.0: "value1", 2.5: "value2"}  # int vs float keys
        
        # This should not crash and should treat 1 and 1.0 as the same key
        result = DeepDiff(d1, d2, ignore_numeric_type_changes=True, significant_digits=None)
        assert result == {}