From bbe50ee1617682c14df3765a019fde90e0544de9 Mon Sep 17 00:00:00 2001 From: Diogo Sousa Date: Fri, 3 Feb 2023 13:08:43 +0000 Subject: [PATCH] tests account controller: clear, pull --- openbb_terminal/account/account_controller.py | 8 +- .../account/test_account_controller.py | 102 +++++++++++++++++- .../session/test_session_model.py | 16 ++- 3 files changed, 120 insertions(+), 6 deletions(-) diff --git a/openbb_terminal/account/account_controller.py b/openbb_terminal/account/account_controller.py index e83546f4e0b..bda3237b090 100644 --- a/openbb_terminal/account/account_controller.py +++ b/openbb_terminal/account/account_controller.py @@ -138,11 +138,11 @@ class AccountController(BaseController): ) if i.lower() in ["y", "yes"]: Local.apply_configs(configs=configs_diff) - console.print("\nDone.", style="info") + console.print("\n[info]Done.[/info]") else: - console.print("\nAborted.", style="info") + console.print("\n[info]Aborted.[/info]") else: - console.print("No changes to apply.", style="info") + console.print("\n[info]No changes to apply.[/info]") def call_clear(self, other_args: List[str]): """Clear data""" @@ -162,7 +162,7 @@ class AccountController(BaseController): console.print("") Hub.clear_user_configs(auth_header=User.get_auth_header()) else: - console.print("\nAborted.", style="info") + console.print("\n[info]Aborted.[/info]") # @log_start_end(log=logger) # def call_upload(self, other_args: List[str]): diff --git a/tests/openbb_terminal/account/test_account_controller.py b/tests/openbb_terminal/account/test_account_controller.py index 828d65103e5..939ad0f581c 100644 --- a/tests/openbb_terminal/account/test_account_controller.py +++ b/tests/openbb_terminal/account/test_account_controller.py @@ -1,8 +1,10 @@ # IMPORTATION STANDARD import os +import json # IMPORTATION THIRDPARTY import pytest +from requests import Response # IMPORTATION INTERNAL from openbb_terminal.account import account_controller @@ -11,6 +13,26 @@ from openbb_terminal.account import account_controller # pylint: disable=W0603 # pylint: disable=E1111 +TEST_SESSION = { + "access_token": "test_token", + "token_type": "bearer", + "uuid": "test_uuid", +} + +CONFIGS = { + "features_settings": { + "USE_WATERMARK": "False", + "TIMEZONE": "Europe/London", + "PLOT_DPI": "95", + "PLOT_HEIGHT_PERCENTAGE": "50.5", + "USER_DATA_DIRECTORY": "some/path/to/user/data", + }, + "features_keys": { + "API_KEY_ALPHAVANTAGE": "test_av", + "API_FRED_KEY": "test_fred", + }, +} + @pytest.fixture(scope="module") def vcr_config(): @@ -274,7 +296,6 @@ 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" ) @@ -296,3 +317,82 @@ def test_call_sync(mocker, other_args, sync): ) assert controller.queue == [] + + +@pytest.mark.parametrize( + "input_value", + [ + "y", + "n", + ], +) +def test_call_pull(mocker, input_value): + + DIFF = {"TIMEZONE": "Europe/London"} + + controller = account_controller.AccountController(queue=None) + path_controller = "openbb_terminal.account.account_controller" + + response = Response() + response.status_code = 200 + response._content = json.dumps(CONFIGS) # pylint: disable=protected-access + + mock_fetch_user_configs = mocker.patch( + target=f"{path_controller}.Hub.fetch_user_configs", + return_value=response, + ) + mock_get_session = mocker.patch( + target=f"{path_controller}.User.get_session", + return_value=TEST_SESSION, + ) + mock_get_diff = mocker.patch( + target=f"{path_controller}.get_diff", + return_value=DIFF, + ) + mock_input = mocker.patch( + target=f"{path_controller}.console.input", + return_value=input_value, + ) + mock_apply_configs = mocker.patch( + target=f"{path_controller}.Local.apply_configs", + ) + controller.call_pull(other_args=list()) + + mock_fetch_user_configs.assert_called_once_with(TEST_SESSION) + mock_get_session.assert_called_once() + mock_get_diff.assert_called_once_with(configs=CONFIGS) + mock_input.assert_called_once() + if input_value == "y": + mock_apply_configs.assert_called_once_with(configs=DIFF) + else: + mock_apply_configs.assert_not_called() + assert controller.queue == [] + + +@pytest.mark.parametrize( + "input_value", + [ + "y", + "n", + ], +) +def test_call_clear(mocker, input_value): + controller = account_controller.AccountController(queue=None) + path_controller = "openbb_terminal.account.account_controller" + + mock_input = mocker.patch( + target=f"{path_controller}.console.input", + return_value=input_value, + ) + mock_clear_user_configs = mocker.patch( + target=f"{path_controller}.Hub.clear_user_configs", + ) + controller.call_clear(other_args=list()) + + mock_input.assert_called_once() + + if input_value == "y": + mock_clear_user_configs.assert_called_once() + else: + mock_clear_user_configs.assert_not_called() + assert controller.queue == [] diff --git a/tests/openbb_terminal/session/test_session_model.py b/tests/openbb_terminal/session/test_session_model.py index bda3bec6ff9..891f94ca8b1 100644 --- a/tests/openbb_terminal/session/test_session_model.py +++ b/tests/openbb_terminal/session/test_session_model.py @@ -16,6 +16,20 @@ TEST_SESSION = { "uuid": "test_uuid", } +CONFIGS = { + "features_settings": { + "USE_WATERMARK": "False", + "TIMEZONE": "Europe/London", + "PLOT_DPI": "95", + "PLOT_HEIGHT_PERCENTAGE": "50.5", + "USER_DATA_DIRECTORY": "some/path/to/user/data", + }, + "features_keys": { + "API_KEY_ALPHAVANTAGE": "test_av", + "API_FRED_KEY": "test_fred", + }, +} + @pytest.mark.parametrize( "email, password, save", @@ -98,7 +112,7 @@ def test_login_success_response(): ): response = Response() response.status_code = 200 - response._content = json.dumps(TEST_SESSION) # pylint: disable=protected-access + response._content = json.dumps(CONFIGS) # pylint: disable=protected-access mock_fetch_user_configs.return_value = response assert session_model.login(TEST_SESSION) == session_model.LoginStatus.SUCCESS