diff --git a/app/commands/project.py b/app/commands/project.py index 74be4484..47b3441c 100644 --- a/app/commands/project.py +++ b/app/commands/project.py @@ -47,12 +47,12 @@ def cli(): ) @doc(project_help.project_help_page(project_help.ProjectHELP.PROJECT_LIST)) def project_list_all(page, page_size, order, order_by, detached): + project_mgr = SrvProjectManager() + if detached: - project_mgr = SrvProjectManager() projects = project_mgr.list_projects(page, page_size, order, order_by) else: while True: - project_mgr = SrvProjectManager() projects = project_mgr.list_projects(page, page_size, order, order_by) if len(projects) < page_size and page == 0: break @@ -62,6 +62,7 @@ def project_list_all(page, page_size, order, order_by, detached): choice = ['next page', 'exit'] else: choice = ['previous page', 'next page', 'exit'] + val = questionary.select('\nWhat do you want?', qmark='', choices=choice).ask() if val == 'exit': mhandler.SrvOutPutHandler.list_success('Project') diff --git a/tests/app/commands/test_entry_point.py b/tests/app/commands/test_entry_point.py new file mode 100644 index 00000000..b95ea560 --- /dev/null +++ b/tests/app/commands/test_entry_point.py @@ -0,0 +1,85 @@ +# Copyright (C) 2022-2023 Indoc Systems +# +# Contact Indoc Systems for any questions regarding the use of this source code. + +from app.commands.dataset import dataset_download +from app.commands.dataset import dataset_list +from app.commands.dataset import dataset_show_detail +from app.commands.entry_point import command_groups +from app.commands.entry_point import entry_point +from app.commands.file import file_check_manifest +from app.commands.file import file_download +from app.commands.file import file_export_manifest +from app.commands.file import file_list +from app.commands.file import file_put +from app.commands.file import file_resume +from app.commands.project import project_list_all +from app.commands.user import login +from app.commands.user import logout + + +def test_entry_point(): + possible_commands = command_groups() + + for x in possible_commands: + assert x in entry_point.commands.keys() + + +def test_project_commands(user_login_true): + assert 'project' in entry_point.commands.keys() + + func_map = { + 'list': project_list_all, + } + project_commands_object = entry_point.commands.get('project') + project_commands_object.callback() + + for x in project_commands_object.commands.keys(): + assert func_map.get(x) == project_commands_object.commands.get(x) + + +def test_user_commands(user_login_true): + assert 'user' in entry_point.commands.keys() + + func_map = { + 'login': login, + 'logout': logout, + } + user_commands_object = entry_point.commands.get('user') + user_commands_object.callback() + + for x in user_commands_object.commands.keys(): + assert func_map.get(x) == user_commands_object.commands.get(x) + + +def test_file_commands(user_login_true): + assert 'file' in entry_point.commands.keys() + + func_map = { + 'list': file_list, + 'upload': file_put, + 'attribute-list': file_check_manifest, + 'attribute-export': file_export_manifest, + 'sync': file_download, + 'resume': file_resume, + } + file_commands_object = entry_point.commands.get('file') + file_commands_object.callback() + + for x in file_commands_object.commands.keys(): + assert func_map.get(x) == file_commands_object.commands.get(x) + + +def test_dataset_commands(user_login_true): + assert 'dataset' in entry_point.commands.keys() + + func_map = { + 'list': dataset_list, + 'show-detail': dataset_show_detail, + 'download': dataset_download, + } + dataset_commands_object = entry_point.commands.get('dataset') + dataset_commands_object.callback() + + for x in dataset_commands_object.commands.keys(): + assert func_map.get(x) == dataset_commands_object.commands.get(x) diff --git a/tests/app/commands/test_project_command.py b/tests/app/commands/test_project_command.py new file mode 100644 index 00000000..d021dd78 --- /dev/null +++ b/tests/app/commands/test_project_command.py @@ -0,0 +1,79 @@ +# Copyright (C) 2022-2023 Indoc Systems +# +# Contact Indoc Systems for any questions regarding the use of this source code. + +from unittest.mock import Mock + +import questionary + +from app.commands.project import project_list_all + + +def list_fake_project(number: int): + return [f'project-{i}' for i in range(number)] + + +def test_list_project_is_smaller_than_page_size(mocker, cli_runner): + page_size = 10 + project_list = list_fake_project(5) + mocker.patch('app.services.project_manager.project.SrvProjectManager.list_projects', return_value=project_list) + + result = cli_runner.invoke( + project_list_all, ['--page', 0, '--page-size', page_size, '--order', 'desc', '--order-by', 'created_at'] + ) + + assert result.exit_code == 0 + assert '' == result.output + + +def test_list_project_is_larger_than_page_size_with_page_0(mocker, cli_runner): + page_size = 10 + project_list = list_fake_project(20) + mocker.patch('app.services.project_manager.project.SrvProjectManager.list_projects', return_value=project_list) + clear_mock = mocker.patch('click.clear', return_value=None) + + question_mock = mocker.patch.object(questionary, 'select', return_value=questionary.select) + questionary.select.return_value.ask = Mock() + questionary.select.return_value.ask.side_effect = ['next page', 'previous page', 'exit'] + + result = cli_runner.invoke( + project_list_all, ['--page', 0, '--page-size', page_size, '--order', 'desc', '--order-by', 'created_at'] + ) + + assert result.exit_code == 0 + assert 'Project list fetched successfully!\n' == result.output + assert question_mock.call_count == 3 + assert clear_mock.call_count == 2 + + +def test_list_project_is_larger_than_page_size_with_page_1(mocker, cli_runner): + page_size = 10 + project_list = list_fake_project(5) + mocker.patch('app.services.project_manager.project.SrvProjectManager.list_projects', return_value=project_list) + clear_mock = mocker.patch('click.clear', return_value=None) + + question_mock = mocker.patch.object(questionary, 'select', return_value=questionary.select) + questionary.select.return_value.ask = Mock() + questionary.select.return_value.ask.side_effect = ['previous page', 'exit'] + + result = cli_runner.invoke( + project_list_all, ['--page', 1, '--page-size', page_size, '--order', 'desc', '--order-by', 'created_at'] + ) + + assert result.exit_code == 0 + assert '' == result.output + assert question_mock.call_count == 1 + assert clear_mock.call_count == 1 + + +def test_list_project_with_detached(mocker, cli_runner): + page_size = 10 + project_list = list_fake_project(10) + mocker.patch('app.services.project_manager.project.SrvProjectManager.list_projects', return_value=project_list) + + result = cli_runner.invoke( + project_list_all, + ['--page', 0, '--page-size', page_size, '--order', 'desc', '--order-by', 'created_at', '--detached'], + ) + + assert result.exit_code == 0 diff --git a/tests/conftest.py b/tests/conftest.py index fcb01dd1..8d85ac44 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -35,6 +35,12 @@ def mock_settings(monkeypatch, mocker): mocker.patch('app.configs.user_config.UserConfig.save') # Do not save config when running tests +@pytest.fixture +def user_login_true(mocker): + mocker.patch('app.services.user_authentication.decorator.check_is_login', return_value=True) + mocker.patch('app.services.user_authentication.decorator.check_is_active', return_value=True) + + @pytest.fixture def settings() -> Settings: return get_settings()