Skip to content

Commit 2eb4e09

Browse files
authored
Merge pull request #215 from boutproject/test-cache-inputs
Cache input data for tests
2 parents 2478fe6 + 535406f commit 2eb4e09

File tree

8 files changed

+253
-177
lines changed

8 files changed

+253
-177
lines changed

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ install_requires =
3030
xarray>=0.18.0
3131
boutdata>=0.1.4
3232
dask[array]>=2.10.0
33+
gelidum>=0.5.3
3334
natsort>=5.5.0
3435
matplotlib>=3.1.1,!=3.3.0,!=3.3.1,!=3.3.2
3536
animatplot>=0.4.2
3637
netcdf4>=1.4.0
3738
Pillow>=6.1.0
3839
importlib-metadata; python_version < "3.8"
39-
tests_require = pytest >= 3.3.0
4040
include_package_data = True
4141
packages = find:
4242

xbout/load.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ def open_boutdataset(
153153

154154
if "reload" in input_type:
155155
if input_type == "reload":
156+
if isinstance(datapath, Path):
157+
# xr.open_mfdataset only accepts glob patterns as strings, not Path
158+
# objects
159+
datapath = str(datapath)
156160
ds = xr.open_mfdataset(
157161
datapath,
158162
chunks=chunks,

xbout/tests/test_against_collect.py

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@
1010

1111

1212
class TestAccuracyAgainstOldCollect:
13-
def test_single_file(self, tmpdir_factory):
13+
def test_single_file(self, tmp_path_factory):
1414

1515
# Create temp directory for files
16-
test_dir = tmpdir_factory.mktemp("test_data")
16+
test_dir = tmp_path_factory.mktemp("test_data")
1717

1818
# Generate some test data
1919
generated_ds = create_bout_ds(syn_data_type="linear")
20-
generated_ds.to_netcdf(str(test_dir.join("BOUT.dmp.0.nc")))
20+
generated_ds.to_netcdf(test_dir.joinpath("BOUT.dmp.0.nc"))
2121

2222
var = "n"
2323
expected = old_collect(var, path=test_dir, xguards=True, yguards=False)
2424

2525
# Test against new standard - open_boutdataset
2626
with pytest.warns(UserWarning):
27-
ds = open_boutdataset(test_dir.join("BOUT.dmp.0.nc"))
27+
ds = open_boutdataset(test_dir.joinpath("BOUT.dmp.0.nc"))
2828
actual = ds[var].values
2929

3030
assert expected.shape == actual.shape
@@ -36,24 +36,24 @@ def test_single_file(self, tmpdir_factory):
3636
assert expected.shape == actual.shape
3737
npt.assert_equal(actual, expected)
3838

39-
def test_multiple_files_along_x(self, tmpdir_factory):
39+
def test_multiple_files_along_x(self, tmp_path_factory):
4040

4141
# Create temp directory for files
42-
test_dir = tmpdir_factory.mktemp("test_data")
42+
test_dir = tmp_path_factory.mktemp("test_data")
4343

4444
# Generate some test data
4545
ds_list, file_list = create_bout_ds_list(
4646
"BOUT.dmp", nxpe=3, nype=1, syn_data_type="linear"
4747
)
4848
for temp_ds, file_name in zip(ds_list, file_list):
49-
temp_ds.to_netcdf(str(test_dir.join(str(file_name))))
49+
temp_ds.to_netcdf(test_dir.joinpath(file_name))
5050

5151
var = "n"
5252
expected = old_collect(var, path=test_dir, prefix="BOUT.dmp", xguards=True)
5353

5454
# Test against new standard - open_boutdataset
5555
with pytest.warns(UserWarning):
56-
ds = open_boutdataset(test_dir.join("BOUT.dmp.*.nc"))
56+
ds = open_boutdataset(test_dir.joinpath("BOUT.dmp.*.nc"))
5757
actual = ds[var].values
5858

5959
assert expected.shape == actual.shape
@@ -65,24 +65,24 @@ def test_multiple_files_along_x(self, tmpdir_factory):
6565
assert expected.shape == actual.shape
6666
npt.assert_equal(actual, expected)
6767

68-
def test_multiple_files_along_y(self, tmpdir_factory):
68+
def test_multiple_files_along_y(self, tmp_path_factory):
6969

7070
# Create temp directory for files
71-
test_dir = tmpdir_factory.mktemp("test_data")
71+
test_dir = tmp_path_factory.mktemp("test_data")
7272

7373
# Generate some test data
7474
ds_list, file_list = create_bout_ds_list(
7575
"BOUT.dmp", nxpe=1, nype=3, syn_data_type="linear"
7676
)
7777
for temp_ds, file_name in zip(ds_list, file_list):
78-
temp_ds.to_netcdf(str(test_dir.join(str(file_name))))
78+
temp_ds.to_netcdf(test_dir.joinpath(file_name))
7979

8080
var = "n"
8181
expected = old_collect(var, path=test_dir, prefix="BOUT.dmp", xguards=True)
8282

8383
# Test against new standard - .open_boutdataset
8484
with pytest.warns(UserWarning):
85-
ds = open_boutdataset(test_dir.join("BOUT.dmp.*.nc"))
85+
ds = open_boutdataset(test_dir.joinpath("BOUT.dmp.*.nc"))
8686
actual = ds[var].values
8787

8888
assert expected.shape == actual.shape
@@ -94,24 +94,24 @@ def test_multiple_files_along_y(self, tmpdir_factory):
9494
assert expected.shape == actual.shape
9595
npt.assert_equal(actual, expected)
9696

97-
def test_multiple_files_along_xy(self, tmpdir_factory):
97+
def test_multiple_files_along_xy(self, tmp_path_factory):
9898

9999
# Create temp directory for files
100-
test_dir = tmpdir_factory.mktemp("test_data")
100+
test_dir = tmp_path_factory.mktemp("test_data")
101101

102102
# Generate some test data
103103
ds_list, file_list = create_bout_ds_list(
104104
"BOUT.dmp", nxpe=3, nype=3, syn_data_type="linear"
105105
)
106106
for temp_ds, file_name in zip(ds_list, file_list):
107-
temp_ds.to_netcdf(str(test_dir.join(str(file_name))))
107+
temp_ds.to_netcdf(test_dir.joinpath(file_name))
108108

109109
var = "n"
110110
expected = old_collect(var, path=test_dir, prefix="BOUT.dmp", xguards=True)
111111

112112
# Test against new standard - .open_boutdataset
113113
with pytest.warns(UserWarning):
114-
ds = open_boutdataset(test_dir.join("BOUT.dmp.*.nc"))
114+
ds = open_boutdataset(test_dir.joinpath("BOUT.dmp.*.nc"))
115115
actual = ds[var].values
116116

117117
assert expected.shape == actual.shape
@@ -123,16 +123,16 @@ def test_multiple_files_along_xy(self, tmpdir_factory):
123123
assert expected.shape == actual.shape
124124
npt.assert_equal(actual, expected)
125125

126-
def test_metadata(self, tmpdir_factory):
126+
def test_metadata(self, tmp_path_factory):
127127
# Create temp directory for files
128-
test_dir = tmpdir_factory.mktemp("test_data")
128+
test_dir = tmp_path_factory.mktemp("test_data")
129129

130130
# Generate some test data
131131
generated_ds = create_bout_ds(syn_data_type="linear")
132-
generated_ds.to_netcdf(str(test_dir.join("BOUT.dmp.0.nc")))
132+
generated_ds.to_netcdf(test_dir.joinpath("BOUT.dmp.0.nc"))
133133

134134
with pytest.warns(UserWarning):
135-
ds = open_boutdataset(test_dir.join("BOUT.dmp.*.nc"))
135+
ds = open_boutdataset(test_dir.joinpath("BOUT.dmp.*.nc"))
136136

137137
for v in METADATA_VARS:
138138
expected = old_collect(v, path=test_dir)
@@ -144,17 +144,17 @@ def test_metadata(self, tmpdir_factory):
144144
actual = new_collect(v, path=test_dir)
145145
npt.assert_equal(actual, expected)
146146

147-
def test_new_collect_indexing_int(self, tmpdir_factory):
147+
def test_new_collect_indexing_int(self, tmp_path_factory):
148148
# Create temp directory for files
149-
test_dir = tmpdir_factory.mktemp("test_data")
149+
test_dir = tmp_path_factory.mktemp("test_data")
150150

151151
# Generate some test data
152152
ds_list, file_list = create_bout_ds_list(
153153
"BOUT.dmp", nxpe=3, nype=3, syn_data_type="linear"
154154
)
155155

156156
for temp_ds, file_name in zip(ds_list, file_list):
157-
temp_ds.to_netcdf(str(test_dir.join(str(file_name))))
157+
temp_ds.to_netcdf(test_dir.joinpath(file_name))
158158

159159
var = "n"
160160
indexers = ["tind", "xind", "yind", "zind"]
@@ -170,16 +170,16 @@ def test_new_collect_indexing_int(self, tmpdir_factory):
170170
assert expected.shape == actual.shape
171171
npt.assert_equal(actual, expected)
172172

173-
def test_new_collect_indexing_list(self, tmpdir_factory):
173+
def test_new_collect_indexing_list(self, tmp_path_factory):
174174
# Create temp directory for files
175-
test_dir = tmpdir_factory.mktemp("test_data")
175+
test_dir = tmp_path_factory.mktemp("test_data")
176176

177177
# Generate some test data
178178
ds_list, file_list = create_bout_ds_list(
179179
"BOUT.dmp", nxpe=3, nype=3, syn_data_type="linear"
180180
)
181181
for temp_ds, file_name in zip(ds_list, file_list):
182-
temp_ds.to_netcdf(str(test_dir.join(str(file_name))))
182+
temp_ds.to_netcdf(test_dir.joinpath(file_name))
183183

184184
var = "n"
185185
indexers = ["tind", "xind", "yind", "zind"]
@@ -195,17 +195,17 @@ def test_new_collect_indexing_list(self, tmpdir_factory):
195195
assert expected.shape == actual.shape
196196
npt.assert_equal(actual, expected)
197197

198-
def test_new_collect_indexing_slice(self, tmpdir_factory):
198+
def test_new_collect_indexing_slice(self, tmp_path_factory):
199199
# Create temp directory for files
200-
test_dir = tmpdir_factory.mktemp("test_data")
200+
test_dir = tmp_path_factory.mktemp("test_data")
201201

202202
# Generate some test data
203203
ds_list, file_list = create_bout_ds_list(
204204
"BOUT.dmp", nxpe=3, nype=3, syn_data_type="linear"
205205
)
206206

207207
for temp_ds, file_name in zip(ds_list, file_list):
208-
temp_ds.to_netcdf(str(test_dir.join(str(file_name))))
208+
temp_ds.to_netcdf(test_dir.joinpath(file_name))
209209

210210
var = "n"
211211
indexers = ["tind", "xind", "yind", "zind"]

xbout/tests/test_animate.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@
1212

1313

1414
@pytest.fixture
15-
def create_test_file(tmpdir_factory):
15+
def create_test_file(tmp_path_factory):
1616

1717
# Create temp dir for output of animate1D/2D
18-
save_dir = tmpdir_factory.mktemp("test_data")
18+
save_dir = tmp_path_factory.mktemp("test_data")
1919

2020
# Generate some test data
2121
ds_list, file_list = create_bout_ds_list(
2222
"BOUT.dmp", nxpe=3, nype=3, syn_data_type="linear"
2323
)
2424
for ds, file_name in zip(ds_list, file_list):
25-
ds.to_netcdf(str(save_dir.join(str(file_name))))
25+
ds.to_netcdf(save_dir.joinpath(file_name))
2626

2727
with pytest.warns(UserWarning):
28-
ds = open_boutdataset(save_dir.join("BOUT.dmp.*.nc")) # Open test data
28+
ds = open_boutdataset(save_dir.joinpath("BOUT.dmp.*.nc")) # Open test data
2929

3030
return save_dir, ds
3131

0 commit comments

Comments
 (0)