Skip to content

Commit

Permalink
Avoid for-loops within unit formatter tests
Browse files Browse the repository at this point in the history
It is much better to parametrize tests instead because a failure of one
parametrized test does not prevent others from running, and it is much
simpler to assign good ids.
  • Loading branch information
eerovaher committed Jul 2, 2024
1 parent ba7a3a4 commit 172936c
Showing 1 changed file with 39 additions and 21 deletions.
60 changes: 39 additions & 21 deletions astropy/units/tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
Regression tests for the units.format package
"""

from __future__ import annotations

import warnings
from contextlib import nullcontext
from fractions import Fraction
from typing import TYPE_CHECKING, NamedTuple

import numpy as np
import pytest
Expand All @@ -18,10 +21,28 @@
from astropy.units.utils import is_effectively_unity
from astropy.utils.exceptions import AstropyDeprecationWarning

if TYPE_CHECKING:
from collections.abc import Iterable


class StringUnitPair(NamedTuple):
string: str
unit: UnitBase


def list_string_unit_pairs(
*test_cases: tuple[Iterable[str], UnitBase],
) -> list[StringUnitPair]:
return [
StringUnitPair(string, unit)
for strings, unit in test_cases
for string in strings
]


@pytest.mark.parametrize(
"strings, unit",
[
"test_pair",
list_string_unit_pairs(
(["m s", "m*s", "m.s"], u.m * u.s),
(["m/s", "m*s**-1", "m /s", "m / s", "m/ s"], u.m / u.s),
(["m**2", "m2", "m**(2)", "m**+2", "m+2", "m^(+2)"], u.m**2),
Expand All @@ -37,12 +58,11 @@
(["mag(ct/s)"], u.MagUnit(u.ct / u.s)),
(["dex"], u.dex),
(["dex(cm s**-2)", "dex(cm/s2)"], u.DexUnit(u.cm / u.s**2)),
],
),
ids=lambda x: x.string,
)
def test_unit_grammar(strings, unit):
for s in strings:
unit2 = u_format.Generic.parse(s)
assert unit2 == unit
def test_unit_grammar(test_pair: StringUnitPair):
assert u_format.Generic.parse(test_pair.string) == test_pair.unit


@pytest.mark.parametrize(
Expand All @@ -54,8 +74,8 @@ def test_unit_grammar_fail(string):


@pytest.mark.parametrize(
"strings, unit",
[
"test_pair",
list_string_unit_pairs(
(["0.1nm"], u.AA),
(["mW/m2"], u.Unit(u.erg / u.cm**2 / u.s)),
(["mW/(m2)"], u.Unit(u.erg / u.cm**2 / u.s)),
Expand Down Expand Up @@ -94,12 +114,11 @@ def test_unit_grammar_fail(string):
(["[cm/s2]"], dex(u.cm / u.s**2)),
(["[K]"], dex(u.K)),
(["[-]"], dex(u.dimensionless_unscaled)),
],
),
ids=lambda x: x.string,
)
def test_cds_grammar(strings, unit):
for s in strings:
unit2 = u_format.CDS.parse(s)
assert unit2 == unit
def test_cds_grammar(test_pair: StringUnitPair):
assert u_format.CDS.parse(test_pair.string) == test_pair.unit


@pytest.mark.parametrize(
Expand Down Expand Up @@ -147,8 +166,8 @@ def test_cds_log10_dimensionless():
# These examples are taken from the EXAMPLES section of
# https://heasarc.gsfc.nasa.gov/docs/heasarc/ofwg/docs/general/ogip_93_001/
@pytest.mark.parametrize(
"strings, unit",
[
"test_pair",
list_string_unit_pairs(
(
["count /s", "count/s", "count s**(-1)", "count / s", "count /s "],
u.count / u.s,
Expand Down Expand Up @@ -213,12 +232,11 @@ def test_cds_log10_dimensionless():
],
(u.count / u.s) * (1.0 / (u.pixel * u.s)),
),
],
),
ids=lambda x: x.string,
)
def test_ogip_grammar(strings, unit):
for s in strings:
unit2 = u_format.OGIP.parse(s)
assert unit2 == unit
def test_ogip_grammar(test_pair: StringUnitPair):
assert u_format.OGIP.parse(test_pair.string) == test_pair.unit


@pytest.mark.parametrize(
Expand Down

0 comments on commit 172936c

Please sign in to comment.