diff --git a/tests/test_command_line_interface.py b/tests/test_command_line_interface.py index 6ff11213..17b3cb95 100644 --- a/tests/test_command_line_interface.py +++ b/tests/test_command_line_interface.py @@ -28,8 +28,6 @@ logger = logging.getLogger() -DOWNLOAD_TEST_FOLDER = "tests/downloads" - def get_all_files_in_folder_tree(folder: str) -> list[str]: downloaded_files = [] diff --git a/tests/test_command_line_interface_nearest_layer_subset.py b/tests/test_command_line_interface_nearest_layer_subset.py index 65fcd035..8207a008 100644 --- a/tests/test_command_line_interface_nearest_layer_subset.py +++ b/tests/test_command_line_interface_nearest_layer_subset.py @@ -4,11 +4,6 @@ import numpy import xarray -from tests.test_command_line_interface import DOWNLOAD_TEST_FOLDER - -DOWNLOAD_TEST_FOLDER_NEAREST_LAYERS_SUBSET = pathlib.Path( - DOWNLOAD_TEST_FOLDER, "nearest_layer_subset" -) SUBSET_NEAREST_LAYER_OPTIONS = { "dataset_id": "cmems_mod_glo_phy-thetao_anfc_0.083deg_P1D-m", "requested_depth": 11.7, diff --git a/tests/test_get_sync.py b/tests/test_get_sync.py index f0d70fa3..064d4569 100644 --- a/tests/test_get_sync.py +++ b/tests/test_get_sync.py @@ -4,18 +4,23 @@ class TestGetSync: - def test_get_sync(self): - self.when_I_get_some_native_files_with_sync() - self.then_same_command_should_not_download() - self.when_I_delete_one_file() - self.then_same_command_with_sync_should_download_only_one_file() + def test_get_sync(self, tmp_path): + self.when_I_get_some_native_files_with_sync(tmp_path) + self.then_same_command_should_not_download(tmp_path) + self.when_I_delete_one_file(tmp_path) + self.then_same_command_with_sync_should_download_only_one_file( + tmp_path + ) + + def test_get_sync_delete(self, tmp_path): - def test_get_sync_delete(self): - self.when_I_get_some_native_files_with_sync() - self.when_I_add_a_file_locally() - self.then_command_sync_delete_should_propose_to_delete_it_and_delete_it() + self.when_I_get_some_native_files_with_sync(tmp_path) + self.when_I_add_a_file_locally(tmp_path) + self.then_command_sync_delete_should_propose_to_delete_it_and_delete_it( + tmp_path + ) - def test_get_sync_not_working_with_datasets_with_parts(self): + def test_get_sync_not_working_with_datasets_with_parts(self, tmp_path): self.command = self.command = [ "copernicusmarine", "get", @@ -26,7 +31,7 @@ def test_get_sync_not_working_with_datasets_with_parts(self): "202311", "--force-download", "-o", - "tests/downloads", + f"{tmp_path}", ] self.output = execute_in_terminal(self.command) assert ( @@ -48,7 +53,7 @@ def test_get_sync_needs_version(self): in self.output.stdout ) - def when_I_get_some_native_files_with_sync(self): + def when_I_get_some_native_files_with_sync(self, tmp_path): self.command = [ "copernicusmarine", "get", @@ -61,11 +66,11 @@ def when_I_get_some_native_files_with_sync(self): "202105", "--force-download", "-o", - "tests/downloads", + f"{tmp_path}", ] self.output = execute_in_terminal(self.command) - def then_same_command_should_not_download(self): + def then_same_command_should_not_download(self, tmp_path): self.command = [ "copernicusmarine", "get", @@ -77,22 +82,24 @@ def then_same_command_should_not_download(self): "--dataset-version", "202105", "-o", - "tests/downloads", + f"{tmp_path}", ] self.output = execute_in_terminal(self.command) assert b"No data to download" in self.output.stdout - def when_I_delete_one_file(self): + def when_I_delete_one_file(self, tmp_path): self.command = [ "rm", - "tests/downloads/ARCTIC_MULTIYEAR_BGC_002_005" + f"{tmp_path}/ARCTIC_MULTIYEAR_BGC_002_005" "/cmems_mod_arc_bgc_my_ecosmo_P1D-m_202105" "/2007/01/" "20070110_dm-25km-NERSC-MODEL-ECOSMO-ARC-RAN-fv2.0.nc", ] self.output = execute_in_terminal(self.command) - def then_same_command_with_sync_should_download_only_one_file(self): + def then_same_command_with_sync_should_download_only_one_file( + self, tmp_path + ): self.command = [ "copernicusmarine", "get", @@ -104,7 +111,7 @@ def then_same_command_with_sync_should_download_only_one_file(self): "--dataset-version", "202105", "-o", - "tests/downloads", + f"{tmp_path}", ] self.output = execute_in_terminal(self.command) assert ( @@ -122,10 +129,10 @@ def then_same_command_with_sync_should_download_only_one_file(self): not in self.output.stdout ) - def when_I_add_a_file_locally(self): + def when_I_add_a_file_locally(self, tmp_path): self.command = [ "touch", - "tests/downloads/ARCTIC_MULTIYEAR_BGC_002_005" + f"{tmp_path}/ARCTIC_MULTIYEAR_BGC_002_005" "/cmems_mod_arc_bgc_my_ecosmo_P1D-m_202105" "/2007/01/" "20070120_dm-25km-NERSC-MODEL-ECOSMO-ARC-RAN-fv2.0.nc", @@ -133,7 +140,7 @@ def when_I_add_a_file_locally(self): self.output = execute_in_terminal(self.command) def then_command_sync_delete_should_propose_to_delete_it_and_delete_it( - self, + self, tmp_path ): self.command = [ "copernicusmarine", @@ -147,7 +154,7 @@ def then_command_sync_delete_should_propose_to_delete_it_and_delete_it( "202105", "--force-download", "-o", - "tests/downloads", + f"{tmp_path}", ] self.output = execute_in_terminal(self.command) assert ( @@ -155,7 +162,7 @@ def then_command_sync_delete_should_propose_to_delete_it_and_delete_it( in self.output.stdout ) assert ( - b"tests/downloads/ARCTIC_MULTIYEAR_BGC_002_005" + f"{tmp_path}".encode() + b"/ARCTIC_MULTIYEAR_BGC_002_005" b"/cmems_mod_arc_bgc_my_ecosmo_P1D-m_202105" b"/2007/01/" b"20070120_dm-25km-NERSC-MODEL-ECOSMO-ARC-RAN-fv2.0.nc" @@ -163,7 +170,7 @@ def then_command_sync_delete_should_propose_to_delete_it_and_delete_it( ) assert ( os.path.isfile( - "tests/downloads/ARCTIC_MULTIYEAR_BGC_002_005" + f"{tmp_path}/ARCTIC_MULTIYEAR_BGC_002_005" "/cmems_mod_arc_bgc_my_ecosmo_P1D-m_202105" "/2007/01/" "20070120_dm-25km-NERSC-MODEL-ECOSMO-ARC-RAN-fv2.0.nc" diff --git a/tests/test_overwrite_output_data.py b/tests/test_overwrite_output_data.py index 5eca7175..b8b40eb9 100644 --- a/tests/test_overwrite_output_data.py +++ b/tests/test_overwrite_output_data.py @@ -208,14 +208,16 @@ def request_data_download( assert output.returncode == 0, output.stdout - def test_that_overwrite_option_does_not_create_subdirectory(self): + def test_that_overwrite_option_does_not_create_subdirectory( + self, tmp_path + ): """Unit test that verify that sub-directory is not created if data is overwritten and provided filepath is relative. JIRA ticket IOD-623 (https://mercator-ocean.atlassian.net/browse/IOD-623) """ relative_folder = pathlib.Path( - "tests/downloads/test_that_overwrite_option_does_not_create_subdirectory" + f"{tmp_path}/test_that_overwrite_option_does_not_create_subdirectory" ) # noqa pathlib.Path.mkdir(relative_folder, parents=True, exist_ok=True) filename = "test_file" diff --git a/tests/test_python_interface.py b/tests/test_python_interface.py index 0faca349..9768b996 100644 --- a/tests/test_python_interface.py +++ b/tests/test_python_interface.py @@ -210,7 +210,7 @@ def test_subset_modify_attr_for_depth(self): assert dataset.depth.attrs["standard_name"] == "depth" assert dataset.depth.attrs["long_name"] == "Depth" - def test_subset_keeps_fillvalue_empty(self): + def test_subset_keeps_fillvalue_empty(self, tmp_path): subset( dataset_id="cmems_mod_glo_phy-thetao_anfc_0.083deg_P1D-m", dataset_version="202211", @@ -224,10 +224,14 @@ def test_subset_keeps_fillvalue_empty(self): minimum_depth=0, maximum_depth=1, force_download=True, + output_directory=tmp_path, output_filename="netcdf_fillval.nc", overwrite_output_data=True, ) - subsetdata = xarray.open_dataset("netcdf_fillval.nc", decode_cf=False) + + subsetdata = xarray.open_dataset( + f"{tmp_path}/netcdf_fillval.nc", decode_cf=False + ) assert "_FillValue" not in subsetdata.longitude.attrs assert "_FillValue" not in subsetdata.time.attrs assert "_FillValue" not in subsetdata.latitude.attrs @@ -236,7 +240,7 @@ def test_subset_keeps_fillvalue_empty(self): assert subsetdata.time.attrs["calendar"] == "gregorian" assert subsetdata.time.attrs["units"] == "hours since 1950-01-01" - def test_subset_keeps_fillvalue_empty_w_compression(self): + def test_subset_keeps_fillvalue_empty_w_compression(self, tmp_path): subset( dataset_id="cmems_mod_glo_phy-thetao_anfc_0.083deg_P1D-m", dataset_version="202211", @@ -250,13 +254,14 @@ def test_subset_keeps_fillvalue_empty_w_compression(self): minimum_depth=0, maximum_depth=1, force_download=True, + output_directory=tmp_path, output_filename="netcdf_fillval_compressed.nc", netcdf_compression_enabled=True, overwrite_output_data=True, ) subsetdata = xarray.open_dataset( - "netcdf_fillval_compressed.nc", decode_cf=False + f"{tmp_path}/netcdf_fillval_compressed.nc", decode_cf=False ) assert "_FillValue" not in subsetdata.longitude.attrs assert "_FillValue" not in subsetdata.time.attrs