refactor 'local_paths' and 'by_variable' into single fixture
@@ -99,29 +99,31 @@ def items_per_file(request):
return request.param
-@pytest.fixture(scope="session")
-def netcdf_local_paths(daily_xarray_dataset, tmpdir_factory, items_per_file):
- """Return a list of paths pointing to netcdf files."""
- tmp_path = tmpdir_factory.mktemp("netcdf_data")
- # copy needed to avoid polluting metadata across multiple tests
- datasets, fnames = _split_up_files_by_day(daily_xarray_dataset.copy(), items_per_file)
- full_paths = [tmp_path.join(fname) for fname in fnames]
- xr.save_mfdataset(datasets, [str(path) for path in full_paths])
- items_per_file = {"D": 1, "2D": 2}[items_per_file]
- return full_paths, items_per_file
+@pytest.fixture(
+ scope="session",
+ params=[_split_up_files_by_day, _split_up_files_by_variable_and_day]
+)
+def file_splitter(request):
+ return request.param
-# TODO: this is quite repetetive of the fixture above. Replace with parametrization.
@pytest.fixture(scope="session")
-def netcdf_local_paths_by_variable(daily_xarray_dataset, tmpdir_factory, items_per_file):
- """Return a list of paths pointing to netcdf files."""
+def netcdf_paths(daily_xarray_dataset, tmpdir_factory, items_per_file, file_splitter):
tmp_path = tmpdir_factory.mktemp("netcdf_data")
- datasets, fnames, fnames_by_variable = _split_up_files_by_variable_and_day(
- daily_xarray_dataset.copy(), items_per_file
- )
+ file_splitter_tuple = file_splitter(daily_xarray_dataset.copy(), items_per_file)
+
+ if len(file_splitter_tuple) == 2:
+ datasets, fnames = file_splitter_tuple
+ else:
+ datasets, fnames, fnames_by_variable = file_splitter_tuple
+
full_paths = [tmp_path.join(fname) for fname in fnames]
xr.save_mfdataset(datasets, [str(path) for path in full_paths])
items_per_file = {"D": 1, "2D": 2}[items_per_file]
+
+ if not fnames_by_variable:
+ return full_paths, items_per_file
+ else:
path_format = str(tmp_path) + "/{variable}_{time:03d}.nc"
return full_paths, items_per_file, fnames_by_variable, path_format
