diff --git a/openbb_terminal/account/account_controller.py b/openbb_terminal/account/account_controller.py index 7b3a71d385b..e83546f4e0b 100644 --- a/openbb_terminal/account/account_controller.py +++ b/openbb_terminal/account/account_controller.py @@ -118,7 +118,7 @@ class AccountController(BaseController): else: console.print(f"sync is {sync}, use --on or --off to change.") - def call_pull(self, _): + def call_pull(self, other_args: List[str]): """Pull data""" parser = argparse.ArgumentParser( add_help=False, @@ -126,7 +126,7 @@ class AccountController(BaseController): prog="pull", description="Pull and apply stored configurations from the cloud.", ) - ns_parser = self.parse_simple_args(parser, _) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: response = Hub.fetch_user_configs(User.get_session()) if response: @@ -144,7 +144,7 @@ class AccountController(BaseController): else: console.print("No changes to apply.", style="info") - def call_clear(self, _): + def call_clear(self, other_args: List[str]): """Clear data""" parser = argparse.ArgumentParser( add_help=False, @@ -152,7 +152,7 @@ class AccountController(BaseController): prog="clear", description="Clear stored configurations from the cloud.", ) - ns_parser = self.parse_simple_args(parser, _) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: i = console.input( "[red]This action is irreversible![/red]\n" diff --git a/tests/openbb_terminal/account/test_account_controller.py b/tests/openbb_terminal/account/test_account_controller.py new file mode 100644 index 00000000000..4b644adebdf --- /dev/null +++ b/tests/openbb_terminal/account/test_account_controller.py @@ -0,0 +1,299 @@ +# IMPORTATION STANDARD +import os +from unittest.mock import patch + +# IMPORTATION THIRDPARTY +import pytest + +# IMPORTATION INTERNAL +from openbb_terminal.account import account_controller + +# pylint: disable=E1101 +# pylint: disable=W0603 +# pylint: disable=E1111 + + +@pytest.fixture(scope="module") +def vcr_config(): + return { + "filter_headers": [("User-Agent", None)], + "filter_query_parameters": [ + ("period1", "MOCK_PERIOD_1"), + ("period2", "MOCK_PERIOD_2"), + ("date", "MOCK_DATE"), + ("apiKey", "MOCK_API_KEY"), + ], + } + + +@pytest.mark.vcr(record_mode="none") +@pytest.mark.parametrize( + "queue, expected", + [ + (["sync", "help"], ["help"]), + (["quit", "help"], ["help"]), + ], +) +def test_menu_with_queue(expected, mocker, queue): + path_controller = "openbb_terminal.account.account_controller" + + # MOCK SWITCH + mocker.patch( + target=f"{path_controller}.AccountController.switch", + return_value=["quit"], + ) + result_menu = account_controller.AccountController(queue=queue).menu() + + assert result_menu == expected + + +@pytest.mark.vcr(record_mode="none") +def test_menu_without_queue_completion(mocker): + path_controller = "openbb_terminal.account.account_controller" + + # ENABLE AUTO-COMPLETION : HELPER_FUNCS.MENU + mocker.patch( + target="openbb_terminal.feature_flags.USE_PROMPT_TOOLKIT", + new=True, + ) + mocker.patch( + target="openbb_terminal.parent_classes.session", + ) + mocker.patch( + target="openbb_terminal.parent_classes.session.prompt", + return_value="quit", + ) + + # DISABLE AUTO-COMPLETION : CONTROLLER.COMPLETER + mocker.patch.object( + target=account_controller.obbff, + attribute="USE_PROMPT_TOOLKIT", + new=True, + ) + mocker.patch( + target=f"{path_controller}.session", + ) + mocker.patch( + target=f"{path_controller}.session.prompt", + return_value="quit", + ) + + result_menu = account_controller.AccountController(queue=None).menu() + + assert result_menu == ["help"] + + +@pytest.mark.vcr(record_mode="none") +@pytest.mark.parametrize( + "mock_input", + ["help", "homee help", "home help", "mock"], +) +def test_menu_without_queue_sys_exit(mock_input, mocker): + path_controller = "openbb_terminal.account.account_controller" + + # DISABLE AUTO-COMPLETION + mocker.patch.object( + target=account_controller.obbff, + attribute="USE_PROMPT_TOOLKIT", + new=False, + ) + mocker.patch( + target=f"{path_controller}.session", + return_value=None, + ) + + # MOCK USER INPUT + mocker.patch("builtins.input", return_value=mock_input) + + # MOCK SWITCH + class SystemExitSideEffect: + def __init__(self): + self.first_call = True + + def __call__(self, *args, **kwargs): + if self.first_call: + self.first_call = False + raise SystemExit() + return ["quit"] + + mock_switch = mocker.Mock(side_effect=SystemExitSideEffect()) + mocker.patch( + target=f"{path_controller}.AccountController.switch", + new=mock_switch, + ) + + result_menu = account_controller.AccountController(queue=None).menu() + + assert result_menu == ["help"] + + +@pytest.mark.vcr(record_mode="none") +@pytest.mark.record_stdout +def test_print_help(): + controller = account_controller.AccountController(queue=None) + controller.print_help() + + +@pytest.mark.vcr(record_mode="none") +@pytest.mark.parametrize( + "an_input, expected_queue", + [ + ("", []), + ("/help", ["home", "help"]), + ("help/help", ["help", "help"]), + ("q", ["quit"]), + ("h", []), + ( + "r", + [ + "quit", + "reset", + "account", + ], + ), + ], +) +def test_switch(an_input, expected_queue): + controller = account_controller.AccountController(queue=None) + queue = controller.switch(an_input=an_input) + + assert queue == expected_queue + + +@pytest.mark.vcr(record_mode="none") +def test_call_cls(mocker): + mocker.patch("os.system") + controller = account_controller.AccountController(queue=None) + controller.call_cls([]) + + assert controller.queue == [] + os.system.assert_called_once_with("cls||clear") + + +@pytest.mark.vcr(record_mode="none") +@pytest.mark.parametrize( + "func, queue, expected_queue", + [ + ( + "call_exit", + [], + ["quit", "quit"], + ), + ("call_exit", ["help"], ["quit", "quit", "help"]), + ("call_home", [], ["quit"]), + ("call_help", [], []), + ("call_quit", [], ["quit"]), + ("call_quit", ["help"], ["quit", "help"]), + ( + "call_reset", + [], + [ + "quit", + "reset", + "account", + ], + ), + ( + "call_reset", + ["help"], + [ + "quit", + "reset", + "account", + "help", + ], + ), + ], +) +def test_call_func_expect_queue(expected_queue, func, queue): + controller = account_controller.AccountController(queue=queue) + result = getattr(controller, func)([]) + + assert result is None + assert controller.queue == expected_queue + + +@pytest.mark.vcr(record_mode="none") +@pytest.mark.parametrize( + "func", + [ + "call_sync", + "call_pull", + "call_clear", + ], +) +def test_call_func_no_parser(func, mocker): + # MOCK PARSE_KNOWN_ARGS_AND_WARN + mocker.patch( + target="openbb_terminal.account.account_controller.AccountController.parse_simple_args", + return_value=None, + ) + controller = account_controller.AccountController(queue=None) + + func_result = getattr(controller, func)(other_args=list()) + assert func_result is None + assert controller.queue == [] + controller.parse_simple_args.assert_called_once() + + +@pytest.mark.vcr +@pytest.mark.record_stdout +@pytest.mark.parametrize( + "other_args, sync", + [ + ( + [ + "--on", + ], + False, + ), + ( + [ + "--off", + ], + True, + ), + ( + [ + "--on", + ], + True, + ), + ( + [ + "--off", + ], + False, + ), + ( + [], + True, + ), + ], +) +def test_call_sync(mocker, other_args, sync): + controller = account_controller.AccountController(queue=None) + path_controller = "openbb_terminal.account.account_controller" + + # MOCK SWITCH + mock_set_obbff = mocker.patch( + target=f"{path_controller}.FeatureFlagsController.set_feature_flag" + ) + mocker.patch( + target=f"{path_controller}.obbff.SYNC_ENABLED", + new=sync, + ) + controller.call_sync(other_args=other_args) + + if not other_args: + mock_set_obbff.assert_not_called() + elif other_args[0] == "--on" and sync: + mock_set_obbff.assert_not_called() + elif other_args[0] == "--off" and not sync: + mock_set_obbff.assert_not_called() + elif other_args[0] in ["--on", "--off"]: + mock_set_obbff.assert_called_once_with( + "OPENBB_SYNC_ENABLED", not sync, force=True + ) + + assert controller.queue == [] diff --git a/tests/openbb_terminal/account/txt/test_account_controller/test_call_sync[other_args0-False].txt b/tests/openbb_terminal/account/txt/test_account_controller/test_call_sync[other_args0-False].txt new file mode 100644 index 00000000000..4edd4cdac07 --- /dev/null +++ b/tests/openbb_terminal/account/txt/test_account_controller/test_call_sync[other_args0-False].txt @@ -0,0 +1 @@ +[info]sync:[/info] OFF diff --git a/tests/openbb_terminal/account/txt/test_account_controller/test_call_sync[other_args1-True].txt b/tests/openbb_terminal/account/txt/test_account_controller/test_call_sync[other_args1-True].txt new file mode 100644 index 00000000000..2be0167f17b --- /dev/null +++ b/tests/openbb_terminal/account/txt/test_account_controller/test_call_sync[other_args1-True].txt @@ -0,0 +1 @@ +[info]sync:[/info] ON diff --git a/tests/openbb_terminal/account/txt/test_account_controller/test_call_sync[other_args2-True].txt b/tests/openbb_terminal/account/txt/test_account_controller/test_call_sync[other_args2-True].txt new file mode 100644 index 00000000000..2be0167f17b --- /dev/null +++ b/tests/openbb_terminal/account/txt/test_account_controller/test_call_sync[other_args2-True].txt @@ -0,0 +1 @@ +[info]sync:[/info] ON diff --git a/tests/openbb_terminal/account/txt/test_account_controller/test_call_sync[other_args3-False].txt b/tests/openbb_terminal/account/txt/test_account_controller/test_call_sync[other_args3-False].txt new file mode 100644 index 00000000000..4edd4cdac07 --- /dev/null +++ b/tests/openbb_terminal/account/txt/test_account_controller/test_call_sync[other_args3-False].txt @@ -0,0 +1 @@ +[info]sync:[/info] OFF diff --git a/tests/openbb_terminal/account/txt/test_account_controller/test_call_sync[other_args4-True].txt b/tests/openbb_terminal/account/txt/test_account_controller/test_call_sync[other_args4-True].txt new file mode 100644 index 00000000000..e7dcaab864b --- /dev/null +++ b/tests/openbb_terminal/account/txt/test_account_controller/test_call_sync[other_args4-True].txt @@ -0,0 +1 @@ +sync is ON, use --on or --off to change. diff --git a/tests/openbb_terminal/account/txt/test_account_controller/test_print_help.txt b/tests/openbb_terminal/account/txt/test_account_controller/test_print_help.txt new file mode 100644 index 00000000000..7ccbf387561 --- /dev/null +++ b/tests/openbb_terminal/account/txt/test_account_controller/test_print_help.txt @@ -0,0 +1,5 @@ +Cloud storage of keys, settings and feature flags: + sync Turns the cloud synchronization on/off + pull Pull data from cloud + clear Clear data from cloud +