diff --git a/antareslauncher/file_manager/file_manager.py b/antareslauncher/file_manager/file_manager.py index 72bb18d..4cc15b3 100644 --- a/antareslauncher/file_manager/file_manager.py +++ b/antareslauncher/file_manager/file_manager.py @@ -26,10 +26,6 @@ def listdir_of(self, directory): list_dir.sort() return list_dir - @staticmethod - def is_dir(dir_path: Path): - return Path(dir_path).is_dir() - def _get_list_dir_without_subdir(self, dir_path, subdir_to_exclude): """Make a list of all the folders inside a directory except one @@ -118,34 +114,6 @@ def zip_dir_excluding_subdir(self, dir_path, zipfile_path, subdir_to_exclude): ) return Path(zipfile_path).is_file() - def unzip(self, file_path: str): - """Unzips the result of the antares job once is has been downloaded - - Args: - file_path: The path to the file - - Returns: - True if the file has been extracted, False otherwise - """ - self.logger.info(f"Unzipping {file_path}") - try: - with zipfile.ZipFile(file=file_path) as zip_file: - progress_bar = self.display.generate_progress_bar( - zip_file.namelist(), - desc="Extracting archive:", - total=len(zip_file.namelist()), - ) - for file in progress_bar: - zip_file.extract( - member=file, - path=Path(file_path).parent, - ) - return True - except ValueError: - return False - except FileNotFoundError: - return False - def make_dir(self, directory_name): self.logger.info(f"Creating directory {directory_name}") os.makedirs(directory_name, exist_ok=True) diff --git a/antareslauncher/main.py b/antareslauncher/main.py index f47fd38..233910d 100644 --- a/antareslauncher/main.py +++ b/antareslauncher/main.py @@ -138,7 +138,6 @@ def run_with( ) study_list_composer = StudyListComposer( repo=data_repo, - file_manager=file_manager, display=display, parameters=StudyListComposerParameters( studies_in_dir=arguments.studies_in, diff --git a/antareslauncher/use_cases/create_list/study_list_composer.py b/antareslauncher/use_cases/create_list/study_list_composer.py index 1a644be..4c25487 100644 --- a/antareslauncher/use_cases/create_list/study_list_composer.py +++ b/antareslauncher/use_cases/create_list/study_list_composer.py @@ -52,12 +52,10 @@ class StudyListComposer: def __init__( self, repo: DataRepoTinydb, - file_manager: FileManager, display: DisplayTerminal, parameters: StudyListComposerParameters, ): self._repo = repo - self._file_manager = file_manager self._display = display self._studies_in_dir = parameters.studies_in_dir self.time_limit = parameters.time_limit @@ -116,10 +114,9 @@ def update_study_database(self): self._new_study_added = False - directories = self._file_manager.listdir_of(self._studies_in_dir) - for directory in directories: - directory_path = Path(self._studies_in_dir) / Path(directory) - if self._file_manager.is_dir(directory_path): + directories = Path(self._studies_in_dir).iterdir() + for directory_path in sorted(directories): + if directory_path.is_dir(): self._update_database_with_directory(directory_path) if not self._new_study_added: @@ -136,8 +133,7 @@ def _update_database_with_new_study( ) self._update_database_with_study(buffer_study) - def _update_database_with_directory(self, directory_path: t.Union[str, Path]): - directory_path = Path(directory_path) + def _update_database_with_directory(self, directory_path: Path): solver_version = get_solver_version(directory_path) antares_version = self.antares_version or solver_version if not antares_version: diff --git a/tests/integration/test_integration_study_list_composer.py b/tests/integration/test_integration_study_list_composer.py deleted file mode 100644 index 889cd8a..0000000 --- a/tests/integration/test_integration_study_list_composer.py +++ /dev/null @@ -1,47 +0,0 @@ -from unittest import mock - -import pytest - -from antareslauncher.data_repo.data_repo_tinydb import DataRepoTinydb -from antareslauncher.display.display_terminal import DisplayTerminal -from antareslauncher.file_manager.file_manager import FileManager -from antareslauncher.use_cases.create_list.study_list_composer import ( - StudyListComposer, - StudyListComposerParameters, -) - - -class TestIntegrationStudyListComposer: - def setup_method(self): - self.repo = mock.Mock(spec=DataRepoTinydb) - self.file_manager = mock.Mock(spec=FileManager) - self.display = mock.Mock(spec=DisplayTerminal) - self.study_list_composer = StudyListComposer( - repo=self.repo, - file_manager=self.file_manager, - display=self.display, - parameters=StudyListComposerParameters( - studies_in_dir="studies_in", - time_limit=42, - n_cpu=24, - log_dir="job_log_dir", - xpansion_mode=None, - output_dir="output_dir", - post_processing=False, - antares_versions_on_remote_server=["700"], - other_options="", - ), - ) - - @pytest.mark.integration_test - def test_get_list_of_studies(self): - self.study_list_composer.get_list_of_studies() - self.repo.get_list_of_studies.assert_called_once_with() - - @pytest.mark.integration_test - def test_update_study_database(self): - self.file_manager.listdir_of = mock.Mock(return_value=["study1", "study2"]) - self.file_manager.is_dir = mock.Mock(return_value=True) - self.study_list_composer.xpansion_mode = "r" # "r", "cpp" or None - self.study_list_composer.update_study_database() - self.display.show_message.assert_called() diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py new file mode 100644 index 0000000..410c237 --- /dev/null +++ b/tests/unit/conftest.py @@ -0,0 +1,60 @@ +import shutil +from pathlib import Path +from unittest import mock + +import pytest +from antareslauncher.data_repo.data_repo_tinydb import DataRepoTinydb +from antareslauncher.display.display_terminal import DisplayTerminal +from antareslauncher.use_cases.create_list.study_list_composer import ( + StudyListComposer, + StudyListComposerParameters, +) +from tests.unit.assets import ASSETS_DIR + + +@pytest.fixture(name="studies_in_dir") +def studies_in_dir_fixture(tmp_path: Path) -> str: + studies_in_dir = tmp_path.joinpath("STUDIES-IN") + assets_dir = ASSETS_DIR.joinpath("study_list_composer/studies") + shutil.copytree(assets_dir, studies_in_dir) + return str(studies_in_dir) + + +@pytest.fixture(name="repo") +def repo_fixture(tmp_path: Path) -> DataRepoTinydb: + return DataRepoTinydb( + database_file_path=tmp_path.joinpath("repo.json"), + db_primary_key="name", + ) + + +@pytest.fixture(name="study_list_composer") +def study_list_composer_fixture( + tmp_path: Path, + repo: DataRepoTinydb, + studies_in_dir: str, +) -> StudyListComposer: + display = mock.Mock(spec=DisplayTerminal) + composer = StudyListComposer( + repo=repo, + display=display, + parameters=StudyListComposerParameters( + studies_in_dir=studies_in_dir, + time_limit=42, + n_cpu=24, + log_dir=str(tmp_path.joinpath("LOGS")), + xpansion_mode=None, + output_dir=str(tmp_path.joinpath("FINISHED")), + post_processing=False, + antares_versions_on_remote_server=[ + "800", + "810", + "820", + "830", + "840", + "850", + ], + other_options="", + ), + ) + return composer diff --git a/tests/unit/test_file_manager.py b/tests/unit/test_file_manager.py index 64f15a4..e751efd 100644 --- a/tests/unit/test_file_manager.py +++ b/tests/unit/test_file_manager.py @@ -7,7 +7,6 @@ from antareslauncher.display.display_terminal import DisplayTerminal from antareslauncher.file_manager import file_manager -from antareslauncher.study_dto import StudyDTO from tests.data import DATA_DIR DIR_TO_ZIP = DATA_DIR / "file-manager-test" / "to-zip" @@ -48,22 +47,6 @@ def test_golden_master_for_zip_study_excluding_output_dir(self, tmp_path): assert result_zip_file.is_file() result_zip_file.unlink() - @pytest.mark.unit_test - def test_unzip(self): - """ - Tests the scenario where the specified zip file does not exist. The expected outcome is - that the function returns False, indicating that the zip file could not be unzipped. - This test is checking that the function correctly handles the case where the input file is not present. - """ - # given - study = StudyDTO("path") - display_terminal = DisplayTerminal() - my_file_manager = file_manager.FileManager(display_terminal) - # when - output = my_file_manager.unzip(study.local_final_zipfile_path) - # then - assert output is False - @pytest.mark.unit_test def test__get_list_dir_without_subdir(self): """ diff --git a/tests/unit/test_study_list_composer.py b/tests/unit/test_study_list_composer.py index b8bec18..1b76ea1 100644 --- a/tests/unit/test_study_list_composer.py +++ b/tests/unit/test_study_list_composer.py @@ -1,18 +1,11 @@ -import shutil from pathlib import Path -from unittest import mock import pytest -from antareslauncher.data_repo.data_repo_tinydb import DataRepoTinydb -from antareslauncher.display.display_terminal import DisplayTerminal -from antareslauncher.file_manager.file_manager import FileManager from antareslauncher.use_cases.create_list.study_list_composer import ( StudyListComposer, - StudyListComposerParameters, get_solver_version, ) -from tests.unit.assets import ASSETS_DIR CONFIG_NOMINAL_VERSION = """\ [antares] @@ -73,55 +66,6 @@ def test_get_solver_version( assert actual == expected -@pytest.fixture(name="studies_in_dir") -def studies_in_dir_fixture(tmp_path: Path) -> str: - studies_in_dir = tmp_path.joinpath("STUDIES-IN") - assets_dir = ASSETS_DIR.joinpath("study_list_composer/studies") - shutil.copytree(assets_dir, studies_in_dir) - return str(studies_in_dir) - - -@pytest.fixture(name="repo") -def repo_fixture(tmp_path: Path) -> DataRepoTinydb: - return DataRepoTinydb( - database_file_path=tmp_path.joinpath("repo.json"), - db_primary_key="name", - ) - - -@pytest.fixture(name="study_list_composer") -def study_list_composer_fixture( - tmp_path: Path, - repo: DataRepoTinydb, - studies_in_dir: str, -) -> StudyListComposer: - display = mock.Mock(spec=DisplayTerminal) - composer = StudyListComposer( - repo=repo, - file_manager=FileManager(display_terminal=display), - display=display, - parameters=StudyListComposerParameters( - studies_in_dir=studies_in_dir, - time_limit=42, - n_cpu=24, - log_dir=str(tmp_path.joinpath("LOGS")), - xpansion_mode=None, - output_dir=str(tmp_path.joinpath("FINISHED")), - post_processing=False, - antares_versions_on_remote_server=[ - "800", - "810", - "820", - "830", - "840", - "850", - ], - other_options="", - ), - ) - return composer - - class TestStudyListComposer: @pytest.mark.parametrize("xpansion_mode", ["r", "cpp", ""]) def test_update_study_database__xpansion_mode(