8
8
9
9
from pint import UnitRegistry
10
10
from pint .unit import Unit
11
- from pint .errors import DimensionalityError
11
+ from pint .errors import UndefinedUnitError , DimensionalityError
12
12
13
13
from pintxarray .accessors import PintDataArrayAccessor , PintDatasetAccessor
14
- from .utils import extract_units
14
+ from .utils import extract_units , raises_regex
15
15
16
16
17
17
# make sure scalars are converted to 0d arrays so quantities can
@@ -37,44 +37,70 @@ def example_quantity_da():
37
37
38
38
39
39
class TestQuantifyDataArray :
40
- @pytest .mark .skip (reason = "Not yet implemented" )
41
- def test_attach_units_given_registry (self ):
42
- ...
40
+ def test_attach_units_given_registry (self , example_unitless_da ):
41
+ orig = example_unitless_da
42
+ ureg = UnitRegistry (force_ndarray = True )
43
+ result = orig .pint .quantify ('m' , unit_registry = ureg )
44
+ assert_array_equal (result .data .magnitude , orig .data )
45
+ assert result .data .units == ureg .Unit ('m' )
43
46
44
47
def test_attach_units (self , example_unitless_da ):
45
48
orig = example_unitless_da
46
49
result = orig .pint .quantify ('m' )
47
50
assert_array_equal (result .data .magnitude , orig .data )
51
+ # TODO better comparisons for when you can't access the unit_registry?
48
52
assert str (result .data .units ) == 'meter'
49
53
50
- def test_attach_units_from_attrs (self ):
51
- ...
54
+ def test_attach_units_from_attrs (self , example_unitless_da ):
55
+ orig = example_unitless_da
56
+ result = orig .pint .quantify ()
57
+ assert_array_equal (result .data .magnitude , orig .data )
58
+ assert str (result .data .units ) == 'meter'
52
59
53
- def test_attach_unit_class (self ):
54
- ...
60
+ def test_attach_unit_class (self , example_unitless_da ):
61
+ orig = example_unitless_da
62
+ ureg = UnitRegistry (force_ndarray = True )
63
+ result = orig .pint .quantify (ureg .Unit ('m' ), unit_registry = ureg )
64
+ assert_array_equal (result .data .magnitude , orig .data )
65
+ assert result .data .units == ureg .Unit ('m' )
55
66
56
67
def test_error_when_already_units (self , example_quantity_da ):
57
68
da = example_quantity_da
58
- with pytest . raises (ValueError ):
69
+ with raises_regex (ValueError , "already has units" ):
59
70
da .pint .quantify ()
60
71
61
- def test_error_on_nonsense_units (self ):
62
- ...
72
+ def test_error_on_nonsense_units (self , example_unitless_da ):
73
+ da = example_unitless_da
74
+ with pytest .raises (UndefinedUnitError ):
75
+ da .pint .quantify (units = 'aecjhbav' )
63
76
64
- def test_registry_kwargs (self ):
65
- ...
77
+ def test_registry_kwargs (self , example_unitless_da ):
78
+ orig = example_unitless_da
79
+ result = orig .pint .quantify (registry_kwargs =
80
+ {'auto_reduce_dimensions' : True })
81
+ assert (result .data ._REGISTRY .auto_reduce_dimensions ) == True
66
82
67
83
68
- @pytest .mark .skip (reason = "Not yet implemented" )
69
84
class TestDequantifyDataArray :
70
- def test_strip_units (self ):
71
- ...
85
+ def test_strip_units (self , example_quantity_da ):
86
+ result = example_quantity_da .pint .dequantify ()
87
+ assert isinstance (result .data , np .ndarray )
88
+ assert isinstance (result .coords ['x' ].data , np .ndarray )
72
89
73
- def test_error_if_no_units (self ):
74
- ...
90
+ def test_error_if_no_units (self , example_unitless_da ):
91
+ with raises_regex (ValueError , "does not have units" ):
92
+ example_unitless_da .pint .dequantify ()
75
93
76
- def test_roundtrip_attrs (self ):
77
- ...
94
+ def test_attrs_reinstated (self , example_quantity_da ):
95
+ da = example_quantity_da
96
+ result = da .pint .dequantify ()
97
+ assert result .attrs ['units' ] == 'meter'
98
+
99
+ def test_roundtrip_data (self , example_unitless_da ):
100
+ orig = example_unitless_da
101
+ quantified = orig .pint .quantify ()
102
+ result = quantified .pint .dequantify ()
103
+ assert_equal (result , orig )
78
104
79
105
80
106
@pytest .mark .skip (reason = "Not yet implemented" )
0 commit comments