From c57a82bed7b6c7d7656399bb9e8f78eee59f56c3 Mon Sep 17 00:00:00 2001 From: nectariosouzo Date: Wed, 27 Sep 2023 14:05:02 -0400 Subject: [PATCH] Add basic testing for new etf endpoints --- openbb_terminal/etf/fmp_model.py | 5 +- .../etf/test_etf.openbb | 1 + .../test_get_etf_holdings[ARKK].yaml | 48 ++++++++++ .../test_get_etf_holdings[VTI].yaml | 48 ++++++++++ ...ct_change[2023-09-01-2022-09-01-ARKK].yaml | 94 +++++++++++++++++++ ...ce_change[2023-09-01-2022-09-01-TSLA].yaml | 48 ++++++++++ ...rformance[2023-09-01-2022-09-01-TSLA].yaml | 48 ++++++++++ ...pct_change[2023-09-01-2022-09-01-ARKK].csv | 1 + ...ice_change[2023-09-01-2022-09-01-TSLA].csv | 1 + .../test_get_etf_holdings[ARKK].json | 1 + tests/openbb_terminal/etf/test_fmp_model.py | 63 +++++++++++++ tests/openbb_terminal/etf/test_fmp_view.py | 22 +++++ 12 files changed, 378 insertions(+), 2 deletions(-) create mode 100644 tests/openbb_terminal/etf/cassettes/test_fmp_model/test_get_etf_holdings[ARKK].yaml create mode 100644 tests/openbb_terminal/etf/cassettes/test_fmp_model/test_get_etf_holdings[VTI].yaml create mode 100644 tests/openbb_terminal/etf/cassettes/test_fmp_model/test_get_holdings_pct_change[2023-09-01-2022-09-01-ARKK].yaml create mode 100644 tests/openbb_terminal/etf/cassettes/test_fmp_model/test_get_stock_price_change[2023-09-01-2022-09-01-TSLA].yaml create mode 100644 tests/openbb_terminal/etf/cassettes/test_fmp_view/test_view_etf_holdings_performance[2023-09-01-2022-09-01-TSLA].yaml create mode 100644 tests/openbb_terminal/etf/csv/test_fmp_model/test_get_holdings_pct_change[2023-09-01-2022-09-01-ARKK].csv create mode 100644 tests/openbb_terminal/etf/csv/test_fmp_model/test_get_stock_price_change[2023-09-01-2022-09-01-TSLA].csv create mode 100644 tests/openbb_terminal/etf/json/test_fmp_model/test_get_etf_holdings[ARKK].json diff --git a/openbb_terminal/etf/fmp_model.py b/openbb_terminal/etf/fmp_model.py index 9f7bf49a606..62db201957d 100644 --- a/openbb_terminal/etf/fmp_model.py +++ b/openbb_terminal/etf/fmp_model.py @@ -71,7 +71,6 @@ def get_stock_price_change(ticker: str, start_date: str, end_date: str) -> float """ current_user = get_current_user() url = f'''https://financialmodelingprep.com/api/v3/historical-price-full/{ticker}?from={start_date}&to={end_date}&serietype=line&apikey={current_user.credentials.API_KEY_FINANCIALMODELINGPREP}''' - response = request(url) if response.status_code != 200 or "Error Message" in response.json(): message = f"Error, Status Code: {response.status_code}." @@ -117,7 +116,7 @@ def get_etf_holdings(ticker: str) -> List[Dict[str,any]]: else message + "\n" + response.json()["Error Message"] + ".\n" ) console.print(message) - return None + return [] return response.json() @@ -137,6 +136,8 @@ def get_holdings_pct_change(ticker: str, start_date: str, end_date: str) -> pd.D Calculated percentage change for each stock in the ETF, in descending order. """ holdings = get_etf_holdings(ticker=ticker) + if len(holdings) == 0: + return pd.DataFrame() data_list = [] for stock in holdings: if stock['asset']: diff --git a/openbb_terminal/miscellaneous/integration_tests_scripts/etf/test_etf.openbb b/openbb_terminal/miscellaneous/integration_tests_scripts/etf/test_etf.openbb index eee0afe7816..f1376c391dc 100644 --- a/openbb_terminal/miscellaneous/integration_tests_scripts/etf/test_etf.openbb +++ b/openbb_terminal/miscellaneous/integration_tests_scripts/etf/test_etf.openbb @@ -2,6 +2,7 @@ etf load spy overview holdings +holdings_perf --limit 3 weights weights --raw candle diff --git a/tests/openbb_terminal/etf/cassettes/test_fmp_model/test_get_etf_holdings[ARKK].yaml b/tests/openbb_terminal/etf/cassettes/test_fmp_model/test_get_etf_holdings[ARKK].yaml new file mode 100644 index 00000000000..8ef804515aa --- /dev/null +++ b/tests/openbb_terminal/etf/cassettes/test_fmp_model/test_get_etf_holdings[ARKK].yaml @@ -0,0 +1,48 @@ +interactions: +- request: + body: null + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + method: GET + uri: https://financialmodelingprep.com/api/v3/etf-holder/ARKK?apikey=MOCK_API_KEY + response: + body: + string: "{\n \"Error Message\": \"Invalid API KEY. Please retry or visit our + documentation to create one FREE https://site.financialmodelingprep.com/developer/docs\"\n}" + headers: + Access-Control-Allow-Credentials: + - 'true' + Access-Control-Allow-Headers: + - X-Requested-With, content-type, auth-token, Authorization, stripe-signature, + APPS + Access-Control-Allow-Methods: + - GET, POST, OPTIONS + Access-Control-Allow-Origin: + - '*' + Access-Control-Max-Age: + - '3600' + Connection: + - keep-alive + Content-Length: + - '154' + Content-Type: + - application/json; charset=utf-8 + Date: + - Wed, 27 Sep 2023 14:51:51 GMT + ETag: + - W/"9a-ufKRTAmgqkT6Vv3bqU5trChBRE8" + Server: + - nginx/1.18.0 (Ubuntu) + X-Frame-Options: + - SAMEORIGIN + X-Powered-By: + - Express + status: + code: 401 + message: Unauthorized +version: 1 diff --git a/tests/openbb_terminal/etf/cassettes/test_fmp_model/test_get_etf_holdings[VTI].yaml b/tests/openbb_terminal/etf/cassettes/test_fmp_model/test_get_etf_holdings[VTI].yaml new file mode 100644 index 00000000000..a96eaf7e257 --- /dev/null +++ b/tests/openbb_terminal/etf/cassettes/test_fmp_model/test_get_etf_holdings[VTI].yaml @@ -0,0 +1,48 @@ +interactions: +- request: + body: null + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + method: GET + uri: https://financialmodelingprep.com/api/v3/etf-holder/VTI?apikey=MOCK_API_KEY + response: + body: + string: "{\n \"Error Message\": \"Invalid API KEY. Please retry or visit our + documentation to create one FREE https://site.financialmodelingprep.com/developer/docs\"\n}" + headers: + Access-Control-Allow-Credentials: + - 'true' + Access-Control-Allow-Headers: + - X-Requested-With, content-type, auth-token, Authorization, stripe-signature, + APPS + Access-Control-Allow-Methods: + - GET, POST, OPTIONS + Access-Control-Allow-Origin: + - '*' + Access-Control-Max-Age: + - '3600' + Connection: + - keep-alive + Content-Length: + - '154' + Content-Type: + - application/json; charset=utf-8 + Date: + - Wed, 27 Sep 2023 14:40:52 GMT + ETag: + - W/"9a-ufKRTAmgqkT6Vv3bqU5trChBRE8" + Server: + - nginx/1.18.0 (Ubuntu) + X-Frame-Options: + - SAMEORIGIN + X-Powered-By: + - Express + status: + code: 401 + message: Unauthorized +version: 1 diff --git a/tests/openbb_terminal/etf/cassettes/test_fmp_model/test_get_holdings_pct_change[2023-09-01-2022-09-01-ARKK].yaml b/tests/openbb_terminal/etf/cassettes/test_fmp_model/test_get_holdings_pct_change[2023-09-01-2022-09-01-ARKK].yaml new file mode 100644 index 00000000000..22cfe40220c --- /dev/null +++ b/tests/openbb_terminal/etf/cassettes/test_fmp_model/test_get_holdings_pct_change[2023-09-01-2022-09-01-ARKK].yaml @@ -0,0 +1,94 @@ +interactions: +- request: + body: null + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + method: GET + uri: https://financialmodelingprep.com/api/v3/etf-holder/ARKK?apikey=MOCK_API_KEY + response: + body: + string: "{\n \"Error Message\": \"Invalid API KEY. Please retry or visit our + documentation to create one FREE https://site.financialmodelingprep.com/developer/docs\"\n}" + headers: + Access-Control-Allow-Credentials: + - 'true' + Access-Control-Allow-Headers: + - X-Requested-With, content-type, auth-token, Authorization, stripe-signature, + APPS + Access-Control-Allow-Methods: + - GET, POST, OPTIONS + Access-Control-Allow-Origin: + - '*' + Access-Control-Max-Age: + - '3600' + Connection: + - keep-alive + Content-Length: + - '154' + Content-Type: + - application/json; charset=utf-8 + Date: + - Wed, 27 Sep 2023 17:42:35 GMT + ETag: + - W/"9a-ufKRTAmgqkT6Vv3bqU5trChBRE8" + Server: + - nginx/1.18.0 (Ubuntu) + X-Frame-Options: + - SAMEORIGIN + X-Powered-By: + - Express + status: + code: 401 + message: Unauthorized +- request: + body: null + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + method: GET + uri: https://financialmodelingprep.com/api/v3/etf-holder/ARKK?apikey=MOCK_API_KEY + response: + body: + string: "{\n \"Error Message\": \"Invalid API KEY. Please retry or visit our + documentation to create one FREE https://site.financialmodelingprep.com/developer/docs\"\n}" + headers: + Access-Control-Allow-Credentials: + - 'true' + Access-Control-Allow-Headers: + - X-Requested-With, content-type, auth-token, Authorization, stripe-signature, + APPS + Access-Control-Allow-Methods: + - GET, POST, OPTIONS + Access-Control-Allow-Origin: + - '*' + Access-Control-Max-Age: + - '3600' + Connection: + - keep-alive + Content-Length: + - '154' + Content-Type: + - application/json; charset=utf-8 + Date: + - Wed, 27 Sep 2023 17:43:14 GMT + ETag: + - W/"9a-ufKRTAmgqkT6Vv3bqU5trChBRE8" + Server: + - nginx/1.18.0 (Ubuntu) + X-Frame-Options: + - SAMEORIGIN + X-Powered-By: + - Express + status: + code: 401 + message: Unauthorized +version: 1 diff --git a/tests/openbb_terminal/etf/cassettes/test_fmp_model/test_get_stock_price_change[2023-09-01-2022-09-01-TSLA].yaml b/tests/openbb_terminal/etf/cassettes/test_fmp_model/test_get_stock_price_change[2023-09-01-2022-09-01-TSLA].yaml new file mode 100644 index 00000000000..eb917028176 --- /dev/null +++ b/tests/openbb_terminal/etf/cassettes/test_fmp_model/test_get_stock_price_change[2023-09-01-2022-09-01-TSLA].yaml @@ -0,0 +1,48 @@ +interactions: +- request: + body: null + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + method: GET + uri: https://financialmodelingprep.com/api/v3/etf-holder/TSLA?apikey=MOCK_API_KEY + response: + body: + string: "{\n \"Error Message\": \"Invalid API KEY. Please retry or visit our + documentation to create one FREE https://site.financialmodelingprep.com/developer/docs\"\n}" + headers: + Access-Control-Allow-Credentials: + - 'true' + Access-Control-Allow-Headers: + - X-Requested-With, content-type, auth-token, Authorization, stripe-signature, + APPS + Access-Control-Allow-Methods: + - GET, POST, OPTIONS + Access-Control-Allow-Origin: + - '*' + Access-Control-Max-Age: + - '3600' + Connection: + - keep-alive + Content-Length: + - '154' + Content-Type: + - application/json; charset=utf-8 + Date: + - Wed, 27 Sep 2023 17:42:36 GMT + ETag: + - W/"9a-ufKRTAmgqkT6Vv3bqU5trChBRE8" + Server: + - nginx/1.18.0 (Ubuntu) + X-Frame-Options: + - SAMEORIGIN + X-Powered-By: + - Express + status: + code: 401 + message: Unauthorized +version: 1 diff --git a/tests/openbb_terminal/etf/cassettes/test_fmp_view/test_view_etf_holdings_performance[2023-09-01-2022-09-01-TSLA].yaml b/tests/openbb_terminal/etf/cassettes/test_fmp_view/test_view_etf_holdings_performance[2023-09-01-2022-09-01-TSLA].yaml new file mode 100644 index 00000000000..12a096f5e88 --- /dev/null +++ b/tests/openbb_terminal/etf/cassettes/test_fmp_view/test_view_etf_holdings_performance[2023-09-01-2022-09-01-TSLA].yaml @@ -0,0 +1,48 @@ +interactions: +- request: + body: null + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + method: GET + uri: https://financialmodelingprep.com/api/v3/etf-holder/TSLA?apikey=MOCK_API_KEY + response: + body: + string: "{\n \"Error Message\": \"Invalid API KEY. Please retry or visit our + documentation to create one FREE https://site.financialmodelingprep.com/developer/docs\"\n}" + headers: + Access-Control-Allow-Credentials: + - 'true' + Access-Control-Allow-Headers: + - X-Requested-With, content-type, auth-token, Authorization, stripe-signature, + APPS + Access-Control-Allow-Methods: + - GET, POST, OPTIONS + Access-Control-Allow-Origin: + - '*' + Access-Control-Max-Age: + - '3600' + Connection: + - keep-alive + Content-Length: + - '154' + Content-Type: + - application/json; charset=utf-8 + Date: + - Wed, 27 Sep 2023 17:52:02 GMT + ETag: + - W/"9a-ufKRTAmgqkT6Vv3bqU5trChBRE8" + Server: + - nginx/1.18.0 (Ubuntu) + X-Frame-Options: + - SAMEORIGIN + X-Powered-By: + - Express + status: + code: 401 + message: Unauthorized +version: 1 diff --git a/tests/openbb_terminal/etf/csv/test_fmp_model/test_get_holdings_pct_change[2023-09-01-2022-09-01-ARKK].csv b/tests/openbb_terminal/etf/csv/test_fmp_model/test_get_holdings_pct_change[2023-09-01-2022-09-01-ARKK].csv new file mode 100644 index 00000000000..e16c76dff88 --- /dev/null +++ b/tests/openbb_terminal/etf/csv/test_fmp_model/test_get_holdings_pct_change[2023-09-01-2022-09-01-ARKK].csv @@ -0,0 +1 @@ +"" diff --git a/tests/openbb_terminal/etf/csv/test_fmp_model/test_get_stock_price_change[2023-09-01-2022-09-01-TSLA].csv b/tests/openbb_terminal/etf/csv/test_fmp_model/test_get_stock_price_change[2023-09-01-2022-09-01-TSLA].csv new file mode 100644 index 00000000000..e16c76dff88 --- /dev/null +++ b/tests/openbb_terminal/etf/csv/test_fmp_model/test_get_stock_price_change[2023-09-01-2022-09-01-TSLA].csv @@ -0,0 +1 @@ +"" diff --git a/tests/openbb_terminal/etf/json/test_fmp_model/test_get_etf_holdings[ARKK].json b/tests/openbb_terminal/etf/json/test_fmp_model/test_get_etf_holdings[ARKK].json new file mode 100644 index 00000000000..0637a088a01 --- /dev/null +++ b/tests/openbb_terminal/etf/json/test_fmp_model/test_get_etf_holdings[ARKK].json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/tests/openbb_terminal/etf/test_fmp_model.py b/tests/openbb_terminal/etf/test_fmp_model.py index a542863d3a9..b21403dabdd 100644 --- a/tests/openbb_terminal/etf/test_fmp_model.py +++ b/tests/openbb_terminal/etf/test_fmp_model.py @@ -28,3 +28,66 @@ def test_get_etf_sector_weightings(recorder, name): result = fmp_model.get_etf_sector_weightings(name) recorder.capture(result) + + +@pytest.mark.vcr +@pytest.mark.parametrize( + "ticker", + [ + "ARKK", + ], +) +def test_get_etf_holdings(recorder, ticker): + result = fmp_model.get_etf_holdings(ticker) + + assert result.count != 0 + recorder.capture(result) + +@pytest.mark.vcr(record_mode='all') +@pytest.mark.parametrize( + "ticker", + [ + "ARKK", + ], +) +@pytest.mark.parametrize( + "start_date", + [ + "2022-09-01", + ], +) +@pytest.mark.parametrize( + "end_date", + [ + "2023-09-01", + ], +) +def test_get_holdings_pct_change(recorder, ticker: str, start_date: str, end_date: str): + result = fmp_model.get_holdings_pct_change(ticker, start_date, end_date) + + assert result.empty + recorder.capture(result) + +@pytest.mark.vcr +@pytest.mark.parametrize( + "ticker", + [ + "TSLA", + ], +) +@pytest.mark.parametrize( + "start_date", + [ + "2022-09-01", + ], +) +@pytest.mark.parametrize( + "end_date", + [ + "2023-09-01", + ], +) +def test_get_stock_price_change(recorder, ticker: str, start_date: str, end_date: str): + result = fmp_model.get_holdings_pct_change(ticker, start_date, end_date) + + recorder.capture(result) \ No newline at end of file diff --git a/tests/openbb_terminal/etf/test_fmp_view.py b/tests/openbb_terminal/etf/test_fmp_view.py index 22c7ea30664..21396ef9523 100644 --- a/tests/openbb_terminal/etf/test_fmp_view.py +++ b/tests/openbb_terminal/etf/test_fmp_view.py @@ -24,3 +24,25 @@ def vcr_config(): ) def test_display_etf_weightings(name): fmp_view.display_etf_weightings(name, raw=True, export="") + +@pytest.mark.vcr +@pytest.mark.parametrize( + "ticker", + [ + "TSLA", + ], +) +@pytest.mark.parametrize( + "start_date", + [ + "2022-09-01", + ], +) +@pytest.mark.parametrize( + "end_date", + [ + "2023-09-01", + ], +) +def test_view_etf_holdings_performance(ticker, start_date, end_date): + fmp_view.view_etf_holdings_performance(ticker, start_date, end_date, raw=True, export="") \ No newline at end of file