diff options
Diffstat (limited to 'pint/testsuite')
-rw-r--r-- | pint/testsuite/helpers.py | 4 | ||||
-rw-r--r-- | pint/testsuite/test_babel.py | 35 | ||||
-rw-r--r-- | pint/testsuite/test_issues.py | 18 | ||||
-rw-r--r-- | pint/testsuite/test_numpy.py | 38 | ||||
-rw-r--r-- | pint/testsuite/test_quantity.py | 9 | ||||
-rw-r--r-- | pint/testsuite/test_unit.py | 41 |
6 files changed, 120 insertions, 25 deletions
diff --git a/pint/testsuite/helpers.py b/pint/testsuite/helpers.py index 5a91391..2bf743c 100644 --- a/pint/testsuite/helpers.py +++ b/pint/testsuite/helpers.py @@ -67,6 +67,10 @@ def requires_babel(): return unittest.skipUnless(HAS_BABEL, "Requires Babel with units support") +def requires_not_babel(): + return unittest.skipIf(HAS_BABEL, "Requires Babel is not installed") + + def requires_uncertainties(): return unittest.skipUnless(HAS_UNCERTAINTIES, "Requires Uncertainties") diff --git a/pint/testsuite/test_babel.py b/pint/testsuite/test_babel.py index 2aac0cd..def7c83 100644 --- a/pint/testsuite/test_babel.py +++ b/pint/testsuite/test_babel.py @@ -5,6 +5,14 @@ from pint.testsuite import BaseTestCase, helpers class TestBabel(BaseTestCase): + @helpers.requires_not_babel() + def test_no_babel(self): + ureg = UnitRegistry() + distance = 24.0 * ureg.meter + self.assertRaises( + Exception, distance.format_babel, locale="fr_FR", length="long" + ) + @helpers.requires_babel() def test_format(self): ureg = UnitRegistry() @@ -46,9 +54,32 @@ class TestBabel(BaseTestCase): mks = ureg.get_system("mks") self.assertEqual(mks.format_babel(locale="fr_FR"), "métrique") - def test_nobabel(self): + @helpers.requires_babel() + def test_no_registry_locale(self): ureg = UnitRegistry() distance = 24.0 * ureg.meter self.assertRaises( - Exception, distance.format_babel, locale="fr_FR", length="long" + Exception, distance.format_babel, ) + + @helpers.requires_babel() + def test_str(self): + ureg = UnitRegistry() + d = 24.0 * ureg.meter + + s = "24.0 meter" + self.assertEqual(str(d), s) + self.assertEqual("%s" % d, s) + self.assertEqual("{}".format(d), s) + + ureg.set_fmt_locale("fr_FR") + s = "24.0 mètres" + self.assertEqual(str(d), s) + self.assertEqual("%s" % d, s) + self.assertEqual("{}".format(d), s) + + ureg.set_fmt_locale(None) + s = "24.0 meter" + self.assertEqual(str(d), s) + self.assertEqual("%s" % d, s) + self.assertEqual("{}".format(d), s) diff --git a/pint/testsuite/test_issues.py b/pint/testsuite/test_issues.py index 3036022..5f42717 100644 --- a/pint/testsuite/test_issues.py +++ b/pint/testsuite/test_issues.py @@ -697,6 +697,24 @@ class TestIssues(QuantityTestCase): with self.assertRaises(DimensionalityError): q.to("joule") + def test_issue507(self): + # leading underscore in unit works with numbers + ureg.define("_100km = 100 * kilometer") + battery_ec = 16 * ureg.kWh / ureg._100km # noqa: F841 + # ... but not with text + ureg.define("_home = 4700 * kWh / year") + with self.assertRaises(AttributeError): + home_elec_power = 1 * ureg._home # noqa: F841 + # ... or with *only* underscores + ureg.define("_ = 45 * km") + with self.assertRaises(AttributeError): + one_blank = 1 * ureg._ # noqa: F841 + + def test_issue960(self): + q = (1 * ureg.nanometer).to_compact("micrometer") + assert q.units == ureg.nanometer + assert q.magnitude == 1 + try: diff --git a/pint/testsuite/test_numpy.py b/pint/testsuite/test_numpy.py index 0efb476..8e3d116 100644 --- a/pint/testsuite/test_numpy.py +++ b/pint/testsuite/test_numpy.py @@ -1,7 +1,6 @@ import copy import operator as op import unittest -from unittest.mock import patch from pint import DimensionalityError, OffsetUnitCalculusError, UnitStrippedWarning from pint.compat import np @@ -885,7 +884,7 @@ class TestNumpyUnclassified(TestNumpyMethods): @helpers.requires_array_function_protocol() def test_result_type_numpy_func(self): - self.assertEqual(np.result_type(self.q), np.dtype("int64")) + self.assertEqual(np.result_type(self.q), np.dtype("int")) @helpers.requires_array_function_protocol() def test_nan_to_num_numpy_func(self): @@ -1031,28 +1030,15 @@ class TestNumpyUnclassified(TestNumpyMethods): np.array([[1, 0, 2], [3, 0, 4]]) * self.ureg.m, ) - @patch("pint.quantity.ARRAY_FALLBACK", False) def test_ndarray_downcast(self): with self.assertWarns(UnitStrippedWarning): np.asarray(self.q) - @patch("pint.quantity.ARRAY_FALLBACK", False) def test_ndarray_downcast_with_dtype(self): with self.assertWarns(UnitStrippedWarning): qarr = np.asarray(self.q, dtype=np.float64) self.assertEqual(qarr.dtype, np.float64) - def test_array_protocol_fallback(self): - with self.assertWarns(DeprecationWarning) as cm: - for attr in ("__array_struct__", "__array_interface__"): - getattr(self.q, attr) - warning_text = str(cm.warnings[0].message) - self.assertTrue( - f"unit of the Quantity being stripped" in warning_text - and "will become unavailable" in warning_text - ) - - @patch("pint.quantity.ARRAY_FALLBACK", False) def test_array_protocol_unavailable(self): for attr in ("__array_struct__", "__array_interface__"): self.assertRaises(AttributeError, getattr, self.q, attr) @@ -1067,14 +1053,32 @@ class TestNumpyUnclassified(TestNumpyMethods): def test_pad(self): # Tests reproduced with modification from NumPy documentation a = [1, 2, 3, 4, 5] * self.ureg.m + b = self.Q_([4.0, 6.0, 8.0, 9.0, -3.0], "degC") + + self.assertQuantityEqual( + np.pad(a, (2, 3), "constant"), [0, 0, 1, 2, 3, 4, 5, 0, 0, 0] * self.ureg.m, + ) + self.assertQuantityEqual( + np.pad(a, (2, 3), "constant", constant_values=(0, 600 * self.ureg.cm)), + [0, 0, 1, 2, 3, 4, 5, 6, 6, 6] * self.ureg.m, + ) self.assertQuantityEqual( - np.pad(a, (2, 3), "constant", constant_values=(4, 600 * self.ureg.cm)), - [4, 4, 1, 2, 3, 4, 5, 6, 6, 6] * self.ureg.m, + np.pad( + b, (2, 1), "constant", constant_values=(np.nan, self.Q_(10, "degC")) + ), + self.Q_([np.nan, np.nan, 4, 6, 8, 9, -3, 10], "degC"), + ) + self.assertRaises( + DimensionalityError, np.pad, a, (2, 3), "constant", constant_values=4 ) self.assertQuantityEqual( np.pad(a, (2, 3), "edge"), [1, 1, 1, 2, 3, 4, 5, 5, 5, 5] * self.ureg.m ) self.assertQuantityEqual( + np.pad(a, (2, 3), "linear_ramp"), + [0, 0, 1, 2, 3, 4, 5, 3, 1, 0] * self.ureg.m, + ) + self.assertQuantityEqual( np.pad(a, (2, 3), "linear_ramp", end_values=(5, -4) * self.ureg.m), [5, 3, 1, 2, 3, 4, 5, 2, -1, -4] * self.ureg.m, ) diff --git a/pint/testsuite/test_quantity.py b/pint/testsuite/test_quantity.py index fbfd773..51faf1a 100644 --- a/pint/testsuite/test_quantity.py +++ b/pint/testsuite/test_quantity.py @@ -6,7 +6,7 @@ import warnings from unittest.mock import patch from pint import DimensionalityError, OffsetUnitCalculusError, UnitRegistry -from pint.compat import BehaviorChangeWarning, np +from pint.compat import np from pint.testsuite import QuantityTestCase, helpers from pint.testsuite.parameterized import ParameterizedTestCase from pint.unit import UnitsContainer @@ -531,11 +531,8 @@ class TestQuantity(QuantityTestCase): iter(x) @helpers.requires_array_function_protocol() - @patch("pint.quantity.SKIP_ARRAY_FUNCTION_CHANGE_WARNING", False) - def test_array_function_warning_on_creation(self): - # Test that warning is raised on first creation, but not second - with self.assertWarns(BehaviorChangeWarning): - self.Q_([]) + def test_no_longer_array_function_warning_on_creation(self): + # Test that warning is no longer raised on first creation with warnings.catch_warnings(): warnings.filterwarnings("error") self.Q_([]) diff --git a/pint/testsuite/test_unit.py b/pint/testsuite/test_unit.py index 0b6c086..d7eb649 100644 --- a/pint/testsuite/test_unit.py +++ b/pint/testsuite/test_unit.py @@ -658,6 +658,47 @@ class TestRegistry(QuantityTestCase): self.assertEqual(ureg.parse_units(""), ureg.Unit("")) self.assertRaises(ValueError, ureg.parse_units, "2 * meter") + def test_parse_string_pattern(self): + ureg = self.ureg + self.assertEqual( + ureg.parse_pattern("10'11", r"{foot}'{inch}"), + [ureg.Quantity(10.0, "foot"), ureg.Quantity(11.0, "inch")], + ) + + def test_parse_string_pattern_no_preprocess(self): + """Were preprocessors enabled, this would be interpreted as 10*11, not + two separate units, and thus cause the parsing to fail""" + ureg = self.ureg + self.assertEqual( + ureg.parse_pattern("10 11", r"{kg} {lb}"), + [ureg.Quantity(10.0, "kilogram"), ureg.Quantity(11.0, "pound")], + ) + + def test_parse_pattern_many_results(self): + ureg = self.ureg + self.assertEqual( + ureg.parse_pattern( + "1.5kg or 2kg will be fine, if you do not have 3kg", + r"{kg}kg", + many=True, + ), + [ + [ureg.Quantity(1.5, "kilogram")], + [ureg.Quantity(2.0, "kilogram")], + [ureg.Quantity(3.0, "kilogram")], + ], + ) + + def test_parse_pattern_many_results_two_units(self): + ureg = self.ureg + self.assertEqual( + ureg.parse_pattern("10'10 or 10'11", "{foot}'{inch}", many=True), + [ + [ureg.Quantity(10.0, "foot"), ureg.Quantity(10.0, "inch")], + [ureg.Quantity(10.0, "foot"), ureg.Quantity(11.0, "inch")], + ], + ) + class TestCompatibleUnits(QuantityTestCase): FORCE_NDARRAY = False |