Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/commands/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')
Expand Down
85 changes: 85 additions & 0 deletions tests/app/commands/test_entry_point.py
Original file line number Diff line number Diff line change
@@ -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)
79 changes: 79 additions & 0 deletions tests/app/commands/test_project_command.py
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down