def test_given_data_repo_if_study_is_not_inside_database_then_db_insert_is_called( self, ): # given repo = DataRepoTinydb("") repo.is_study_inside_database = mock.Mock(return_value=False) type(repo).db = mock.PropertyMock() study_dto = StudyDTO(path="path") # when repo.save_study(study_dto) # then repo.db.insert.assert_called_once()
def test_given_db_when_get_list_of_studies_is_called_then_db_all_is_called( self): # given repo = DataRepoTinydb("") repo.doc_to_study = mock.Mock(return_value=42) type(repo).db = mock.PropertyMock() repo.db.all = mock.Mock(return_value=[]) # when repo.get_list_of_studies() # then repo.db.all_assert_calles_once()
def test_given_data_repo_when_save_study_is_called_then_is_study_inside_database_is_called( self, ): # given repo_mock = DataRepoTinydb("") type(repo_mock).db = mock.PropertyMock() repo_mock.is_study_inside_database = mock.Mock() study_dto = StudyDTO(path="path") # when repo_mock.save_study(study_dto) # then repo_mock.is_study_inside_database.assert_called_with(study=study_dto)
def test_integration_given_data_repo_if_study_is_found_once_in_database_then_db_update_is_called( self, ): # given repo = DataRepoTinydb("") type(repo).db = mock.PropertyMock() repo.db.search = mock.Mock(return_value=["A"]) study_dto = StudyDTO(path="path") # when repo.save_study(study_dto) # then repo.db.update.assert_called_once()
def test_given_db_of_n_elements_when_get_list_of_studies_is_called_then_doc_to_study_is_called_n_times( self, ): # given n = 5 repo = DataRepoTinydb("") repo.doc_to_study = mock.Mock(return_value=42) type(repo).db = mock.PropertyMock() repo.db.all = mock.Mock(return_value=[""] * n) # when repo.get_list_of_studies() # then assert repo.doc_to_study.call_count == n
def test_given_tinydb_document_when_doc_to_study_called_then_return_corresponding_study( self, ): # given expected_study = StudyDTO(path="path") expected_study.job_id = 42 expected_study.n_cpu = 999 doc = tinydb.database.Document(expected_study.__dict__, 42) # when output_study = DataRepoTinydb.doc_to_study(doc) # then assert expected_study.__dict__ == output_study.__dict__
def test_is_study_inside_database_returns_true_only_if_one_study_is_found( self): # given repo = DataRepoTinydb("") type(repo).db = mock.PropertyMock() dummy_study = StudyDTO(path="path") repo.db.search = mock.Mock(return_value=["first_element"]) # when output = repo.is_study_inside_database(study=dummy_study) # then assert output is True repo.db.search = mock.Mock( return_value=["first_element", "second_element"]) # when output = repo.is_study_inside_database(study=dummy_study) # then assert output is False repo.db.search = mock.Mock(return_value=[]) # when output = repo.is_study_inside_database(study=dummy_study) # then assert output is False
def test_is_job_id_inside_database_returns_true_only_if_one_job_id_is_found( self): # given repo = DataRepoTinydb("") type(repo).db = mock.PropertyMock() study_dto = StudyDTO(path="path") study_dto.job_id = 6381 repo.get_list_of_studies = mock.Mock(return_value=[study_dto]) repo.save_study(study_dto) # when output = repo.is_job_id_inside_database(6381) # then assert output is True
def run_with( arguments: argparse.Namespace, parameters: MainParameters, show_banner=False ): """Instantiates all the objects necessary to antares-launcher, and runs the program""" if arguments.version: print(f"Antares_Launcher v{VERSION}") return if show_banner: print(ANTARES_LAUNCHER_BANNER) display = DisplayTerminal() file_manager = FileManager(display) db_json_file_path = parameters.json_dir / parameters.default_json_db_name tree_structure_initializer = TreeStructureInitializer( display, file_manager, arguments.studies_in, arguments.log_dir, arguments.output_dir, ) tree_structure_initializer.init_tree_structure() logger_initializer = LoggerInitializer( str(Path(arguments.log_dir) / "antares_launcher.log") ) logger_initializer.init_logger() # connection ssh_dict = get_ssh_config_dict( file_manager, arguments.json_ssh_config, parameters.default_ssh_dict, ) connection = ssh_connection.SshConnection(config=ssh_dict) verify_connection(connection, display) slurm_script_features = SlurmScriptFeatures(parameters.slurm_script_path) environment = RemoteEnvironmentWithSlurm(connection, slurm_script_features) data_repo = DataRepoTinydb( database_file_path=db_json_file_path, db_primary_key=parameters.db_primary_key ) study_list_composer = StudyListComposer( repo=data_repo, file_manager=file_manager, display=display, parameters=StudyListComposerParameters( studies_in_dir=arguments.studies_in, time_limit=arguments.time_limit, log_dir=arguments.log_dir, n_cpu=arguments.n_cpu, xpansion_mode=arguments.xpansion_mode, output_dir=arguments.output_dir, post_processing=arguments.post_processing, antares_versions_on_remote_server=parameters.antares_versions_on_remote_server, ), ) launch_controller = LaunchController( repo=data_repo, env=environment, file_manager=file_manager, display=display, ) state_updater = StateUpdater(env=environment, display=display) retrieve_controller = RetrieveController( repo=data_repo, env=environment, file_manager=file_manager, display=display, state_updater=state_updater, ) slurm_queue_show = SlurmQueueShow(env=environment, display=display) check_queue_controller = CheckQueueController( slurm_queue_show=slurm_queue_show, state_updater=state_updater, repo=data_repo, ) job_kill_controller = JobKillController( env=environment, display=display, repo=data_repo, ) wait_controller = WaitController(display=display) launcher = AntaresLauncher( study_list_composer=study_list_composer, launch_controller=launch_controller, retrieve_controller=retrieve_controller, job_kill_controller=job_kill_controller, check_queue_controller=check_queue_controller, wait_controller=wait_controller, wait_mode=arguments.wait_mode, wait_time=arguments.wait_time, job_id_to_kill=arguments.job_id_to_kill, xpansion_mode=arguments.xpansion_mode, check_queue_bool=arguments.check_queue, ) launcher.run()
def setup_method(self): self.data_repo = DataRepoTinydb("") self.data_repo.save_study = mock.Mock() self.display = mock.Mock()
def run_with(arguments): """Instantiates all the objects necessary to antares-launcher, and runs the program""" if arguments.version: print(f"Antares_Launcher v{VERSION}") return print(ANTARES_LAUNCHER_BANNER) studies_in = Path(arguments.studies_in).resolve() display = DisplayTerminal() file_manager = FileManager(display) json_file_name = JSON_DIR / DEFAULT_JSON_DB_NAME tree_structure_initializer = TreeStructureInitializer( display, file_manager, arguments.studies_in, definitions.LOG_DIR, arguments.output_dir, ) tree_structure_initializer.init_tree_structure() logger_initializer = LoggerInitializer( Path(arguments.log_dir) / "antares_launcher.log") logger_initializer.init_logger() # connection ssh_dict = get_ssh_config_dict(file_manager, arguments.json_ssh_config) connection = ssh_connection.SshConnection(config=ssh_dict) verify_connection(connection, display) slurm_script_features = SlurmScriptFeatures() environment = RemoteEnvironmentWithSlurm(connection, slurm_script_features) data_repo = DataRepoTinydb(database_name=json_file_name) study_list_composer = StudyListComposer( repo=data_repo, file_manager=file_manager, display=display, studies_in_dir=studies_in, time_limit=arguments.time_limit, n_cpu=arguments.n_cpu, log_dir=arguments.log_dir, output_dir=arguments.output_dir, xpansion_mode=arguments.xpansion_mode, post_processing=arguments.post_processing, ) launch_controller = LaunchController( repo=data_repo, env=environment, file_manager=file_manager, display=display, ) state_updater = StateUpdater(env=environment, display=display) retrieve_controller = RetrieveController( repo=data_repo, env=environment, file_manager=file_manager, display=display, state_updater=state_updater, ) slurm_queue_show = SlurmQueueShow(env=environment, display=display) check_queue_controller = CheckQueueController( slurm_queue_show=slurm_queue_show, state_updater=state_updater, repo=data_repo) job_kill_controller = JobKillController( env=environment, display=display, repo=data_repo, ) wait_controller = WaitController(display=display) launcher = AntaresLauncher( study_list_composer=study_list_composer, launch_controller=launch_controller, retrieve_controller=retrieve_controller, job_kill_controller=job_kill_controller, check_queue_controller=check_queue_controller, wait_controller=wait_controller, wait_mode=arguments.wait_mode, wait_time=arguments.wait_time, job_id_to_kill=arguments.job_id_to_kill, xpansion_mode=arguments.xpansion_mode, check_queue_bool=arguments.check_queue, ) launcher.run()