Fix catching of warnings during testing.
This commit is contained in:
parent
a3d800240b
commit
79b9b4d4f1
|
@ -15,7 +15,7 @@ planck_constant = 6.62606957e-34 J s = h
|
||||||
hbar = planck_constant / (2 * pi) = ħ
|
hbar = planck_constant / (2 * pi) = ħ
|
||||||
|
|
||||||
# 0.000 80 e-11
|
# 0.000 80 e-11
|
||||||
newtonian_constant_of_gravitation = 6.67384e-11 m^3 kg^-1 s^-2 = G
|
newtonian_constant_of_gravitation = 6.67384e-11 m^3 kg^-1 s^-2
|
||||||
|
|
||||||
# 0.000 000 035 e-19
|
# 0.000 000 035 e-19
|
||||||
# elementary_charge = 1.602176565e-19 C = e
|
# elementary_charge = 1.602176565e-19 C = e
|
||||||
|
|
|
@ -72,7 +72,7 @@ US_survey_acre = 160 * rod ** 2
|
||||||
esu = 1 * erg**0.5 * centimeter**0.5 = statcoulombs = statC = franklin = Fr
|
esu = 1 * erg**0.5 * centimeter**0.5 = statcoulombs = statC = franklin = Fr
|
||||||
esu_per_second = 1 * esu / second = statampere
|
esu_per_second = 1 * esu / second = statampere
|
||||||
ampere_turn = 1 * A
|
ampere_turn = 1 * A
|
||||||
gilbert = 10 / (4 * pi ) * ampere_turn = G
|
gilbert = 10 / (4 * pi ) * ampere_turn
|
||||||
coulomb = ampere * second = C
|
coulomb = ampere * second = C
|
||||||
volt = joule / coulomb = V
|
volt = joule / coulomb = V
|
||||||
farad = coulomb / volt = F
|
farad = coulomb / volt = F
|
||||||
|
|
|
@ -9,6 +9,7 @@ from pint.compat import ndarray, unittest
|
||||||
|
|
||||||
from pint import logger, UnitRegistry
|
from pint import logger, UnitRegistry
|
||||||
from pint.quantity import _Quantity
|
from pint.quantity import _Quantity
|
||||||
|
from logging.handlers import BufferingHandler
|
||||||
|
|
||||||
h = logging.StreamHandler()
|
h = logging.StreamHandler()
|
||||||
f = logging.Formatter("%(levelname)s %(asctime)s %(funcName)s %(lineno)d %(message)s")
|
f = logging.Formatter("%(levelname)s %(asctime)s %(funcName)s %(lineno)d %(message)s")
|
||||||
|
@ -18,6 +19,22 @@ logger.addHandler(h)
|
||||||
logger.setLevel(logging.DEBUG)
|
logger.setLevel(logging.DEBUG)
|
||||||
|
|
||||||
|
|
||||||
|
class TestHandler(BufferingHandler):
|
||||||
|
def __init__(self):
|
||||||
|
# BufferingHandler takes a "capacity" argument
|
||||||
|
# so as to know when to flush. As we're overriding
|
||||||
|
# shouldFlush anyway, we can set a capacity of zero.
|
||||||
|
# You can call flush() manually to clear out the
|
||||||
|
# buffer.
|
||||||
|
BufferingHandler.__init__(self, 0)
|
||||||
|
|
||||||
|
def shouldFlush(self):
|
||||||
|
return False
|
||||||
|
|
||||||
|
def emit(self, record):
|
||||||
|
self.buffer.append(record.__dict__)
|
||||||
|
|
||||||
|
|
||||||
class TestCase(unittest.TestCase):
|
class TestCase(unittest.TestCase):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|
|
@ -8,28 +8,11 @@ from collections import defaultdict
|
||||||
from pint import UnitRegistry
|
from pint import UnitRegistry
|
||||||
from pint.context import Context, _freeze
|
from pint.context import Context, _freeze
|
||||||
from pint.unit import UnitsContainer
|
from pint.unit import UnitsContainer
|
||||||
from pint.testsuite import TestCase
|
from pint.testsuite import TestCase, TestHandler
|
||||||
from pint.compat import unittest
|
from pint.compat import unittest
|
||||||
|
|
||||||
from pint import logger
|
from pint import logger
|
||||||
|
|
||||||
from logging.handlers import BufferingHandler
|
|
||||||
|
|
||||||
class TestHandler(BufferingHandler):
|
|
||||||
def __init__(self):
|
|
||||||
# BufferingHandler takes a "capacity" argument
|
|
||||||
# so as to know when to flush. As we're overriding
|
|
||||||
# shouldFlush anyway, we can set a capacity of zero.
|
|
||||||
# You can call flush() manually to clear out the
|
|
||||||
# buffer.
|
|
||||||
BufferingHandler.__init__(self, 0)
|
|
||||||
|
|
||||||
def shouldFlush(self):
|
|
||||||
return False
|
|
||||||
|
|
||||||
def emit(self, record):
|
|
||||||
self.buffer.append(record.__dict__)
|
|
||||||
|
|
||||||
def add_ctxs(ureg):
|
def add_ctxs(ureg):
|
||||||
a, b = UnitsContainer({'[length]': 1}), UnitsContainer({'[time]': -1})
|
a, b = UnitsContainer({'[length]': 1}), UnitsContainer({'[time]': -1})
|
||||||
d = Context('lc')
|
d = Context('lc')
|
||||||
|
|
|
@ -4,7 +4,6 @@ from __future__ import division, unicode_literals, print_function, absolute_impo
|
||||||
|
|
||||||
import math
|
import math
|
||||||
import copy
|
import copy
|
||||||
import warnings
|
|
||||||
import operator as op
|
import operator as op
|
||||||
|
|
||||||
from pint.unit import (ScaleConverter, OffsetConverter, UnitsContainer,
|
from pint.unit import (ScaleConverter, OffsetConverter, UnitsContainer,
|
||||||
|
@ -13,7 +12,7 @@ from pint.unit import (ScaleConverter, OffsetConverter, UnitsContainer,
|
||||||
LazyRegistry, ParserHelper)
|
LazyRegistry, ParserHelper)
|
||||||
from pint import DimensionalityError, UndefinedUnitError
|
from pint import DimensionalityError, UndefinedUnitError
|
||||||
from pint.compat import u, unittest
|
from pint.compat import u, unittest
|
||||||
from pint.testsuite import TestCase
|
from pint.testsuite import TestCase, logger, TestHandler
|
||||||
|
|
||||||
|
|
||||||
class TestConverter(unittest.TestCase):
|
class TestConverter(unittest.TestCase):
|
||||||
|
@ -434,6 +433,21 @@ class TestRegistry(TestCase):
|
||||||
self.assertEqual(t.to('kelvin').magnitude, self.ureg._units['degF'].converter.to_reference(8.))
|
self.assertEqual(t.to('kelvin').magnitude, self.ureg._units['degF'].converter.to_reference(8.))
|
||||||
self.assertEqual(dt.to('delta_kelvin').magnitude, self.ureg._units['delta_degF'].converter.to_reference(8.))
|
self.assertEqual(dt.to('delta_kelvin').magnitude, self.ureg._units['delta_degF'].converter.to_reference(8.))
|
||||||
|
|
||||||
|
def test_redefinition(self):
|
||||||
|
d = UnitRegistry().define
|
||||||
|
|
||||||
|
th = TestHandler()
|
||||||
|
logger.addHandler(th)
|
||||||
|
d('meter = [fruits]')
|
||||||
|
d('kilo- = 1000')
|
||||||
|
d('[speed] = [vegetables]')
|
||||||
|
|
||||||
|
# aliases
|
||||||
|
d('bla = 3.2 meter = inch')
|
||||||
|
d('myk- = 1000 = kilo-')
|
||||||
|
|
||||||
|
self.assertEqual(len(th.buffer), 5)
|
||||||
|
|
||||||
|
|
||||||
class TestEquivalents(TestCase):
|
class TestEquivalents(TestCase):
|
||||||
|
|
||||||
|
@ -496,20 +510,6 @@ class TestEquivalents(TestCase):
|
||||||
self.assertEqual(ureg.parse_units(''), UnitsContainer())
|
self.assertEqual(ureg.parse_units(''), UnitsContainer())
|
||||||
self.assertRaises(ValueError, ureg.parse_units, '2 * meter')
|
self.assertRaises(ValueError, ureg.parse_units, '2 * meter')
|
||||||
|
|
||||||
def test_redefinition(self):
|
|
||||||
d = UnitRegistry().define
|
|
||||||
|
|
||||||
with warnings.catch_warnings(record=True) as w:
|
|
||||||
d('meter = [time]')
|
|
||||||
d('kilo- = 1000')
|
|
||||||
d('[speed] = [length]')
|
|
||||||
|
|
||||||
# aliases
|
|
||||||
d('bla = 3.2 meter = inch')
|
|
||||||
d('myk- = 1000 = kilo-')
|
|
||||||
|
|
||||||
self.assertEqual(len(w), 5)
|
|
||||||
|
|
||||||
|
|
||||||
class TestRegistryWithDefaultRegistry(TestRegistry):
|
class TestRegistryWithDefaultRegistry(TestRegistry):
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue