Skip to content

Commit 4f09daa

Browse files
committed
Added tests for CfpintUnit.from_unit.
1 parent 51cb1b1 commit 4f09daa

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

lib/iris/tests/unit/common/mixin/test_pintunits.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
from datetime import datetime
22

3+
import cf_units
34
import numpy as np
5+
import pytest
46

7+
import iris.common.mixin
58
from iris.common.mixin import CfpintUnit
69

710

@@ -32,3 +35,87 @@ def test_calendar():
3235
# .. but it is cf_units behaviour + currently required for correct netcdf saving
3336
# it also means that calendar is not checked in unit/string eq (!!!)
3437
assert str(unit) == "days since 1970-01-01"
38+
39+
40+
_UNKNOWN_NAMES = iris.common.mixin.CfpintUnit._IRIS_EXTRA_CATEGORIES["unknown"]
41+
_NOUNIT_NAMES = iris.common.mixin.CfpintUnit._IRIS_EXTRA_CATEGORIES["no_unit"]
42+
43+
44+
class TestFromUnit:
45+
"""Test CfpintUnit creation from various sources."""
46+
47+
def test_none_unknown(self):
48+
unit = CfpintUnit.from_unit(None)
49+
assert unit.category == "unknown"
50+
assert unit.calendar is None
51+
assert unit == "unknown"
52+
53+
@pytest.mark.parametrize("name", _UNKNOWN_NAMES)
54+
def test_str_unknown(self, name):
55+
unit = CfpintUnit.from_unit(None)
56+
assert unit.category == "unknown"
57+
assert unit.calendar is None
58+
assert all(unit == form for form in _UNKNOWN_NAMES) # string equivalence
59+
60+
def test_cfunits_unknown(self):
61+
cfunit = cf_units.Unit(None)
62+
unit = CfpintUnit.from_unit(None)
63+
assert unit.is_unknown()
64+
65+
@pytest.mark.parametrize("name", _NOUNIT_NAMES)
66+
def test_str_nounit(self, name):
67+
unit = CfpintUnit.from_unit(name)
68+
assert unit.category == "no_unit"
69+
assert unit.calendar is None
70+
assert all(unit == form for form in _NOUNIT_NAMES) # string equivalence
71+
72+
def test_cfunits_nounit(self):
73+
cfunit = cf_units.Unit("no_unit")
74+
unit = CfpintUnit.from_unit(cfunit)
75+
assert unit.is_no_unit()
76+
77+
def test_str(self):
78+
unit = CfpintUnit.from_unit("m")
79+
assert unit == "metres" # string equivalence
80+
assert unit.calendar is None
81+
assert unit.category == "regular"
82+
83+
def test_cfunits(self):
84+
cfunit = cf_units.Unit("m")
85+
unit = CfpintUnit.from_unit(cfunit)
86+
assert unit == "metre"
87+
88+
def test_str_date(self):
89+
unit = CfpintUnit.from_unit("days since 1970-01-01")
90+
assert unit == "days since 1970-01-01"
91+
assert unit.category == "regular"
92+
assert unit.is_datelike()
93+
assert unit.calendar == "standard"
94+
95+
@pytest.mark.skip("from_unit does not support calendar (yet?)")
96+
def test_str_date_calendar(self):
97+
unit = CfpintUnit.from_unit("days since 1970-01-01", calendar="360_day")
98+
# YUCK!! cf_units compatibility
99+
# TODO: this needs to change
100+
assert unit == "days since 1970-01-01"
101+
assert unit.category == "regular"
102+
assert unit.is_datelike()
103+
assert unit.calendar == "360_day"
104+
105+
def test_cfunits_date(self):
106+
cfunit = cf_units.Unit("hours since 1800-03-09 11:11")
107+
unit = CfpintUnit.from_unit(cfunit)
108+
# NB time ref is reproduced as-is
109+
# TODO: should get normalised
110+
assert unit == "hours since 1800-03-09 11:11"
111+
assert unit.is_datelike()
112+
assert unit.calendar == "standard"
113+
114+
def test_cfunits_date_calendar(self):
115+
cfunit = cf_units.Unit("hours since 1800-03-09 11:11", calendar="365_day")
116+
unit = CfpintUnit.from_unit(cfunit)
117+
# NB time ref is reproduced as-is
118+
# TODO: should get normalised
119+
assert unit == "hours since 1800-03-09 11:11"
120+
assert unit.is_datelike()
121+
assert unit.calendar == "365_day"

requirements/py313.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ dependencies:
1919
- matplotlib-base >=3.5, !=3.9.1
2020
- netcdf4
2121
- numpy >=1.24, !=1.24.3
22+
- pint
2223
- python-xxhash
2324
- pyproj
2425
- scipy

0 commit comments

Comments
 (0)