Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

skip babel tests if locales aren't installed #2022

Merged
merged 2 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading