Files
OpenBB/tests/openbb_terminal/reports/test_reports_controller.py
montezdesousa a9787dd2d1 Refactor reports menu and make it work on installer (#2878)
* start papermill rollback

* move reports to templates folder

* comment some stuff

* refactor reports controller

* change var names

* clean ctrl class vars

* small refactoring on metadata search

* relocate some func

* breaking changes

* revert some changes

* destroying changes

* change menu appearance

* change menu appeaerance again

* update choices

* change some assets locations

* remove unused vars

* change some docstrings

* rename vars

* its working

* forgot one dash

* omg its working

* clean some code and doc

* change func name

* fix a bug and add some comments

* fix economy report startup

* add custom reports

* allow custom reports

* forgot to delete this

* unused import

* make somestuff more explicit, 5 min rule

* add helper func

* supress errors--no-verify

* add some type checks

* fix check ipynb func

* check inputs in run

* rewrite some integration tests

* remove unused import

* add some docs to website

* add auto completion

* warn if no forecast

* add to do

* add arg name if 1 arg

* add papermill pointing to github main

* add notebooks

* fix terminal spec

* relocated choices files

* merge main

* relocate website docs for reports

* limit user choices

* fix portfolio auto completion

* limit choices fix

* clean som edocstrings

* limit user choices in run command

* improve run parser

* fix msg

* add reports controller tests

* relax crypto choices limit

* contain commands

* pass pre commit

* flake8

* flake8

* ignore mypy

* pylint

* website

* move docs to jupyter tab

* add installer dep

* remove etf

* remove reports

* update reports to main

* handle events erros

* rever reports to previous commits before main merge

* add papermill installer strategies

* add flags to terminal

* avoid sdk crash

* execute notebook in thread

* remove unused sys

* execute report in a child process

* fix some notebook stuff

* fix portfolio report

* fix reports controller volatility

* change api to sdk

* move images

* remove som unucessary installer imports

* remove debugger warning

* move ipykernel launcher

* supress notebook crash

* remove portfolio decorator

* fix run command

* forgot to change this

* add images

* fix portfolio report

* fix crypto report

* linting and tests

* add run integration test

* pylint

* hide reports module args

* try at supressing args

* try at supreccing warning

* solve some os issues

* handle pm errors

* fix warning unrecognized arguments

* change comment

* avoid crash when parameters not found

* fix etf commands

* minor cleanup

* update forecasting check

* fix some linting

* fix futures test

* pylint

* limit user choices to prompt

* limit user choices to prompt

* supress warnings

* revert poetry lock back to main

* papermill pointing to main

* flake8

* forgot requirements

* Update helper_funcs.py

* Update reports_model.py

* choices

Co-authored-by: hjoaquim <h.joaquim@campus.fct.unl.pt>
Co-authored-by: James Simmons <53658028+simmonsj330@users.noreply.github.com>
2022-10-26 21:27:30 +01:00

359 lines
9.3 KiB
Python

# IMPORTATION STANDARD
import os
# IMPORTATION THIRDPARTY
import pytest
# IMPORTATION INTERNAL
from openbb_terminal.reports import reports_controller, reports_model
# pylint: disable=E1101
# pylint: disable=W0603
# pylint: disable=E1111
@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.reports.reports_controller"
# MOCK SWITCH
mocker.patch(
target=f"{path_controller}.ReportController.switch",
return_value=["quit"],
)
result_menu = reports_controller.ReportController(queue=queue).menu()
assert result_menu == expected
@pytest.mark.vcr(record_mode="none")
def test_menu_without_queue_completion(mocker):
path_controller = "openbb_terminal.reports.reports_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=reports_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 = reports_controller.ReportController(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.reports.reports_controller"
# DISABLE AUTO-COMPLETION
mocker.patch.object(
target=reports_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}.ReportController.switch",
new=mock_switch,
)
result_menu = reports_controller.ReportController(queue=None).menu()
assert result_menu == ["help"]
@pytest.mark.vcr(record_mode="none")
@pytest.mark.record_stdout
def test_print_help():
controller = reports_controller.ReportController(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",
"reports",
],
),
],
)
def test_switch(an_input, expected_queue):
controller = reports_controller.ReportController(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 = reports_controller.ReportController(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",
"reports",
],
),
(
"call_reset",
["help"],
[
"quit",
"reset",
"reports",
"help",
],
),
],
)
def test_call_func_expect_queue(expected_queue, func, queue):
controller = reports_controller.ReportController(queue=queue)
result = getattr(controller, func)([])
assert result is None
assert controller.queue == expected_queue
@pytest.mark.vcr(record_mode="none")
@pytest.mark.parametrize(
"tested_func, other_args, mocked_func, called_args, called_kwargs",
[
(
"call_etf",
[
"--symbol=SPY",
],
"reports_model.render_report",
[],
dict(
input_path=str(reports_model.REPORTS_FOLDER / "etf"),
args_dict={"symbol": "SPY"},
),
),
(
"call_forex",
[
"--symbol=EURUSD",
],
"reports_model.render_report",
[],
dict(
input_path=str(reports_model.REPORTS_FOLDER / "forex"),
args_dict={"symbol": "EURUSD"},
),
),
(
"call_portfolio",
[
"--orderbook=market.csv",
],
"reports_model.render_report",
[],
dict(
input_path=str(reports_model.REPORTS_FOLDER / "portfolio"),
args_dict={"orderbook": "market.csv"},
),
),
(
"call_economy",
[],
"reports_model.render_report",
[],
dict(
input_path=str(reports_model.REPORTS_FOLDER / "economy"),
args_dict={},
),
),
(
"call_equity",
[
"--symbol=TSLA",
],
"reports_model.render_report",
[],
dict(
input_path=str(reports_model.REPORTS_FOLDER / "equity"),
args_dict={"symbol": "TSLA"},
),
),
(
"call_crypto",
[
"--symbol=BTC",
],
"reports_model.render_report",
[],
dict(
input_path=str(reports_model.REPORTS_FOLDER / "crypto"),
args_dict={"symbol": "BTC"},
),
),
],
)
def test_call_func(
tested_func, mocked_func, other_args, called_args, called_kwargs, mocker
):
path_controller = "openbb_terminal.reports.reports_controller"
# MOCK REMOVE
mocker.patch(target=f"{path_controller}.os.remove")
if mocked_func:
mock = mocker.Mock()
mocker.patch(
target=f"{path_controller}.{mocked_func}",
new=mock,
)
controller = reports_controller.ReportController(queue=None)
getattr(controller, tested_func)(other_args)
if called_args or called_kwargs:
mock.assert_called_once_with(*called_args, **called_kwargs)
else:
mock.assert_called_once()
else:
controller = reports_controller.ReportController(queue=None)
getattr(controller, tested_func)(other_args)
# TODO: No test for forecast command, because it depends on darts being available
@pytest.mark.vcr(record_mode="none")
@pytest.mark.parametrize(
"tested_func, other_args, mocked_func, called_args, called_kwargs",
[
(
"call_forecast",
[
"--symbol=TSLA",
],
"reports_model.render_report",
[],
dict(
input_path=str(reports_model.REPORTS_FOLDER / "forecast"),
args_dict={"symbol": "TSLA"},
),
),
],
)
def test_call_func_forecast(
tested_func, mocked_func, other_args, called_args, called_kwargs, mocker
):
path_controller = "openbb_terminal.reports.reports_controller"
# MOCK REMOVE
mocker.patch(target=f"{path_controller}.os.remove")
try:
import darts # pyright: reportMissingImports=false # noqa: F401, E501 #pylint: disable=import-outside-toplevel, unused-import
forecast = True
except ImportError:
forecast = False
if forecast:
if mocked_func:
mock = mocker.Mock()
mocker.patch(
target=f"{path_controller}.{mocked_func}",
new=mock,
)
controller = reports_controller.ReportController(queue=None)
getattr(controller, tested_func)(other_args)
if called_args or called_kwargs:
mock.assert_called_once_with(*called_args, **called_kwargs)
else:
mock.assert_called_once()
else:
controller = reports_controller.ReportController(queue=None)
getattr(controller, tested_func)(other_args)