11
11
assert_equal ,
12
12
assert_identical ,
13
13
assert_units_equal ,
14
- raises_regex ,
15
14
requires_bottleneck ,
16
15
requires_dask_array ,
17
16
requires_scipy ,
@@ -111,10 +110,62 @@ def test_override_units(self, example_unitless_da, no_unit_value):
111
110
with pytest .raises (AttributeError ):
112
111
result ["u" ].data .units
113
112
114
- def test_error_when_already_units (self , example_quantity_da ):
113
+ def test_error_when_changing_units (self , example_quantity_da ):
115
114
da = example_quantity_da
116
- with raises_regex (ValueError , "already has units" ):
117
- da .pint .quantify ()
115
+ with pytest .raises (ValueError , match = "already has units" ):
116
+ da .pint .quantify ("s" )
117
+
118
+ def test_attach_no_units (self ):
119
+ arr = xr .DataArray ([1 , 2 , 3 ], dims = "x" )
120
+ quantified = arr .pint .quantify ()
121
+ assert_identical (quantified , arr )
122
+ assert_units_equal (quantified , arr )
123
+
124
+ def test_attach_no_new_units (self ):
125
+ da = xr .DataArray (unit_registry .Quantity ([1 , 2 , 3 ], "m" ), dims = "x" )
126
+ quantified = da .pint .quantify ()
127
+ assert_identical (quantified , da )
128
+ assert_units_equal (quantified , da )
129
+
130
+ def test_attach_same_units (self ):
131
+ da = xr .DataArray (unit_registry .Quantity ([1 , 2 , 3 ], "m" ), dims = "x" )
132
+ quantified = da .pint .quantify ("m" )
133
+ assert_identical (quantified , da )
134
+ assert_units_equal (quantified , da )
135
+
136
+ def test_error_when_changing_units_dimension_coordinates (self ):
137
+ arr = xr .DataArray (
138
+ [1 , 2 , 3 ],
139
+ dims = "x" ,
140
+ coords = {"x" : ("x" , [- 1 , 0 , 1 ], {"units" : unit_registry .Unit ("m" )})},
141
+ )
142
+ with pytest .raises (ValueError , match = "already has units" ):
143
+ arr .pint .quantify ({"x" : "s" })
144
+
145
+ def test_dimension_coordinate_array (self ):
146
+ ds = xr .Dataset (coords = {"x" : ("x" , [10 ], {"units" : "m" })})
147
+ arr = ds .x
148
+
149
+ # does not actually quantify because `arr` wraps a IndexVariable
150
+ # but we still get a `Unit` in the attrs
151
+ q = arr .pint .quantify ()
152
+ assert isinstance (q .attrs ["units" ], Unit )
153
+
154
+ def test_dimension_coordinate_array_already_quantified (self ):
155
+ ds = xr .Dataset (coords = {"x" : ("x" , [10 ], {"units" : unit_registry .Unit ("m" )})})
156
+ arr = ds .x
157
+
158
+ with pytest .raises (ValueError ):
159
+ arr .pint .quantify ({"x" : "s" })
160
+
161
+ def test_dimension_coordinate_array_already_quantified_same_units (self ):
162
+ ds = xr .Dataset (coords = {"x" : ("x" , [10 ], {"units" : unit_registry .Unit ("m" )})})
163
+ arr = ds .x
164
+
165
+ quantified = arr .pint .quantify ({"x" : "m" })
166
+
167
+ assert_identical (quantified , arr )
168
+ assert_units_equal (quantified , arr )
118
169
119
170
def test_error_on_nonsense_units (self , example_unitless_da ):
120
171
da = example_unitless_da
@@ -135,22 +186,6 @@ def test_parse_integer_inverse(self):
135
186
result = da .pint .quantify ()
136
187
assert result .pint .units == Unit ("1 / meter" )
137
188
138
- def test_dimension_coordinate (self ):
139
- ds = xr .Dataset (coords = {"x" : ("x" , [10 ], {"units" : "m" })})
140
- arr = ds .x
141
-
142
- # does not actually quantify because `arr` wraps a IndexVariable
143
- # but we still get a `Unit` in the attrs
144
- q = arr .pint .quantify ()
145
- assert isinstance (q .attrs ["units" ], Unit )
146
-
147
- def test_dimension_coordinate_already_quantified (self ):
148
- ds = xr .Dataset (coords = {"x" : ("x" , [10 ], {"units" : unit_registry .Unit ("m" )})})
149
- arr = ds .x
150
-
151
- with pytest .raises (ValueError ):
152
- arr .pint .quantify ({"x" : "s" })
153
-
154
189
155
190
@pytest .mark .parametrize ("formatter" , ("" , "P" , "C" ))
156
191
@pytest .mark .parametrize ("modifier" , ("" , "~" ))
@@ -308,8 +343,35 @@ def test_override_units(self, example_unitless_ds, no_unit_value):
308
343
)
309
344
310
345
def test_error_when_already_units (self , example_quantity_ds ):
311
- with raises_regex (ValueError , "already has units" ):
312
- example_quantity_ds .pint .quantify ({"funds" : "pounds" })
346
+ with pytest .raises (ValueError , match = "already has units" ):
347
+ example_quantity_ds .pint .quantify ({"funds" : "kg" })
348
+
349
+ def test_attach_no_units (self ):
350
+ ds = xr .Dataset ({"a" : ("x" , [1 , 2 , 3 ])})
351
+ quantified = ds .pint .quantify ()
352
+ assert_identical (quantified , ds )
353
+ assert_units_equal (quantified , ds )
354
+
355
+ def test_attach_no_new_units (self ):
356
+ ds = xr .Dataset ({"a" : ("x" , unit_registry .Quantity ([1 , 2 , 3 ], "m" ))})
357
+ quantified = ds .pint .quantify ()
358
+
359
+ assert_identical (quantified , ds )
360
+ assert_units_equal (quantified , ds )
361
+
362
+ def test_attach_same_units (self ):
363
+ ds = xr .Dataset ({"a" : ("x" , unit_registry .Quantity ([1 , 2 , 3 ], "m" ))})
364
+ quantified = ds .pint .quantify ({"a" : "m" })
365
+
366
+ assert_identical (quantified , ds )
367
+ assert_units_equal (quantified , ds )
368
+
369
+ def test_error_when_changing_units_dimension_coordinates (self ):
370
+ ds = xr .Dataset (
371
+ coords = {"x" : ("x" , [- 1 , 0 , 1 ], {"units" : unit_registry .Unit ("m" )})},
372
+ )
373
+ with pytest .raises (ValueError , match = "already has units" ):
374
+ ds .pint .quantify ({"x" : "s" })
313
375
314
376
def test_error_on_nonsense_units (self , example_unitless_ds ):
315
377
ds = example_unitless_ds
0 commit comments