Skip to content

Commit

Permalink
skip babel tests if locales aren't installed (#2022)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgsavage committed Jun 21, 2024
1 parent 88e2e80 commit 9014d14
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
23 changes: 20 additions & 3 deletions pint/testsuite/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,26 @@ def requires_numpy_at_least(version):
)


requires_babel = pytest.mark.skipif(
not HAS_BABEL, reason="Requires Babel with units support"
)
def requires_babel(tested_locales=[]):
if not HAS_BABEL:
return pytest.mark.skip("Requires Babel with units support")

import locale

default_locale = locale.getlocale(locale.LC_NUMERIC)
locales_unavailable = False
try:
for loc in tested_locales:
locale.setlocale(locale.LC_NUMERIC, loc)
except locale.Error:
locales_unavailable = True
locale.setlocale(locale.LC_NUMERIC, default_locale)

return pytest.mark.skipif(
locales_unavailable, reason="Tested locales not available."
)


requires_not_babel = pytest.mark.skipif(
HAS_BABEL, reason="Requires Babel not to be installed"
)
Expand Down
8 changes: 4 additions & 4 deletions pint/testsuite/test_babel.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_no_babel(func_registry):
distance.format_babel(locale="fr_FR", length="long")


@helpers.requires_babel()
@helpers.requires_babel(["fr_FR", "ro_RO"])
def test_format(func_registry):
ureg = func_registry
dirname = os.path.dirname(__file__)
Expand All @@ -36,7 +36,7 @@ def test_format(func_registry):
assert mks.format_babel(locale="fr_FR") == "métrique"


@helpers.requires_babel()
@helpers.requires_babel(["fr_FR", "ro_RO"])
def test_registry_locale():
ureg = UnitRegistry(fmt_locale="fr_FR")
dirname = os.path.dirname(__file__)
Expand All @@ -60,7 +60,7 @@ def test_registry_locale():
assert mks.format_babel(locale="fr_FR") == "métrique"


@helpers.requires_babel()
@helpers.requires_babel(["fr_FR"])
def test_unit_format_babel():
ureg = UnitRegistry(fmt_locale="fr_FR")
volume = ureg.Unit("ml")
Expand All @@ -85,7 +85,7 @@ def test_no_registry_locale(func_registry):
distance.format_babel()


@helpers.requires_babel()
@helpers.requires_babel(["fr_FR"])
def test_str(func_registry):
ureg = func_registry
d = 24.1 * ureg.meter
Expand Down
2 changes: 1 addition & 1 deletion pint/testsuite/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ def test_issue1674(self, module_registry):
arr_of_q * q_arr, np.array([Q_(2, "m^2"), Q_(8, "m s")], dtype="object")
)

@helpers.requires_babel()
@helpers.requires_babel(["es_ES"])
def test_issue_1400(self, sess_registry):
q1 = 3.1 * sess_registry.W
q2 = 3.1 * sess_registry.W / sess_registry.cm
Expand Down

0 comments on commit 9014d14

Please sign in to comment.