Skip to content

Commit d923b76

Browse files
authored
units: support for offset units, add tests (#693)
1 parent bd5c92e commit d923b76

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/pynxtools/units/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,6 @@ def is_valid_unit(unit: str):
167167
if ureg.Unit(unit) == ureg.Unit("pixel") and str(expected_dim) == "[length]":
168168
return True
169169

170-
actual_dim = (1 * ureg(unit)).dimensionality
170+
actual_dim = ureg.Quantity(1, unit).to_base_units().dimensionality
171171

172172
return actual_dim == expected_dim

tests/units/test_units.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import pytest
2+
3+
from pynxtools.units import NXUnitSet
4+
5+
6+
@pytest.mark.parametrize(
7+
"unit_category,unit,expected",
8+
[
9+
("NX_LENGTH", "meter", True),
10+
("NX_LENGTH", "m", True),
11+
("NX_LENGTH", "second", False),
12+
("NX_TEMPERATURE", "kelvin", True),
13+
("NX_TEMPERATURE", "celsius", True), # offset unit
14+
("NX_TEMPERATURE", "degC", True), # alias
15+
("NX_TEMPERATURE", "second", False),
16+
("NX_ANY", "meter", True),
17+
("NX_ANY", "", True), # empty allowed
18+
("NX_ANY", "foobar", False), # unknown unit not allowed for NX_ANY
19+
("NX_LENGTH", "foobar", False), # unknown unit not allowed
20+
("NX_LENGTH", "pixel", True), # pixel is accepted as length
21+
("NX_DIMENSIONLESS", "", True),
22+
("NX_DIMENSIONLESS", "meter", False),
23+
("NX_UNITLESS", "", True),
24+
("NX_UNITLESS", "meter", False),
25+
],
26+
)
27+
def test_matches(unit_category, unit, expected):
28+
assert NXUnitSet.matches(unit_category, unit) == expected

0 commit comments

Comments
 (0)