From 4e7b94272e9ce33e960c3b1ce3a94122db247884 Mon Sep 17 00:00:00 2001 From: zhiren Date: Thu, 20 Jul 2023 16:02:39 -0400 Subject: [PATCH 1/4] add the new test cases for project command --- app/commands/project.py | 5 +- tests/app/commands/test_project_command.py | 59 ++++++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 tests/app/commands/test_project_command.py diff --git a/app/commands/project.py b/app/commands/project.py index 292b964a..44ada516 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_project_command.py b/tests/app/commands/test_project_command.py new file mode 100644 index 00000000..577e6b5f --- /dev/null +++ b/tests/app/commands/test_project_command.py @@ -0,0 +1,59 @@ +# Copyright (C) 2022-2023 Indoc Research +# +# Contact Indoc Research 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_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 From 07b0619f281ed76e184d408b9019b437f79fe8e5 Mon Sep 17 00:00:00 2001 From: zhiren Date: Thu, 20 Jul 2023 16:33:33 -0400 Subject: [PATCH 2/4] add new test cases for check command entry --- tests/app/commands/test_entry_point.py | 81 ++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 tests/app/commands/test_entry_point.py diff --git a/tests/app/commands/test_entry_point.py b/tests/app/commands/test_entry_point.py new file mode 100644 index 00000000..669bf5c8 --- /dev/null +++ b/tests/app/commands/test_entry_point.py @@ -0,0 +1,81 @@ +# Copyright (C) 2022-2023 Indoc Research +# +# Contact Indoc Research 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(): + assert 'project' in entry_point.commands.keys() + + func_map = { + 'list': project_list_all, + } + project_commands_object = entry_point.commands.get('project') + + for x in project_commands_object.commands.keys(): + assert func_map.get(x) == project_commands_object.commands.get(x) + + +def test_user_commands(): + assert 'user' in entry_point.commands.keys() + + func_map = { + 'login': login, + 'logout': logout, + } + user_commands_object = entry_point.commands.get('user') + + for x in user_commands_object.commands.keys(): + assert func_map.get(x) == user_commands_object.commands.get(x) + + +def test_file_commands(): + 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') + + for x in file_commands_object.commands.keys(): + assert func_map.get(x) == file_commands_object.commands.get(x) + + +def test_dataset_commands(): + 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') + + for x in dataset_commands_object.commands.keys(): + assert func_map.get(x) == dataset_commands_object.commands.get(x) From 0274ea76061b4d3d113be914dd9bf4cf66ed51f0 Mon Sep 17 00:00:00 2001 From: zhiren Date: Fri, 28 Jul 2023 11:52:01 -0400 Subject: [PATCH 3/4] update the entry point function with callback --- tests/app/commands/test_entry_point.py | 12 ++++++++---- tests/app/commands/test_project_command.py | 20 ++++++++++++++++++++ tests/conftest.py | 6 ++++++ 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/tests/app/commands/test_entry_point.py b/tests/app/commands/test_entry_point.py index 669bf5c8..e4f098bf 100644 --- a/tests/app/commands/test_entry_point.py +++ b/tests/app/commands/test_entry_point.py @@ -25,19 +25,20 @@ def test_entry_point(): assert x in entry_point.commands.keys() -def test_project_commands(): +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(): +def test_user_commands(user_login_true): assert 'user' in entry_point.commands.keys() func_map = { @@ -45,12 +46,13 @@ def test_user_commands(): '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(): +def test_file_commands(user_login_true): assert 'file' in entry_point.commands.keys() func_map = { @@ -62,12 +64,13 @@ def test_file_commands(): '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(): +def test_dataset_commands(user_login_true): assert 'dataset' in entry_point.commands.keys() func_map = { @@ -76,6 +79,7 @@ def test_dataset_commands(): '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 index 577e6b5f..eefb3ae3 100644 --- a/tests/app/commands/test_project_command.py +++ b/tests/app/commands/test_project_command.py @@ -46,6 +46,26 @@ def test_list_project_is_larger_than_page_size_with_page_0(mocker, cli_runner): 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) diff --git a/tests/conftest.py b/tests/conftest.py index 41dd3eaf..3d662a67 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() From 69603c699a68ee1e922f4ee17539aeb89627cb52 Mon Sep 17 00:00:00 2001 From: zhiren Date: Fri, 28 Jul 2023 15:43:20 -0400 Subject: [PATCH 4/4] use indoc systems in the license --- tests/app/commands/test_entry_point.py | 4 ++-- tests/app/commands/test_project_command.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/app/commands/test_entry_point.py b/tests/app/commands/test_entry_point.py index e4f098bf..b95ea560 100644 --- a/tests/app/commands/test_entry_point.py +++ b/tests/app/commands/test_entry_point.py @@ -1,6 +1,6 @@ -# Copyright (C) 2022-2023 Indoc Research +# Copyright (C) 2022-2023 Indoc Systems # -# Contact Indoc Research for any questions regarding the use of this source code. +# 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 diff --git a/tests/app/commands/test_project_command.py b/tests/app/commands/test_project_command.py index eefb3ae3..d021dd78 100644 --- a/tests/app/commands/test_project_command.py +++ b/tests/app/commands/test_project_command.py @@ -1,6 +1,6 @@ -# Copyright (C) 2022-2023 Indoc Research +# Copyright (C) 2022-2023 Indoc Systems # -# Contact Indoc Research for any questions regarding the use of this source code. +# Contact Indoc Systems for any questions regarding the use of this source code. from unittest.mock import Mock