Exemplo n.º 1
0
    def test_errors(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)

        msg = "Cannot convert from 'a' (c) to 'b' (d)msg"
        ex = DimensionalityError('a', 'b', 'c', 'd', 'msg')
        self.assertEqual(str(ex), msg)
Exemplo n.º 2
0
    def test_undefined_unit_error_multi(self):
        x = ("meter", "kg")
        msg = "('meter', 'kg') are not defined in the unit registry"

        ex = UndefinedUnitError(x)
        assert str(ex) == msg
        ex = UndefinedUnitError(list(x))
        assert str(ex) == msg

        with pytest.raises(PintError):
            raise ex
Exemplo n.º 3
0
 def test_str_errors(self):
     self.assertEqual(
         str(UndefinedUnitError('rabbits')),
         "'{0!s}' is not defined in the unit registry".format('rabbits'))
     self.assertEqual(
         str(UndefinedUnitError(('rabbits', 'horses'))),
         "'{0!s}' are not defined in the unit registry".format(
             ('rabbits', 'horses')))
     self.assertEqual(u(str(DimensionalityError('meter', 'second'))),
                      "Cannot convert from 'meter' to 'second'")
     self.assertEqual(
         str(DimensionalityError('meter', 'second', 'length', 'time')),
         "Cannot convert from 'meter' (length) to 'second' (time)")
Exemplo n.º 4
0
    def test_undefined_unit_error(self):
        x = ("meter", )
        msg = "'meter' is not defined in the unit registry"

        ex = UndefinedUnitError(x)
        assert str(ex) == msg
        ex = UndefinedUnitError(list(x))
        assert str(ex) == msg
        ex = UndefinedUnitError(set(x))
        assert str(ex) == msg

        with pytest.raises(PintError):
            raise ex
Exemplo n.º 5
0
    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))
Exemplo n.º 6
0
 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)
Exemplo n.º 7
0
 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)
Exemplo n.º 8
0
 def test_undefined_unit_error(self):
     x = ("meter", )
     msg = "'meter' is not defined in the unit registry"
     assert str(UndefinedUnitError(x)) == msg
     assert str(UndefinedUnitError(list(x))) == msg
     assert str(UndefinedUnitError(set(x))) == msg