-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
expose testing functions as public API (#24)
* add a assert_units_equal function * expose the testing module * set a fallback version * document the testing functions * add a docstring * Update pint_xarray/testing.py Co-authored-by: Jon Thielen <[email protected]> * Update pint_xarray/testing.py Co-authored-by: keewis <[email protected]> Co-authored-by: Jon Thielen <[email protected]>
- Loading branch information
Showing
5 changed files
with
104 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from . import conversion | ||
|
||
|
||
def assert_units_equal(a, b): | ||
""" assert that the units of two xarray objects are equal | ||
Raises an :py:exc:`AssertionError` if the units of both objects are not | ||
equal. ``units`` attributes and attached unit objects are compared | ||
separately. | ||
Parameters | ||
---------- | ||
a, b : DataArray or Dataset | ||
The objects to compare | ||
""" | ||
|
||
__tracebackhide__ = True | ||
|
||
assert conversion.extract_units(a) == conversion.extract_units(b) | ||
assert conversion.extract_unit_attributes(a) == conversion.extract_unit_attributes( | ||
b | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import pint | ||
import pytest | ||
import xarray as xr | ||
|
||
from pint_xarray import testing | ||
|
||
unit_registry = pint.UnitRegistry(force_ndarray_like=True) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("a", "b", "error"), | ||
( | ||
pytest.param( | ||
xr.DataArray(attrs={"units": "K"}), | ||
xr.DataArray(attrs={"units": "K"}), | ||
None, | ||
id="equal attrs", | ||
), | ||
pytest.param( | ||
xr.DataArray(attrs={"units": "m"}), | ||
xr.DataArray(attrs={"units": "K"}), | ||
AssertionError, | ||
id="different attrs", | ||
), | ||
pytest.param( | ||
xr.DataArray([10, 20] * unit_registry.K), | ||
xr.DataArray([50, 80] * unit_registry.K), | ||
None, | ||
id="equal units", | ||
), | ||
pytest.param( | ||
xr.DataArray([10, 20] * unit_registry.K), | ||
xr.DataArray([50, 80] * unit_registry.dimensionless), | ||
AssertionError, | ||
id="different units", | ||
), | ||
pytest.param( | ||
xr.Dataset({"a": ("x", [0, 10], {"units": "K"})}), | ||
xr.Dataset({"a": ("x", [20, 40], {"units": "K"})}), | ||
None, | ||
id="matching variables", | ||
), | ||
pytest.param( | ||
xr.Dataset({"a": ("x", [0, 10], {"units": "K"})}), | ||
xr.Dataset({"b": ("x", [20, 40], {"units": "K"})}), | ||
AssertionError, | ||
id="mismatching variables", | ||
), | ||
), | ||
) | ||
def test_assert_units_equal(a, b, error): | ||
if error is not None: | ||
with pytest.raises(error): | ||
testing.assert_units_equal(a, b) | ||
|
||
return | ||
|
||
testing.assert_units_equal(a, b) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters