summaryrefslogtreecommitdiff
path: root/pint/testsuite/test_errors.py
blob: 3e65c613520ac7e287d9697af4613030a37257d9 (plain)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import pickle

from pint import (
    DefinitionSyntaxError,
    DimensionalityError,
    LogarithmicUnitCalculusError,
    OffsetUnitCalculusError,
    Quantity,
    RedefinitionError,
    UndefinedUnitError,
    UnitRegistry,
)
from pint.errors import LOG_ERROR_DOCS_HTML, OFFSET_ERROR_DOCS_HTML
from pint.testsuite import BaseTestCase


class TestErrors(BaseTestCase):
    def test_definition_syntax_error(self):
        ex = DefinitionSyntaxError("foo")
        self.assertEqual(str(ex), "foo")

        # filename and lineno can be attached after init
        ex.filename = "a.txt"
        ex.lineno = 123
        self.assertEqual(str(ex), "While opening a.txt, in line 123: foo")

        ex = DefinitionSyntaxError("foo", lineno=123)
        self.assertEqual(str(ex), "In line 123: foo")

        ex = DefinitionSyntaxError("foo", filename="a.txt")
        self.assertEqual(str(ex), "While opening a.txt: foo")

        ex = DefinitionSyntaxError("foo", filename="a.txt", lineno=123)
        self.assertEqual(str(ex), "While opening a.txt, in line 123: foo")

    def test_redefinition_error(self):
        ex = RedefinitionError("foo", "bar")
        self.assertEqual(str(ex), "Cannot redefine 'foo' (bar)")

        # filename and lineno can be attached after init
        ex.filename = "a.txt"
        ex.lineno = 123
        self.assertEqual(
            str(ex), "While opening a.txt, in line 123: Cannot redefine 'foo' (bar)"
        )

        ex = RedefinitionError("foo", "bar", lineno=123)
        self.assertEqual(str(ex), "In line 123: Cannot redefine 'foo' (bar)")

        ex = RedefinitionError("foo", "bar", filename="a.txt")
        self.assertEqual(str(ex), "While opening a.txt: Cannot redefine 'foo' (bar)")

        ex = RedefinitionError("foo", "bar", filename="a.txt", lineno=123)
        self.assertEqual(
            str(ex), "While opening a.txt, in line 123: Cannot redefine 'foo' (bar)"
        )

    def test_undefined_unit_error(self):
        x = ("meter",)
        msg = "'meter' is not defined in the unit registry"
        self.assertEqual(str(UndefinedUnitError(x)), msg)
        self.assertEqual(str(UndefinedUnitError(list(x))), msg)
        self.assertEqual(str(UndefinedUnitError(set(x))), msg)

    def test_undefined_unit_error_multi(self):
        x = ("meter", "kg")
        msg = "('meter', 'kg') are not defined in the unit registry"
        self.assertEqual(str(UndefinedUnitError(x)), msg)
        self.assertEqual(str(UndefinedUnitError(list(x))), msg)

    def test_dimensionality_error(self):
        ex = DimensionalityError("a", "b")
        self.assertEqual(str(ex), "Cannot convert from 'a' to 'b'")
        ex = DimensionalityError("a", "b", "c")
        self.assertEqual(str(ex), "Cannot convert from 'a' (c) to 'b' ()")
        ex = DimensionalityError("a", "b", "c", "d", extra_msg=": msg")
        self.assertEqual(str(ex), "Cannot convert from 'a' (c) to 'b' (d): msg")

    def test_offset_unit_calculus_error(self):
        ex = OffsetUnitCalculusError(Quantity("1 kg")._units)
        self.assertEqual(
            str(ex),
            "Ambiguous operation with offset unit (kilogram). See "
            + OFFSET_ERROR_DOCS_HTML
            + " for guidance.",
        )
        ex = OffsetUnitCalculusError(Quantity("1 kg")._units, Quantity("1 s")._units)
        self.assertEqual(
            str(ex),
            "Ambiguous operation with offset unit (kilogram, second). See "
            + OFFSET_ERROR_DOCS_HTML
            + " for guidance.",
        )

    def test_logarithmic_unit_calculus_error(self):
        Quantity = UnitRegistry(autoconvert_offset_to_baseunit=True).Quantity
        ex = LogarithmicUnitCalculusError(Quantity("1 dB")._units)
        self.assertEqual(
            str(ex),
            "Ambiguous operation with logarithmic unit (decibel). See "
            + LOG_ERROR_DOCS_HTML
            + " for guidance.",
        )
        ex = LogarithmicUnitCalculusError(
            Quantity("1 dB")._units, Quantity("1 octave")._units
        )
        self.assertEqual(
            str(ex),
            "Ambiguous operation with logarithmic unit (decibel, octave). See "
            + LOG_ERROR_DOCS_HTML
            + " for guidance.",
        )

    def test_pickle_definition_syntax_error(self):
        # OffsetUnitCalculusError raised from a custom ureg must be pickleable even if
        # the ureg is not registered as the application ureg
        ureg = UnitRegistry(filename=None)
        ureg.define("foo = [bar]")
        ureg.define("bar = 2 foo")
        q1 = ureg.Quantity("1 foo")
        q2 = ureg.Quantity("1 bar")

        for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
            for ex in [
                DefinitionSyntaxError("foo", filename="a.txt", lineno=123),
                RedefinitionError("foo", "bar"),
                UndefinedUnitError("meter"),
                DimensionalityError("a", "b", "c", "d", extra_msg=": msg"),
                OffsetUnitCalculusError(
                    Quantity("1 kg")._units, Quantity("1 s")._units
                ),
                OffsetUnitCalculusError(q1._units, q2._units),
            ]:
                with self.subTest(protocol=protocol, etype=type(ex)):
                    pik = pickle.dumps(ureg.Quantity("1 foo"), protocol)
                    with self.assertRaises(UndefinedUnitError):
                        pickle.loads(pik)

                    # assert False, ex.__reduce__()
                    ex2 = pickle.loads(pickle.dumps(ex, protocol))
                    assert type(ex) is type(ex2)
                    self.assertEqual(ex.args, ex2.args)
                    self.assertEqual(ex.__dict__, ex2.__dict__)
                    self.assertEqual(str(ex), str(ex2))