Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tests not writing in a tmp_path #70

Merged
merged 5 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions tests/test_command_line_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
5 changes: 0 additions & 5 deletions tests/test_command_line_interface_nearest_layer_subset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
57 changes: 32 additions & 25 deletions tests/test_get_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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 (
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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 (
Expand All @@ -122,18 +129,18 @@ 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",
]
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",
Expand All @@ -147,23 +154,23 @@ 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 (
b"Some files will be deleted due to sync delete:"
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"
in self.output.stdout
)
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"
Expand Down
6 changes: 4 additions & 2 deletions tests/test_overwrite_output_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
13 changes: 9 additions & 4 deletions tests/test_python_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
Expand All @@ -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",
Expand All @@ -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
Expand Down