Skip to content

Commit 6da7f73

Browse files
authored
Merge pull request #34 from DocOtak/rcaneill-issue33
Add test on units
2 parents 480e316 + 0105e05 commit 6da7f73

File tree

5 files changed

+74
-5
lines changed

5 files changed

+74
-5
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,16 @@ jobs:
4545
uses: actions/setup-python@v2
4646
with:
4747
python-version: ${{ matrix.python-version }}
48+
- name: Install udunits
49+
run: sudo apt-get install -y libudunits2-dev
4850
- name: Run image
4951
uses: abatilo/[email protected]
5052
with:
5153
poetry-version: ${{ matrix.poetry-version }}
5254
- name: Install dependencies
5355
run: |
5456
poetry install
57+
poetry run pip install cf_units==3.0.1
5558
- name: Test with pytest
5659
run: |
5760
poetry run pytest gsw_xarray/tests

gsw_xarray/_attributes.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@
308308
},
309309
"enthalpy_second_derivatives": (
310310
{
311-
"units": "(J/kg)(g/kg)^-2)",
311+
"units": "(J/kg)(g/kg)^-2",
312312
},
313313
{
314314
"units": "J/(kg K(g/kg))",
@@ -319,7 +319,7 @@
319319
),
320320
"enthalpy_second_derivatives_CT_exact": (
321321
{
322-
"units": "(J/kg)(g/kg)^-2)",
322+
"units": "(J/kg)(g/kg)^-2",
323323
},
324324
{
325325
"units": "J/(kg K(g/kg))",
@@ -690,7 +690,7 @@
690690
"units": "(kg/m^3)(g/kg)^-2", # matlab doc is uncorrect
691691
},
692692
{
693-
"units": "(kg/m^3)(g/kg)^-1 (J/kg)^-1",
693+
"units": "(kg/m^3)(g/kg)^-1 J^-1/kg^-1",
694694
},
695695
{
696696
"units": "(kg/m^3)(J/kg)^-2",
@@ -800,7 +800,7 @@
800800
"units": "(m^3/kg)(g/kg)^-2", # error in doc?
801801
},
802802
{
803-
"units": "(m^3/kg)(g/kg)^-1 (J/kg)^-1",
803+
"units": "(m^3/kg)(g/kg)^-1 J^-1/kg^-1",
804804
},
805805
{
806806
"units": "(m^3/kg)(J/kg)^-2",

gsw_xarray/tests/test_units.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
Testing units with pint and cf_units
3+
"""
4+
import pytest
5+
6+
from .test_imports import gsw_base
7+
from gsw_xarray._attributes import _func_attrs
8+
9+
from pint import UnitRegistry
10+
11+
ureg = UnitRegistry()
12+
13+
14+
@pytest.mark.parametrize("func_name", gsw_base)
15+
def test_unit_pint(func_name):
16+
if func_name in ["indexer", "match_args_return", "pchip_interp"]:
17+
# Internal gsw cookery or non wrapped functions
18+
return
19+
if func_name == "geostrophic_velocity":
20+
pytest.xfail(
21+
'geostrophic_velocity outputs "degree_north" and "degree_east" that are not compatible with pint'
22+
)
23+
attrs = _func_attrs[func_name]
24+
if isinstance(attrs, dict):
25+
attrs = [
26+
attrs,
27+
]
28+
for a in attrs:
29+
print(a["units"])
30+
ureg.Unit(a["units"])
31+
32+
@pytest.mark.parametrize("func_name", gsw_base)
33+
def test_unit_cf_units(func_name):
34+
cf_units = pytest.importorskip("cf_units")
35+
if func_name in ["indexer", "match_args_return", "pchip_interp"]:
36+
# Internal gsw cookery or non wrapped functions
37+
return
38+
attrs = _func_attrs[func_name]
39+
if isinstance(attrs, dict):
40+
attrs = [
41+
attrs,
42+
]
43+
for a in attrs:
44+
print(a["units"])
45+
cf_units.Unit(a["units"])

poetry.lock

Lines changed: 21 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ furo = {version = ">=2022.1.2", optional = true}
1414

1515
[tool.poetry.dev-dependencies]
1616
pytest = ">=6.2.5"
17+
Pint = ">=0.18"
1718

1819
[tool.poetry.extras]
1920
docs = ["Sphinx", "furo"]

0 commit comments

Comments
 (0)