From 33b17bf7a01a79f29d695bb30955cad90cbd6abe Mon Sep 17 00:00:00 2001 From: james Date: Tue, 21 Feb 2023 10:24:27 -0500 Subject: [PATCH] Tests pt1 --- openbb_terminal/economy/fred_view.py | 1 + .../test_print_help.txt | 1 - .../test_fixedincome_controller.py | 192 ++++++++++++++++++ .../test_print_help.txt | 27 +++ 4 files changed, 220 insertions(+), 1 deletion(-) create mode 100644 tests/openbb_terminal/fixedincome/test_fixedincome_controller.py create mode 100644 tests/openbb_terminal/fixedincome/txt/test_fixedincome_controller/test_print_help.txt diff --git a/openbb_terminal/economy/fred_view.py b/openbb_terminal/economy/fred_view.py index 65c7b53c08e..2d7da159e8a 100644 --- a/openbb_terminal/economy/fred_view.py +++ b/openbb_terminal/economy/fred_view.py @@ -183,6 +183,7 @@ def format_data_to_plot(data: pd.DataFrame, detail: dict) -> Tuple[pd.DataFrame, return data_to_plot, title + @log_start_end(log=logger) @check_api_key(["API_FRED_KEY"]) def display_yield_curve( diff --git a/tests/openbb_terminal/economy/txt/test_economy_controller/test_print_help.txt b/tests/openbb_terminal/economy/txt/test_economy_controller/test_print_help.txt index 0a89c544e3a..a3889bc5637 100644 --- a/tests/openbb_terminal/economy/txt/test_economy_controller/test_print_help.txt +++ b/tests/openbb_terminal/economy/txt/test_economy_controller/test_print_help.txt @@ -2,7 +2,6 @@ futures display a futures and commodities overview [WallStreetJournal, Finviz] map S&P500 index stocks map [Finviz] bigmac The Economist Big Mac index [Nasdaq] - ycrv show sovereign yield curves [FRED] events show economic calendar [Nasdaq] edebt show external debt statistics for various countries [Wikipedia] diff --git a/tests/openbb_terminal/fixedincome/test_fixedincome_controller.py b/tests/openbb_terminal/fixedincome/test_fixedincome_controller.py new file mode 100644 index 00000000000..d403ca4abc1 --- /dev/null +++ b/tests/openbb_terminal/fixedincome/test_fixedincome_controller.py @@ -0,0 +1,192 @@ +import os +import pytest + +from openbb_terminal.fixedincome import fixedincome_controller + + +@pytest.mark.vcr(record_mode="none") +@pytest.mark.parametrize( + "queue, expected", + [ + (["load", "help"], ["help"]), + (["quit", "help"], ["help"]), + ], +) +def test_menu_with_queue(expected, mocker, queue): + path_controller = "openbb_terminal.fixedincome.fixedincome_controller" + + # MOCK SWITCH + mocker.patch( + target=f"{path_controller}.FixedIncomeController.switch", + return_value=["quit"], + ) + result_menu = fixedincome_controller.FixedIncomeController(queue=queue).menu() + + assert result_menu == expected + + +@pytest.mark.vcr(record_mode="none") +def test_menu_without_queue_completion(mocker): + path_controller = "openbb_terminal.fixedincome.fixedincome_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=fixedincome_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 = fixedincome_controller.FixedIncomeController(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.fixedincome.fixedincome_controller" + + # DISABLE AUTO-COMPLETION + mocker.patch.object( + target=fixedincome_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}.FixedIncomeController.switch", + new=mock_switch, + ) + + result_menu = fixedincome_controller.FixedIncomeController(queue=None).menu() + + assert result_menu == ["help"] + + +@pytest.mark.vcr(record_mode="none") +@pytest.mark.record_stdout +def test_print_help(): + controller = fixedincome_controller.FixedIncomeController(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", + "fixedincome", + ], + ), + ], +) +def test_switch(an_input, expected_queue): + controller = fixedincome_controller.FixedIncomeController(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 = fixedincome_controller.FixedIncomeController(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", + "fixedincome", + ], + ), + ( + "call_reset", + ["help"], + [ + "quit", + "reset", + "fixedincome", + "help", + ], + ), + ], +) +def test_call_func_expect_queue(expected_queue, func, queue): + controller = fixedincome_controller.FixedIncomeController(queue=queue) + result = getattr(controller, func)([]) + + assert result is None + assert controller.queue == expected_queue diff --git a/tests/openbb_terminal/fixedincome/txt/test_fixedincome_controller/test_print_help.txt b/tests/openbb_terminal/fixedincome/txt/test_fixedincome_controller/test_print_help.txt new file mode 100644 index 00000000000..1cf15e46e0a --- /dev/null +++ b/tests/openbb_terminal/fixedincome/txt/test_fixedincome_controller/test_print_help.txt @@ -0,0 +1,27 @@ +Reference rates: + estr euro short-term rate (estr) [FRED, ECB] + sofr secured overnight financing rate (sofr) [FRED] + sonia sterling overnight index average (sonia) [FRED] + ameribor american interbank offered rate (ameribor) [FRED] + +Central bank rates: + fed effective federal funds rate [FRED] + projection the Federal Reserve's projection of the federal funds rate. [FRED] + iorb interest rate on reserve balances [FRED] + dwpcr discount window primary credit rate [FRED] + ecb key ECB interest rates [FRED] + +Government Bonds: + treasury plot countries (including forecast) treasury rates [OECD] + usrates plot secondary market, constant maturity or inflation protected rates [FRED] + ycrv plot US yield curve [FRED] + ecbycrv plot euro area yield curve [ECB] + +Corporate Bonds: + +Spreads: + + tmc 10-year treasury constant maturity minus selected treasury constant maturity [FRED] + ffrmc selected treasury constant maturity minus federal funds rate [FRED] + tbffr selected treasury bill minus federal funds rate [FRED] +