1
+ from pathlib import Path
2
+
1
3
import pytest
2
4
from lark import LarkError
3
5
from pint import UnitRegistry
12
14
from ucumvert .ucum_pint import find_ucum_codes_that_need_mapping
13
15
from ucumvert .xml_util import get_metric_units , get_non_metric_units
14
16
15
- ureg = UnitRegistry ()
16
-
17
17
18
18
def get_unit_atoms ():
19
19
"""List of all case-sensitive defined UCUM units in ucum-essence.xml"""
@@ -28,7 +28,7 @@ def test_find_ucum_codes_that_need_mapping():
28
28
29
29
30
30
def test_ucum_to_pint (ucum_parser , ureg_std ):
31
- expected_quantity = ureg ("kilogram" )
31
+ expected_quantity = ureg_std ("kilogram" )
32
32
parsed_data = ucum_parser .parse ("kg" )
33
33
result = UcumToPintTransformer (ureg = ureg_std ).transform (parsed_data )
34
34
assert result == expected_quantity
@@ -99,7 +99,11 @@ def test_ucum_all_unit_atoms_pint_vs_str(
99
99
assert ureg_ucumvert (result_str ) == expected_quantity
100
100
101
101
102
- def test_ucum_preprocessor (ureg_ucumvert ):
102
+ def test_ucum_preprocessor ():
103
+ # Don't use ureg_ucumvert from fixture here, because we want to modify it.
104
+ defdir = Path (__file__ ).resolve ().parents [1 ] / "src" / "ucumvert"
105
+ ureg_ucumvert = UnitRegistry ()
106
+ ureg_ucumvert .load_definitions (defdir / "pint_ucum_defs.txt" )
103
107
expected = ureg_ucumvert ("m*kg" )
104
108
ureg_ucumvert .preprocessors .append (ucum_preprocessor )
105
109
assert ureg_ucumvert ("m.kg" ) == expected
@@ -111,3 +115,14 @@ def test_ucum_unitregistry():
111
115
ureg = PintUcumRegistry ()
112
116
assert ureg .from_ucum ("m.kg" ) == ureg ("m*kg" )
113
117
assert ureg .from_ucum ("Cel" ) == ureg ("degC" )
118
+
119
+
120
+ def test_prefix_with_unit_that_is_also_a_prefix_issue24 (ucum_parser , ureg_ucumvert ):
121
+ parsed_data = ucum_parser .parse ("m[IU]/L" )
122
+
123
+ expected_quantity = ureg_ucumvert ("milliinternational_unit/liter" )
124
+ assert expected_quantity == UcumToPintTransformer ().transform (parsed_data )
125
+ assert expected_quantity == 10 ** - 3 * ureg_ucumvert ("[IU]/L" )
126
+
127
+ result_str = UcumToPintStrTransformer ().transform (parsed_data )
128
+ assert result_str == "(((m[IU]) / (L)))"
0 commit comments