Files
OpenBB/tests/openbb_terminal/test_helper_funcs.py
montezdesousa 475ebb5acd [WIP] Migrate user, feature flags and configurations to dataclass (#4309)
* implement user and change is guest

* replace User methods

* move remaining functions to user

* fix bug

* portfolio test

* fix tests

* fix tests

* uncomment skip

* ruff

* start credentials model implementation

* change deault structure

* refacto quandl

* change default struct and file names

* bug

* start using global user

* add comments

* move update flair

* change cfg references in keys model

* reddit & fmp

* refactore additional keys

* remaing config keys

* remove unsude imports and fix hub keys

* ruff

* fix line too long

* unblock unittests

* fix keys model tests

* remove os patch

* fix account controller tests

* fix account controller tests

* fix account model

* account fixed

* fix user tests

* User Model

* User Model

* User Model

* Move session folder

* User Model : grouping the models

* User Model : fix bugs

* Config Plot + bug fix

* User Model : Refactoring plots

* Feature flags

* Remove dup

* clean comments a fix some vars

* fix feature flags controller

* refactor obbff from settings controller

* refactor a bunch of obbff variables

* fix bug blocking terminal

* fix integration tests

* refactor controllers with obbff

* refactor remaining ffs

* remove comments

* log not started

* mispelled variable

* set current user on local model

* User Model : moving files + updating

* User Preferences

* User : Refactor is_guest function + bug fix

* User : refactor is_guest function

* Fix conftest.py

* remove lost obbffs

* typo on fileoverwrite

* Plot config: move variables and start plot dpi

* User : tests

* refactor plot dpi references

* remove plot dpi imports

* refactor some config terminal leftover

* /account/sync : fix bug

* remove configs reload

* start path refactor

* installation paths vs user paths

* remove cfg_plot

* Tests : User Refactoring

* user prefix only for preferences

* user prefix only for preferences

* move user paths

* remove comment

* update apply remote configs

* fix tz not updating

* save some copys

* small fix

* create interface for credentials and preferences

* wrong docstrings fixed

* ruff core

* fix some pref setting

* login is better

* ruff

* ruff

* remove Literal

* add small comment

* Tests : User Refactoring

* Credentials Model : add DataBento

* Fix helper_funcs

* Fixing code

* Tests : fixing

* User Model : freezing the models

* Fixed Income + Databento

* Tests : fixing keys

* black

* ruff

* Tests : user model

* Tests : user model

* Fix linting

* Fix linting

* Linting

* Linting

* Fixing code and tests

* Fix tests

* mypy

* SDK

* Clean ruff cache

* Fix syntax issue

* Update fred_view

* Black

* Linting

* Update code

* Update tests

* Linting

* Linting

* Tests

* Black

* Linting

* Linting

* Mock os.listdir to avoid looking for file that doesnt exist

* Mock options paths

* lint

* Tests : fix

* Code : update

* Tests + Linting

* Tests

* Ruff

* Code : update

* Tests

* Code : update

* Fix workflow

* Code : update

---------

Co-authored-by: Chavithra PARANA <chavithra@gmail.com>
Co-authored-by: luqazino@gmail.com <luqazino@gmail.com>
Co-authored-by: James Maslek <jmaslek11@gmail.com>
2023-03-05 00:37:19 +01:00

232 lines
6.7 KiB
Python

# IMPORTATION STANDARD
import datetime
from pathlib import Path
# IMPORTATION THIRDPARTY
import pandas as pd
import pytest
# IMPORTATION INTERNAL
from openbb_terminal.core.session.current_user import (
PreferencesModel,
copy_user,
)
from openbb_terminal.helper_funcs import (
check_start_less_than_end,
export_data,
remove_timezone_from_dataframe,
revert_lambda_long_number_format,
update_news_from_tweet_to_be_displayed,
)
# pylint: disable=E1101
# pylint: disable=W0603
# pylint: disable=E1111
# pylint: disable=W0621
# pylint: disable=W0613
# pylint: disable=R0912
@pytest.fixture(scope="module")
def vcr_config():
return {
"filter_headers": [
("User-Agent", None),
("Authorization", "MOCK_AUTHORIZATION"),
],
}
@pytest.fixture
def mock_compose_export_path(monkeypatch, tmp_path):
# files in tmp_dir will remain (in separate folders) for 3 sequential runs of pytest
def mock_return(func_name, *args, **kwargs):
return tmp_path / f"20220829_235959_{func_name}"
monkeypatch.setattr("openbb_terminal.helper_funcs.compose_export_path", mock_return)
@pytest.mark.parametrize(
"export_type, dir_path, func_name, df",
[
(
"csv",
"C:/openbb_terminal/common/behavioural_analysis",
"queries",
pd.DataFrame(),
),
(
"json",
"C:/openbb_terminal/common/behavioural_analysis",
"queries",
pd.DataFrame(),
),
(
"xlsx",
"C:/openbb_terminal/common/behavioural_analysis",
"queries",
pd.DataFrame(),
),
(
"png",
"C:/openbb_terminal/common/behavioural_analysis",
"queries",
pd.DataFrame(),
),
(
"jpg",
"C:/openbb_terminal/common/behavioural_analysis",
"queries",
pd.DataFrame(),
),
(
"pdf",
"C:/openbb_terminal/common/behavioural_analysis",
"queries",
pd.DataFrame(),
),
(
"svg",
"C:/openbb_terminal/common/behavioural_analysis",
"queries",
pd.DataFrame(),
),
],
)
def test_export_data_filetypes(
mock_compose_export_path, export_type, dir_path, func_name, df, tmp_path
):
export_data(export_type, dir_path, func_name, df)
assert Path(tmp_path / f"20220829_235959_{func_name}.{export_type}").exists()
# TODO add assertions to check the validity of the files?
@pytest.mark.parametrize(
"export_type, dir_path, func_name, data",
[
(
# Dict instead of DataFrame
"csv",
"C:/openbb_terminal/common/behavioural_analysis",
"queries",
dict({"test": "dict"}),
),
],
)
def test_export_data_invalid_data(
mock_compose_export_path, export_type, dir_path, func_name, data
):
with pytest.raises(AttributeError):
assert export_data(export_type, dir_path, func_name, data)
def test_start_less_than_end():
assert check_start_less_than_end("2022-01-01", "2022-01-02") is False
assert check_start_less_than_end("2022-01-02", "2022-01-01") is True
assert check_start_less_than_end("2022-01-01", "2022-01-01") is True
assert check_start_less_than_end(None, "2022-01-01") is False
assert check_start_less_than_end("2022-01-02", None) is False
assert check_start_less_than_end(None, None) is False
@pytest.mark.parametrize(
"df, df_expected",
[
(
pd.DataFrame(
{
"date1": pd.date_range("2021-01-01", periods=5, tz="Europe/London"),
"date2": pd.date_range("2022-01-01", periods=5, tz="Europe/London"),
"value1": [1, 2, 3, 4, 5],
"value2": [6, 7, 8, 9, 10],
}
),
pd.DataFrame(
{
"date1": [
datetime.date(2021, 1, 1),
datetime.date(2021, 1, 2),
datetime.date(2021, 1, 3),
datetime.date(2021, 1, 4),
datetime.date(2021, 1, 5),
],
"date2": [
datetime.date(2022, 1, 1),
datetime.date(2022, 1, 2),
datetime.date(2022, 1, 3),
datetime.date(2022, 1, 4),
datetime.date(2022, 1, 5),
],
"value1": [1, 2, 3, 4, 5],
"value2": [6, 7, 8, 9, 10],
}
),
),
],
)
def test_remove_timezone_from_dataframe(df, df_expected):
# set date1 as index
df.set_index("date1", inplace=True)
df_expected.set_index("date1", inplace=True)
df_result = remove_timezone_from_dataframe(df)
assert df_result.equals(df_expected)
@pytest.mark.parametrize(
"value, expected",
[
(123, 123),
("xpto", "xpto"),
(
"this isssssssssss a veryyyyy long stringgg",
"this isssssssssss a veryyyyy long stringgg",
),
(None, None),
(True, True),
(0, 0),
("2022-01-01", "2022-01-01"),
("3/9/2022", "3/9/2022"),
("2022-03-09 10:30:00", "2022-03-09 10:30:00"),
("a 123", "a 123"),
([1, 2, 3], [1, 2, 3]),
("", ""),
("-3 K", -3000.0),
("-99 M", -99000000.0),
("-125 B", -125000000000.0),
("-15 T", -15000000000000.0),
("-15 P", -15000000000000000.0),
("-15 P xpto", "-15 P xpto"),
("-15 P 3 K", "-15 P 3 K"),
("15 P -3 K", "15 P -3 K"),
("2.130", 2.130),
("2,130.000", "2,130.000"), # this is not a valid number
("674,234.99", "674,234.99"), # this is not a valid number
],
)
def test_revert_lambda_long_number_format(value, expected):
assert revert_lambda_long_number_format(value) == expected
@pytest.mark.vcr
def test_update_news_from_tweet_to_be_displayed(mocker):
preferences = PreferencesModel(
TOOLBAR_TWEET_NEWS=True,
TOOLBAR_TWEET_NEWS_SECONDS_BETWEEN_UPDATES=300,
TOOLBAR_TWEET_NEWS_NUM_LAST_TWEETS_TO_READ=3,
TOOLBAR_TWEET_NEWS_KEYWORDS="BREAKING,JUST IN",
)
mock_current_user = copy_user(preferences=preferences)
mocker.patch(
target="openbb_terminal.core.session.current_user.__current_user",
new=mock_current_user,
)
mocker.patch(
target="openbb_terminal.helper_funcs.LAST_TWEET_NEWS_UPDATE_CHECK_TIME",
new=None,
)
news_tweet = update_news_from_tweet_to_be_displayed()
assert isinstance(news_tweet, str) and news_tweet != ""