Skip to content

Commit

Permalink
Fix tests not writing in a tmp_path
Browse files Browse the repository at this point in the history
  • Loading branch information
mbertin committed May 14, 2024
1 parent 831242e commit fcbf0c1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 34 deletions.
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
56 changes: 31 additions & 25 deletions tests/test_get_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@


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):
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()
def test_get_sync_delete(self, tmp_path):
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 +30,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 +52,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, output_path):
self.command = [
"copernicusmarine",
"get",
Expand All @@ -61,11 +65,11 @@ def when_I_get_some_native_files_with_sync(self):
"202105",
"--force-download",
"-o",
"tests/downloads",
f"{output_path}",
]
self.output = execute_in_terminal(self.command)

def then_same_command_should_not_download(self):
def then_same_command_should_not_download(self, output_path):
self.command = [
"copernicusmarine",
"get",
Expand All @@ -77,22 +81,24 @@ def then_same_command_should_not_download(self):
"--dataset-version",
"202105",
"-o",
"tests/downloads",
f"{output_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, output_path):
self.command = [
"rm",
"tests/downloads/ARCTIC_MULTIYEAR_BGC_002_005"
f"{output_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, output_path
):
self.command = [
"copernicusmarine",
"get",
Expand All @@ -104,7 +110,7 @@ def then_same_command_with_sync_should_download_only_one_file(self):
"--dataset-version",
"202105",
"-o",
"tests/downloads",
f"{output_path}",
]
self.output = execute_in_terminal(self.command)
assert (
Expand All @@ -122,18 +128,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, output_path):
self.command = [
"touch",
"tests/downloads/ARCTIC_MULTIYEAR_BGC_002_005"
f"{output_path}s/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, output_path
):
self.command = [
"copernicusmarine",
Expand All @@ -147,23 +153,23 @@ def then_command_sync_delete_should_propose_to_delete_it_and_delete_it(
"202105",
"--force-download",
"-o",
"tests/downloads",
f"{output_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"{output_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"{output_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

0 comments on commit fcbf0c1

Please sign in to comment.