2
2
3
3
import os
4
4
from os .path import join as pjoin , dirname
5
- import shutil
6
- import tempfile
7
- import time
8
- import sys
9
5
from io import BytesIO
10
6
from glob import glob
11
7
from contextlib import contextmanager
12
8
13
9
import numpy as np
14
- from numpy .testing import dec , assert_
15
10
16
- from .. netcdf import netcdf_file
11
+ import pytest
17
12
18
- from nose . tools import assert_true , assert_false , assert_equal , assert_raises
13
+ from .. netcdf import netcdf_file
19
14
20
15
TEST_DATA_PATH = pjoin (dirname (__file__ ), 'data' )
21
16
@@ -36,54 +31,41 @@ def make_simple(*args, **kwargs):
36
31
f .close ()
37
32
38
33
39
- def gen_for_simple (ncfileobj ):
40
- ''' Generator for example fileobj tests '''
41
- yield assert_equal , ncfileobj .history , b'Created for a test'
34
+ def assert_simple_truths (ncfileobj ):
35
+ assert ncfileobj .history == b'Created for a test'
42
36
time = ncfileobj .variables ['time' ]
43
- yield assert_equal , time .units , b'days since 2008-01-01'
44
- yield assert_equal , time .shape , (N_EG_ELS ,)
45
- yield assert_equal , time [- 1 ], N_EG_ELS - 1
46
-
47
-
48
- def test_read_write_files ():
49
- # test round trip for example file
50
- cwd = os .getcwd ()
51
- try :
52
- tmpdir = tempfile .mkdtemp ()
53
- os .chdir (tmpdir )
54
- with make_simple ('simple.nc' , 'w' ) as f :
55
- pass
56
- # To read the NetCDF file we just created::
57
- with netcdf_file ('simple.nc' ) as f :
58
- # Using mmap is the default
59
- yield assert_true , f .use_mmap
60
- for testargs in gen_for_simple (f ):
61
- yield testargs
62
-
63
- # Now without mmap
64
- with netcdf_file ('simple.nc' , mmap = False ) as f :
65
- # Using mmap is the default
66
- yield assert_false , f .use_mmap
67
- for testargs in gen_for_simple (f ):
68
- yield testargs
69
-
70
- # To read the NetCDF file we just created, as file object, no
71
- # mmap. When n * n_bytes(var_type) is not divisible by 4, this
72
- # raised an error in pupynere 1.0.12 and scipy rev 5893, because
73
- # calculated vsize was rounding up in units of 4 - see
74
- # https://www.unidata.ucar.edu/software/netcdf/docs/netcdf.html
75
- fobj = open ('simple.nc' , 'rb' )
76
- with netcdf_file (fobj ) as f :
77
- # by default, don't use mmap for file-like
78
- yield assert_false , f .use_mmap
79
- for testargs in gen_for_simple (f ):
80
- yield testargs
81
- except :
82
- os .chdir (cwd )
83
- shutil .rmtree (tmpdir )
84
- raise
85
- os .chdir (cwd )
86
- shutil .rmtree (tmpdir )
37
+ assert time .units == b'days since 2008-01-01'
38
+ assert time .shape == (N_EG_ELS ,)
39
+ assert time [- 1 ] == N_EG_ELS - 1
40
+
41
+
42
+ def test_read_write_files (tmp_path ):
43
+ os .chdir (str (tmp_path ))
44
+
45
+ with make_simple ('simple.nc' , 'w' ) as f :
46
+ pass
47
+ # To read the NetCDF file we just created::
48
+ with netcdf_file ('simple.nc' ) as f :
49
+ # Using mmap is the default
50
+ assert f .use_mmap
51
+ assert_simple_truths (f )
52
+
53
+ # Now without mmap
54
+ with netcdf_file ('simple.nc' , mmap = False ) as f :
55
+ # Using mmap is the default
56
+ assert not f .use_mmap
57
+ assert_simple_truths (f )
58
+
59
+ # To read the NetCDF file we just created, as file object, no
60
+ # mmap. When n * n_bytes(var_type) is not divisible by 4, this
61
+ # raised an error in pupynere 1.0.12 and scipy rev 5893, because
62
+ # calculated vsize was rounding up in units of 4 - see
63
+ # https://www.unidata.ucar.edu/software/netcdf/docs/netcdf.html
64
+ fobj = open ('simple.nc' , 'rb' )
65
+ with netcdf_file (fobj ) as f :
66
+ # by default, don't use mmap for file-like
67
+ assert not f .use_mmap
68
+ assert_simple_truths (f )
87
69
88
70
89
71
def test_read_write_sio ():
@@ -93,28 +75,26 @@ def test_read_write_sio():
93
75
94
76
eg_sio2 = BytesIO (str_val )
95
77
with netcdf_file (eg_sio2 ) as f2 :
96
- for testargs in gen_for_simple (f2 ):
97
- yield testargs
78
+ assert_simple_truths (f2 )
98
79
99
80
# Test that error is raised if attempting mmap for sio
100
81
eg_sio3 = BytesIO (str_val )
101
- yield assert_raises , ValueError , netcdf_file , eg_sio3 , 'r' , True
82
+ with pytest .raises (ValueError ):
83
+ netcdf_file (eg_sio3 , 'r' , True )
102
84
# Test 64-bit offset write / read
103
85
eg_sio_64 = BytesIO ()
104
86
with make_simple (eg_sio_64 , 'w' , version = 2 ) as f_64 :
105
87
str_val = eg_sio_64 .getvalue ()
106
88
107
89
eg_sio_64 = BytesIO (str_val )
108
90
with netcdf_file (eg_sio_64 ) as f_64 :
109
- for testargs in gen_for_simple (f_64 ):
110
- yield testargs
111
- yield assert_equal , f_64 .version_byte , 2
91
+ assert_simple_truths (f_64 )
92
+ assert f_64 .version_byte == 2
112
93
# also when version 2 explicitly specified
113
94
eg_sio_64 = BytesIO (str_val )
114
95
with netcdf_file (eg_sio_64 , version = 2 ) as f_64 :
115
- for testargs in gen_for_simple (f_64 ):
116
- yield testargs
117
- yield assert_equal , f_64 .version_byte , 2
96
+ assert_simple_truths (f_64 )
97
+ assert f_64 .version_byte == 2
118
98
119
99
120
100
def test_read_example_data ():
@@ -134,7 +114,8 @@ def test_itemset_no_segfault_on_readonly():
134
114
time_var = f .variables ['time' ]
135
115
136
116
# time_var.assignValue(42) should raise a RuntimeError--not seg. fault!
137
- assert_raises (RuntimeError , time_var .assignValue , 42 )
117
+ with pytest .raises (RuntimeError ):
118
+ time_var .assignValue (42 )
138
119
139
120
140
121
def test_write_invalid_dtype ():
@@ -147,22 +128,22 @@ def test_write_invalid_dtype():
147
128
with netcdf_file (BytesIO (), 'w' ) as f :
148
129
f .createDimension ('time' , N_EG_ELS )
149
130
for dt in dtypes :
150
- yield assert_raises , ValueError , \
151
- f .createVariable , 'time' , dt , ('time' ,)
131
+ with pytest . raises ( ValueError ):
132
+ f .createVariable ( 'time' , dt , ('time' ,) )
152
133
153
134
154
135
def test_flush_rewind ():
155
136
stream = BytesIO ()
156
137
with make_simple (stream , mode = 'w' ) as f :
157
- x = f .createDimension ('x' ,4 )
138
+ x = f .createDimension ('x' , 4 )
158
139
v = f .createVariable ('v' , 'i2' , ['x' ])
159
140
v [:] = 1
160
141
f .flush ()
161
142
len_single = len (stream .getvalue ())
162
143
f .flush ()
163
144
len_double = len (stream .getvalue ())
164
145
165
- assert_ ( len_single == len_double )
146
+ assert len_single == len_double
166
147
167
148
168
149
def test_dtype_specifiers ():
@@ -192,8 +173,8 @@ def test_ticket_1720():
192
173
193
174
io = BytesIO (contents )
194
175
with netcdf_file (io , 'r' ) as f :
195
- assert_equal ( f .history , b'Created for a test' )
176
+ assert f .history == b'Created for a test'
196
177
float_var = f .variables ['float_var' ]
197
- assert_equal ( float_var .units , b'metres' )
198
- assert_equal ( float_var .shape , (10 ,) )
199
- assert_ ( np .allclose (float_var [:], items ) )
178
+ assert float_var .units == b'metres'
179
+ assert float_var .shape == (10 ,)
180
+ assert np .allclose (float_var [:], items )
0 commit comments