diff options
Diffstat (limited to 'pint/testsuite/test_log_units.py')
-rw-r--r-- | pint/testsuite/test_log_units.py | 48 |
1 files changed, 26 insertions, 22 deletions
diff --git a/pint/testsuite/test_log_units.py b/pint/testsuite/test_log_units.py index 0860dd7..77eba02 100644 --- a/pint/testsuite/test_log_units.py +++ b/pint/testsuite/test_log_units.py @@ -1,9 +1,10 @@ +import logging import math import pytest from pint import OffsetUnitCalculusError, UnitRegistry -from pint.testsuite import QuantityTestCase +from pint.testsuite import QuantityTestCase, helpers from pint.unit import Unit, UnitsContainer @@ -18,10 +19,7 @@ def ureg(): class TestLogarithmicQuantity(QuantityTestCase): - - FORCE_NDARRAY = False - - def test_log_quantity_creation(self): + def test_log_quantity_creation(self, caplog): # Following Quantity Creation Pattern for args in ( @@ -30,42 +28,46 @@ class TestLogarithmicQuantity(QuantityTestCase): (4.2, self.ureg.dBm), ): x = self.Q_(*args) - self.assertEqual(x.magnitude, 4.2) - self.assertEqual(x.units, UnitsContainer(decibelmilliwatt=1)) + assert x.magnitude == 4.2 + assert x.units == UnitsContainer(decibelmilliwatt=1) x = self.Q_(self.Q_(4.2, "dBm")) - self.assertEqual(x.magnitude, 4.2) - self.assertEqual(x.units, UnitsContainer(decibelmilliwatt=1)) + assert x.magnitude == 4.2 + assert x.units == UnitsContainer(decibelmilliwatt=1) x = self.Q_(4.2, UnitsContainer(decibelmilliwatt=1)) y = self.Q_(x) - self.assertEqual(x.magnitude, y.magnitude) - self.assertEqual(x.units, y.units) - self.assertIsNot(x, y) + assert x.magnitude == y.magnitude + assert x.units == y.units + assert x is not y # Using multiplications for dB units requires autoconversion to baseunits new_reg = UnitRegistry(autoconvert_offset_to_baseunit=True) x = new_reg.Quantity("4.2 * dBm") - self.assertEqual(x.magnitude, 4.2) - self.assertEqual(x.units, UnitsContainer(decibelmilliwatt=1)) + assert x.magnitude == 4.2 + assert x.units == UnitsContainer(decibelmilliwatt=1) + + with caplog.at_level(logging.DEBUG): + assert "wally" not in caplog.text + assert 4.2 * new_reg.dBm == new_reg.Quantity(4.2, 2 * new_reg.dBm) - with self.capture_log() as buffer: - self.assertEqual(4.2 * new_reg.dBm, new_reg.Quantity(4.2, 2 * new_reg.dBm)) - self.assertEqual(len(buffer), 1) + assert len(caplog.records) == 1 def test_log_convert(self): # # 1 dB = 1/10 * bel - # self.assertQuantityAlmostEqual(self.Q_(1.0, "dB").to("dimensionless"), self.Q_(1, "bell") / 10) + # helpers.assert_quantity_almost_equal(self.Q_(1.0, "dB").to("dimensionless"), self.Q_(1, "bell") / 10) # # Uncomment Bell unit in default_en.txt # ## Test dB to dB units octave - decade # 1 decade = log2(10) octave - self.assertQuantityAlmostEqual( + helpers.assert_quantity_almost_equal( self.Q_(1.0, "decade"), self.Q_(math.log(10, 2), "octave") ) # ## Test dB to dB units dBm - dBu # 0 dBm = 1mW = 1e3 uW = 30 dBu - self.assertAlmostEqual(self.Q_(0.0, "dBm"), self.Q_(29.999999999999996, "dBu")) + helpers.assert_quantity_almost_equal( + self.Q_(0.0, "dBm"), self.Q_(29.999999999999996, "dBu"), atol=1e-7 + ) def test_mix_regular_log_units(self): # Test regular-logarithmic mixed definition, such as dB/km or dB/cm @@ -73,13 +75,15 @@ class TestLogarithmicQuantity(QuantityTestCase): # Multiplications and divisions with a mix of Logarithmic Units and regular Units is normally not possible. # The reason is that dB are considered by pint like offset units. # Multiplications and divisions that involve offset units are badly defined, so pint raises an error - with self.assertRaises(OffsetUnitCalculusError): + with pytest.raises(OffsetUnitCalculusError): (-10.0 * self.ureg.dB) / (1 * self.ureg.cm) # However, if the flag autoconvert_offset_to_baseunit=True is given to UnitRegistry, then pint converts the unit to base. # With this flag on multiplications and divisions are now possible: new_reg = UnitRegistry(autoconvert_offset_to_baseunit=True) - self.assertQuantityAlmostEqual(-10 * new_reg.dB / new_reg.cm, 0.1 / new_reg.cm) + helpers.assert_quantity_almost_equal( + -10 * new_reg.dB / new_reg.cm, 0.1 / new_reg.cm + ) log_unit_names = [ |