-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[bug] Fix SettingWithCopyWarning and export error (#2481)
* fixed Pandas SettingWithCopyWarnings * fixed typo in test_google_view * fixed typo in test_google_view
- Loading branch information
1 parent
3da5c35
commit db844a5
Showing
9 changed files
with
1,074 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
822 changes: 822 additions & 0 deletions
822
...ehavioural_analysis/cassettes/test_google_view/test_display_queries_export[AAPL-10-].yaml
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# IMPORTATION STANDARD | ||
|
||
# IMPORTATION THIRDPARTY | ||
import pandas as pd | ||
import pytest | ||
|
||
# IMPORTATION INTERNAL | ||
from openbb_terminal.common.behavioural_analysis import google_view | ||
|
||
# pylint: disable=E1101 | ||
# pylint: disable=W0603 | ||
# pylint: disable=E1111 | ||
# pylint: disable=W0621 | ||
# pylint: disable=W0613 | ||
|
||
|
||
@pytest.fixture | ||
def get_queries_response(): | ||
""" | ||
mock response from get_queries function in google_model | ||
""" | ||
top_list = [ | ||
["stock aapl", 100], | ||
["aapl price", 29], | ||
["stock price aapl", 24], | ||
["amzn", 19], | ||
["tsla", 19], | ||
["msft", 15], | ||
["tsla stock", 12], | ||
["amzn stock", 11], | ||
["fb stock", 10], | ||
["msft stock", 9], | ||
["nvda", 7], | ||
["dow", 6], | ||
["tesla", 6], | ||
["amd", 5], | ||
["goog", 5], | ||
["baba", 5], | ||
["aapl premarket", 5], | ||
["nvda stock", 5], | ||
["tesla stock", 5], | ||
["amd stock", 4], | ||
["aapl stock price today", 4], | ||
["nflx", 4], | ||
["nio", 4], | ||
["baba stock", 4], | ||
["nio stock", 4], | ||
] | ||
|
||
resp = { | ||
"AAPL": { | ||
"top": pd.DataFrame(top_list, columns=["query", "value"]), | ||
"rising": pd.DataFrame(top_list, columns=["query", "value"]), | ||
} | ||
} | ||
|
||
return resp | ||
|
||
|
||
@pytest.mark.default_cassette("test_google_view") | ||
@pytest.mark.vcr | ||
@pytest.mark.parametrize( | ||
"symbol, limit, export", | ||
[ | ||
( | ||
# standard run without export | ||
"AAPL", | ||
10, | ||
"", | ||
), | ||
( | ||
# standard run WITH export | ||
"AAPL", | ||
10, | ||
"csv", | ||
), | ||
], | ||
) | ||
def test_display_queries(mocker, symbol, limit, export, get_queries_response): | ||
# MOCK VISUALIZE_OUTPUT | ||
mocker.patch(target="openbb_terminal.helper_classes.TerminalStyle.visualize_output") | ||
|
||
# MOCK GOOGLE_MODEL | ||
mocker.patch( | ||
target="openbb_terminal.common.behavioural_analysis.google_model.get_queries", | ||
return_value=get_queries_response, | ||
) | ||
|
||
# MOCK EXPORT_DATA | ||
export_mock = mocker.Mock() | ||
mocker.patch( | ||
target="openbb_terminal.common.behavioural_analysis.google_view.export_data", | ||
new=export_mock, | ||
) | ||
|
||
google_view.display_queries(symbol, limit, export) | ||
|
||
if export: | ||
pd.testing.assert_frame_equal( | ||
export_mock.call_args[0][3], # fourth positional arg is the df | ||
pd.DataFrame( | ||
get_queries_response[symbol]["top"].head(limit) | ||
), # expected DF | ||
) |
11 changes: 11 additions & 0 deletions
11
...ommon/behavioural_analysis/txt/test_google_view/test_display_queries_export[AAPL-10-].txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
query value | ||
0 stock aapl 100% | ||
1 aapl price 29% | ||
2 stock price aapl 24% | ||
3 amzn 19% | ||
4 tsla 19% | ||
5 msft 15% | ||
6 tsla stock 12% | ||
7 amzn stock 11% | ||
8 fb stock 10% | ||
9 msft stock 9% |
11 changes: 11 additions & 0 deletions
11
...on/behavioural_analysis/txt/test_google_view/test_display_queries_export[AAPL-10-csv].txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
query value | ||
0 stock aapl 100% | ||
1 aapl price 29% | ||
2 stock price aapl 24% | ||
3 amzn 19% | ||
4 tsla 19% | ||
5 msft 15% | ||
6 tsla stock 12% | ||
7 amzn stock 11% | ||
8 fb stock 10% | ||
9 msft stock 9% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# IMPORTATION STANDARD | ||
from pathlib import Path | ||
|
||
# IMPORTATION THIRDPARTY | ||
import pandas as pd | ||
import pytest | ||
|
||
# IMPORTATION INTERNAL | ||
from openbb_terminal.helper_funcs import export_data | ||
|
||
# pylint: disable=E1101 | ||
# pylint: disable=W0603 | ||
# pylint: disable=E1111 | ||
# pylint: disable=W0621 | ||
# pylint: disable=W0613 | ||
|
||
|
||
@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"{func_name}_20220829_235959" | ||
|
||
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"{func_name}_20220829_235959.{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) |