28
28
requires_scipy_or_netCDF4 , requires_dask , requires_h5netcdf ,
29
29
requires_pynio , requires_pathlib , has_netCDF4 , has_scipy ,
30
30
assert_allclose , flaky , network , requires_rasterio ,
31
- assert_identical )
31
+ assert_identical , raises_regex )
32
32
from .test_dataset import create_test_data
33
33
34
34
from xarray .tests import mock
@@ -113,7 +113,7 @@ def __getitem__(self, key):
113
113
return self .array [key ]
114
114
115
115
array = UnreliableArray ([0 ])
116
- with self . assertRaises (UnreliableArrayFailure ):
116
+ with pytest . raises (UnreliableArrayFailure ):
117
117
array [0 ]
118
118
self .assertEqual (array [0 ], 0 )
119
119
@@ -218,7 +218,7 @@ def assert_loads(vars=None):
218
218
self .assertTrue (v ._in_memory )
219
219
self .assertDatasetIdentical (expected , actual )
220
220
221
- with self . assertRaises (AssertionError ):
221
+ with pytest . raises (AssertionError ):
222
222
# make sure the contextmanager works!
223
223
with assert_loads () as ds :
224
224
pass
@@ -345,8 +345,7 @@ def test_roundtrip_datetime_data(self):
345
345
kwds = {'encoding' : {'t0' : {'units' : 'days since 1950-01-01' }}}
346
346
with self .roundtrip (expected , save_kwargs = kwds ) as actual :
347
347
self .assertDatasetIdentical (expected , actual )
348
- self .assertEquals (actual .t0 .encoding ['units' ],
349
- 'days since 1950-01-01' )
348
+ assert actual .t0 .encoding ['units' ] == 'days since 1950-01-01'
350
349
351
350
def test_roundtrip_timedelta_data (self ):
352
351
time_deltas = pd .to_timedelta (['1h' , '2h' , 'NaT' ])
@@ -528,7 +527,7 @@ def test_roundtrip_endian(self):
528
527
529
528
if type (self ) is NetCDF4DataTest :
530
529
ds ['z' ].encoding ['endian' ] = 'big'
531
- with self . assertRaises (NotImplementedError ):
530
+ with pytest . raises (NotImplementedError ):
532
531
with self .roundtrip (ds ) as actual :
533
532
pass
534
533
@@ -539,7 +538,7 @@ def test_invalid_dataarray_names_raise(self):
539
538
da = xr .DataArray (data )
540
539
for name , e in zip ([0 , (4 , 5 ), True , '' ], [te , te , te , ve ]):
541
540
ds = Dataset ({name : da })
542
- with self . assertRaisesRegexp (* e ):
541
+ with raises_regex (* e ):
543
542
with self .roundtrip (ds ) as actual :
544
543
pass
545
544
@@ -551,17 +550,17 @@ def test_encoding_kwarg(self):
551
550
self .assertEqual (ds .x .encoding , {})
552
551
553
552
kwargs = dict (encoding = {'x' : {'foo' : 'bar' }})
554
- with self . assertRaisesRegexp (ValueError , 'unexpected encoding' ):
553
+ with raises_regex (ValueError , 'unexpected encoding' ):
555
554
with self .roundtrip (ds , save_kwargs = kwargs ) as actual :
556
555
pass
557
556
558
557
kwargs = dict (encoding = {'x' : 'foo' })
559
- with self . assertRaisesRegexp (ValueError , 'must be castable' ):
558
+ with raises_regex (ValueError , 'must be castable' ):
560
559
with self .roundtrip (ds , save_kwargs = kwargs ) as actual :
561
560
pass
562
561
563
562
kwargs = dict (encoding = {'invalid' : {}})
564
- with self . assertRaises (KeyError ):
563
+ with pytest . raises (KeyError ):
565
564
with self .roundtrip (ds , save_kwargs = kwargs ) as actual :
566
565
pass
567
566
@@ -676,9 +675,9 @@ def test_open_group(self):
676
675
self .assertVariableEqual (actual ['x' ], expected ['x' ])
677
676
678
677
# check that missing group raises appropriate exception
679
- with self . assertRaises (IOError ):
678
+ with pytest . raises (IOError ):
680
679
open_dataset (tmp_file , group = 'bar' )
681
- with self . assertRaisesRegexp (ValueError , 'must be a string' ):
680
+ with raises_regex (ValueError , 'must be a string' ):
682
681
open_dataset (tmp_file , group = (1 , 2 , 3 ))
683
682
684
683
def test_open_subgroup (self ):
@@ -1019,7 +1018,7 @@ def create_store(self):
1019
1018
1020
1019
def test_array_attrs (self ):
1021
1020
ds = Dataset (attrs = {'foo' : [[1 , 2 ], [3 , 4 ]]})
1022
- with self . assertRaisesRegexp (ValueError , 'must be 1-dimensional' ):
1021
+ with raises_regex (ValueError , 'must be 1-dimensional' ):
1023
1022
with self .roundtrip (ds ) as roundtripped :
1024
1023
pass
1025
1024
@@ -1036,11 +1035,11 @@ def test_netcdf3_endianness(self):
1036
1035
1037
1036
@requires_netCDF4
1038
1037
def test_nc4_scipy (self ):
1039
- with create_tmp_file () as tmp_file :
1038
+ with create_tmp_file (allow_cleanup_failure = True ) as tmp_file :
1040
1039
with nc4 .Dataset (tmp_file , 'w' , format = 'NETCDF4' ) as rootgrp :
1041
1040
rootgrp .createGroup ('foo' )
1042
1041
1043
- with self . assertRaisesRegexp (TypeError , 'pip install netcdf4' ):
1042
+ with raises_regex (TypeError , 'pip install netcdf4' ):
1044
1043
open_dataset (tmp_file , engine = 'scipy' )
1045
1044
1046
1045
@@ -1096,18 +1095,18 @@ def test_write_store(self):
1096
1095
1097
1096
def test_engine (self ):
1098
1097
data = create_test_data ()
1099
- with self . assertRaisesRegexp (ValueError , 'unrecognized engine' ):
1098
+ with raises_regex (ValueError , 'unrecognized engine' ):
1100
1099
data .to_netcdf ('foo.nc' , engine = 'foobar' )
1101
- with self . assertRaisesRegexp (ValueError , 'invalid engine' ):
1100
+ with raises_regex (ValueError , 'invalid engine' ):
1102
1101
data .to_netcdf (engine = 'netcdf4' )
1103
1102
1104
1103
with create_tmp_file () as tmp_file :
1105
1104
data .to_netcdf (tmp_file )
1106
- with self . assertRaisesRegexp (ValueError , 'unrecognized engine' ):
1105
+ with raises_regex (ValueError , 'unrecognized engine' ):
1107
1106
open_dataset (tmp_file , engine = 'foobar' )
1108
1107
1109
1108
netcdf_bytes = data .to_netcdf ()
1110
- with self . assertRaisesRegexp (ValueError , 'can only read' ):
1109
+ with raises_regex (ValueError , 'can only read' ):
1111
1110
open_dataset (BytesIO (netcdf_bytes ), engine = 'foobar' )
1112
1111
1113
1112
def test_cross_engine_read_write_netcdf3 (self ):
@@ -1389,12 +1388,12 @@ def test_common_coord_when_datavars_minimal(self):
1389
1388
def test_invalid_data_vars_value_should_fail (self ):
1390
1389
1391
1390
with self .setup_files_and_datasets () as (files , _ ):
1392
- with self . assertRaises (ValueError ):
1391
+ with pytest . raises (ValueError ):
1393
1392
with open_mfdataset (files , data_vars = 'minimum' ):
1394
1393
pass
1395
1394
1396
1395
# test invalid coord parameter
1397
- with self . assertRaises (ValueError ):
1396
+ with pytest . raises (ValueError ):
1398
1397
with open_mfdataset (files , coords = 'minimum' ):
1399
1398
pass
1400
1399
@@ -1452,7 +1451,7 @@ def test_open_mfdataset(self):
1452
1451
self .assertEqual (actual .foo .variable .data .chunks ,
1453
1452
((3 , 2 , 3 , 2 ),))
1454
1453
1455
- with self . assertRaisesRegexp (IOError , 'no files to open' ):
1454
+ with raises_regex (IOError , 'no files to open' ):
1456
1455
open_mfdataset ('foo-bar-baz-*.nc' , autoclose = self .autoclose )
1457
1456
1458
1457
@requires_pathlib
@@ -1483,7 +1482,7 @@ def test_attrs_mfdataset(self):
1483
1482
# first dataset loaded
1484
1483
self .assertEqual (actual .test1 , ds1 .test1 )
1485
1484
# attributes from ds2 are not retained, e.g.,
1486
- with self . assertRaisesRegexp (AttributeError ,
1485
+ with raises_regex (AttributeError ,
1487
1486
'no attribute' ):
1488
1487
actual .test2
1489
1488
@@ -1513,15 +1512,15 @@ def test_save_mfdataset_roundtrip(self):
1513
1512
1514
1513
def test_save_mfdataset_invalid (self ):
1515
1514
ds = Dataset ()
1516
- with self . assertRaisesRegexp (ValueError , 'cannot use mode' ):
1515
+ with raises_regex (ValueError , 'cannot use mode' ):
1517
1516
save_mfdataset ([ds , ds ], ['same' , 'same' ])
1518
- with self . assertRaisesRegexp (ValueError , 'same length' ):
1517
+ with raises_regex (ValueError , 'same length' ):
1519
1518
save_mfdataset ([ds , ds ], ['only one path' ])
1520
1519
1521
1520
def test_save_mfdataset_invalid_dataarray (self ):
1522
1521
# regression test for GH1555
1523
1522
da = DataArray ([1 , 2 ])
1524
- with self . assertRaisesRegexp (TypeError , 'supports writing Dataset' ):
1523
+ with raises_regex (TypeError , 'supports writing Dataset' ):
1525
1524
save_mfdataset ([da ], ['dataarray' ])
1526
1525
1527
1526
@@ -1846,11 +1845,11 @@ def test_indexing(self):
1846
1845
# but on x and y only windowed operations are allowed, more
1847
1846
# exotic slicing should raise an error
1848
1847
err_msg = 'not valid on rasterio'
1849
- with self . assertRaisesRegexp (IndexError , err_msg ):
1848
+ with raises_regex (IndexError , err_msg ):
1850
1849
actual .isel (x = [2 , 4 ], y = [1 , 3 ]).values
1851
- with self . assertRaisesRegexp (IndexError , err_msg ):
1850
+ with raises_regex (IndexError , err_msg ):
1852
1851
actual .isel (x = [4 , 2 ]).values
1853
- with self . assertRaisesRegexp (IndexError , err_msg ):
1852
+ with raises_regex (IndexError , err_msg ):
1854
1853
actual .isel (x = slice (5 , 2 , - 1 )).values
1855
1854
1856
1855
# Integer indexing
@@ -1916,7 +1915,7 @@ def test_caching(self):
1916
1915
1917
1916
# Without cache an error is raised
1918
1917
err_msg = 'not valid on rasterio'
1919
- with self . assertRaisesRegexp (IndexError , err_msg ):
1918
+ with raises_regex (IndexError , err_msg ):
1920
1919
actual .isel (x = [2 , 4 ]).values
1921
1920
1922
1921
# This should cache everything
@@ -1976,7 +1975,7 @@ class TestEncodingInvalid(TestCase):
1976
1975
1977
1976
def test_extract_nc4_variable_encoding (self ):
1978
1977
var = xr .Variable (('x' ,), [1 , 2 , 3 ], {}, {'foo' : 'bar' })
1979
- with self . assertRaisesRegexp (ValueError , 'unexpected encoding' ):
1978
+ with raises_regex (ValueError , 'unexpected encoding' ):
1980
1979
_extract_nc4_variable_encoding (var , raise_on_invalid = True )
1981
1980
1982
1981
var = xr .Variable (('x' ,), [1 , 2 , 3 ], {}, {'chunking' : (2 , 1 )})
@@ -1992,7 +1991,7 @@ def test_extract_h5nc_encoding(self):
1992
1991
# not supported with h5netcdf (yet)
1993
1992
var = xr .Variable (('x' ,), [1 , 2 , 3 ], {},
1994
1993
{'least_sigificant_digit' : 2 })
1995
- with self . assertRaisesRegexp (ValueError , 'unexpected encoding' ):
1994
+ with raises_regex (ValueError , 'unexpected encoding' ):
1996
1995
_extract_nc4_variable_encoding (var , raise_on_invalid = True )
1997
1996
1998
1997
@@ -2025,17 +2024,17 @@ def new_dataset_and_coord_attrs():
2025
2024
ds , attrs = new_dataset_and_attrs ()
2026
2025
2027
2026
attrs [123 ] = 'test'
2028
- with self . assertRaisesRegexp (TypeError , 'Invalid name for attr' ):
2027
+ with raises_regex (TypeError , 'Invalid name for attr' ):
2029
2028
ds .to_netcdf ('test.nc' )
2030
2029
2031
2030
ds , attrs = new_dataset_and_attrs ()
2032
2031
attrs [MiscObject ()] = 'test'
2033
- with self . assertRaisesRegexp (TypeError , 'Invalid name for attr' ):
2032
+ with raises_regex (TypeError , 'Invalid name for attr' ):
2034
2033
ds .to_netcdf ('test.nc' )
2035
2034
2036
2035
ds , attrs = new_dataset_and_attrs ()
2037
2036
attrs ['' ] = 'test'
2038
- with self . assertRaisesRegexp (ValueError , 'Invalid name for attr' ):
2037
+ with raises_regex (ValueError , 'Invalid name for attr' ):
2039
2038
ds .to_netcdf ('test.nc' )
2040
2039
2041
2040
# This one should work
@@ -2046,12 +2045,12 @@ def new_dataset_and_coord_attrs():
2046
2045
2047
2046
ds , attrs = new_dataset_and_attrs ()
2048
2047
attrs ['test' ] = {'a' : 5 }
2049
- with self . assertRaisesRegexp (TypeError , 'Invalid value for attr' ):
2048
+ with raises_regex (TypeError , 'Invalid value for attr' ):
2050
2049
ds .to_netcdf ('test.nc' )
2051
2050
2052
2051
ds , attrs = new_dataset_and_attrs ()
2053
2052
attrs ['test' ] = MiscObject ()
2054
- with self . assertRaisesRegexp (TypeError , 'Invalid value for attr' ):
2053
+ with raises_regex (TypeError , 'Invalid value for attr' ):
2055
2054
ds .to_netcdf ('test.nc' )
2056
2055
2057
2056
ds , attrs = new_dataset_and_attrs ()
0 commit comments