diff options
author | Igor Radovanovic <74266147+IgorWounds@users.noreply.github.com> | 2024-03-14 14:54:03 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-14 13:54:03 +0000 |
commit | 6d259a50c52d2c162a56cd1a6aaa4d903fa6ff4d (patch) | |
tree | 2ec08754c6545662fd21f428433d12e5c73b694d | |
parent | a2ef2adc5cf22d929f4dbf4a684ab8a81b2b6ddb (diff) |
Add unit tests (#6213)
11 files changed, 281 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore index a3530870231..85d7f18c32c 100644 --- a/.gitignore +++ b/.gitignore @@ -45,6 +45,7 @@ openbb_terminal/core/plots/assets/plotly*.js .dccache *rome.json **/node_modules/* +.cursorignore # pyinstaller artifacts *.pyo **/dist/* diff --git a/openbb_platform/extensions/econometrics/tests/.gitkeep b/openbb_platform/extensions/econometrics/tests/.gitkeep deleted file mode 100644 index e69de29bb2d..00000000000 --- a/openbb_platform/extensions/econometrics/tests/.gitkeep +++ /dev/null diff --git a/openbb_platform/extensions/econometrics/tests/test_econometrics_utils.py b/openbb_platform/extensions/econometrics/tests/test_econometrics_utils.py new file mode 100644 index 00000000000..57ffd16fbeb --- /dev/null +++ b/openbb_platform/extensions/econometrics/tests/test_econometrics_utils.py @@ -0,0 +1,25 @@ +"""Test the econometrics utils module.""" + +import numpy as np +import pandas as pd +from extensions.econometrics.openbb_econometrics.utils import ( + get_engle_granger_two_step_cointegration_test, + mock_multi_index_data, +) + + +def test_get_engle_granger_two_step_cointegration_test(): + """Test the get_engle_granger_two_step_cointegration_test function.""" + x = pd.Series(np.random.randn(100)) + y = pd.Series(np.random.randn(100)) + + result = get_engle_granger_two_step_cointegration_test(x, y) + + assert result + + +def test_mock_multi_index_data(): + """Test the mock_multi_index_data function.""" + mi_data = mock_multi_index_data() + assert isinstance(mi_data, pd.DataFrame) + assert mi_data.index.nlevels == 2 diff --git a/openbb_platform/extensions/quantitative/tests/.gitkeep b/openbb_platform/extensions/quantitative/tests/.gitkeep deleted file mode 100644 index e69de29bb2d..00000000000 --- a/openbb_platform/extensions/quantitative/tests/.gitkeep +++ /dev/null diff --git a/openbb_platform/extensions/quantitative/tests/test_quantitative_helpers.py b/openbb_platform/extensions/quantitative/tests/test_quantitative_helpers.py new file mode 100644 index 00000000000..82f6ae52b15 --- /dev/null +++ b/openbb_platform/extensions/quantitative/tests/test_quantitative_helpers.py @@ -0,0 +1,14 @@ +"""Test the quantitative helpers.""" + +import pandas as pd +from extensions.quantitative.openbb_quantitative.helpers import ( + validate_window, +) + + +def test_validate_window(): + input_data = pd.Series(range(1, 100)) + validate_window( + input_data=input_data, + window=20, + ) diff --git a/openbb_platform/extensions/technical/tests/.gitkeep b/openbb_platform/extensions/technical/tests/.gitkeep deleted file mode 100644 index e69de29bb2d..00000000000 --- a/openbb_platform/extensions/technical/tests/.gitkeep +++ /dev/null diff --git a/openbb_platform/extensions/technical/tests/test_technical_helpers.py b/openbb_platform/extensions/technical/tests/test_technical_helpers.py new file mode 100644 index 00000000000..c653e6d98e0 --- /dev/null +++ b/openbb_platform/extensions/technical/tests/test_technical_helpers.py @@ -0,0 +1,100 @@ +"""Test the technical helpers module.""" + +import numpy as np +import pandas as pd +import pytest +from extensions.technical.openbb_technical.helpers import ( + calculate_cones, + calculate_fib_levels, + clenow_momentum, + garman_klass, + hodges_tompkins, + parkinson, + rogers_satchell, + validate_data, + yang_zhang, +) + +# pylint: disable=redefined-outer-name + + +@pytest.fixture(scope="module") +def mock_data(top_range: int = 100): + _open = pd.Series(np.arange(1, top_range)) + _high = pd.Series(np.arange(1, top_range)) + _low = pd.Series(np.arange(1, top_range)) + _close = pd.Series(np.arange(1, top_range)) + _volume = pd.Series(np.arange(1, top_range)) + _date = pd.Series(pd.date_range("2021-01-01", periods=top_range - 1, freq="D")) + return pd.DataFrame( + { + "open": _open, + "high": _high, + "low": _low, + "close": _close, + "volume": _volume, + "date": _date, + } + ).set_index("date") + + +def test_parkinson_with_mock_data(mock_data): + """Test parkinson with valid input.""" + result = parkinson(mock_data) + assert not result.empty + + +def test_garman_klass_with_mock_data(mock_data): + """Test garman_klass with valid input.""" + result = garman_klass(mock_data) + assert not result.empty + + +def test_hodges_tompkins_with_mock_data(mock_data): + """Test hodges_tompkins with valid input.""" + result = hodges_tompkins(mock_data) + assert not result.empty + + +def test_rogers_satchell_with_mock_data(mock_data): + """Test rogers_satchell with valid input.""" + result = rogers_satchell(mock_data) + assert not result.empty + + +def test_yang_zhang_with_mock_data(mock_data): + """Test yang_zhang with valid input.""" + result = yang_zhang(mock_data) + assert not result.empty + + +def test_clenow_momentum_with_mock_data(mock_data): + """Test clenow_momentum with valid input.""" + result = clenow_momentum(mock_data["close"]) + assert result + + +def test_calculate_cones_with_mock_data(mock_data): + """Test calculate_cones with valid input.""" + result = calculate_cones( + mock_data, + lower_q=0.1, + upper_q=0.9, + is_crypto=False, + model="STD", + ) + assert not result.empty + + +def test_calculate_fib_levels_with_mock_data(mock_data): + """Test calculate_fib_levels with valid input.""" + result = calculate_fib_levels(mock_data, "close") + assert result + + +def test_validate_data_with_mock_data(mock_data): + """Test validate_data with valid input.""" + try: + validate_data(mock_data["close"].tolist(), 20) + except ValueError: + pytest.fail("validate_data raised ValueError unexpectedly!") diff --git a/openbb_platform/obbject_extensions/charting/tests/test_charting_core_ta_helpers.py b/openbb_platform/obbject_extensions/charting/tests/test_charting_core_ta_helpers.py new file mode 100644 index 00000000000..1804a9df3a4 --- /dev/null +++ b/openbb_platform/obbject_extensions/charting/tests/test_charting_core_ta_helpers.py @@ -0,0 +1,34 @@ +"""Test the charting core ta helpers.""" + +import pandas as pd +import pytest +from obbject_extensions.charting.openbb_charting.core.plotly_ta.ta_helpers import ( + check_columns, +) + + +def test_check_columns(): + """Test check_columns.""" + data = pd.DataFrame( + { + "open": [1, 2, 3, 4, 5], + "high": [1, 2, 3, 4, 5], + "low": [1, 2, 3, 4, 5], + "close": [1, 2, 3, 4, 5], + "volume": [1, 2, 3, 4, 5], + } + ) + result = check_columns(data) + assert result + + +def test_check_columns_fail(): + """Test check_columns.""" + data = pd.DataFrame( + { + "open": [1, 2, 3, 4, 5], + "volume": [1, 2, 3, 4, 5], + } + ) + with pytest.raises(IndexError): + check_columns(data) diff --git a/openbb_platform/providers/tradingeconomics/tests/test_url_generator.py b/openbb_platform/providers/tradingeconomics/tests/test_url_generator.py new file mode 100644 index 00000000000..566282ba09a --- /dev/null +++ b/openbb_platform/providers/tradingeconomics/tests/test_url_generator.py @@ -0,0 +1,73 @@ +"""Test the url generator.""" + +from unittest.mock import Mock + +from providers.tradingeconomics.openbb_tradingeconomics.utils.url_generator import ( + check_args, + generate_url, +) + + +def test_check_args_valid(): + query_args = {"country": "US", "start_date": "2023-01-01", "end_date": "2023-01-31"} + to_include = ["country", "start_date", "end_date"] + assert check_args(query_args, to_include) + + +def test_check_args_invalid(): + query_args = {"country": "US"} + to_include = ["start_date", "end_date"] + assert not check_args(query_args, to_include) + + +def create_query_mock(query_dict): + query_mock = Mock() + query_mock.dict.return_value = query_dict + return query_mock + + +def test_generate_url_country_only(): + query_mock = create_query_mock({"country": "US"}) + expected_url = "https://api.tradingeconomics.com/calendar/country/US?c=" + assert generate_url(query_mock) == expected_url + + +def test_generate_url_country_and_dates(): + query_mock = create_query_mock( + {"country": "US", "start_date": "2023-01-01", "end_date": "2023-01-31"} + ) + expected_url = ( + "https://api.tradingeconomics.com/calendar/country/US/2023-01-01/2023-01-31?c=" + ) + assert generate_url(query_mock) == expected_url + + +def test_generate_url_importance_only(): + query_mock = create_query_mock({"importance": "High"}) + expected_url = "https://api.tradingeconomics.com/calendar?importance=High&c=" + assert generate_url(query_mock) == expected_url + + +def test_generate_url_no_data(): + query_mock = create_query_mock({}) + expected_url = "https://api.tradingeconomics.com/calendar?c=" + assert generate_url(query_mock) == expected_url + + +def test_generate_url_group_only(): + query_mock = create_query_mock({"group": "G20"}) + expected_url = "https://api.tradingeconomics.com/calendar/group/G20?c=" + assert generate_url(query_mock) == expected_url + + +def test_generate_url_country_group_and_dates(): + query_mock = create_query_mock( + { + "country": "US", + "group": "G20", + "start_date": "2023-01-01", + "end_date": "2023-01-31", + } + ) + expected_url = "https://api.tradingeconomics.com/calendar/country/US/group/G20/2023-01-01/2023-01-31?c=" + assert generate_url(query_mock) == expected_url diff --git a/openbb_platform/providers/yfinance/tests/.gitkeep b/openbb_platform/providers/yfinance/tests/.gitkeep deleted file mode 100644 index e69de29bb2d..00000000000 --- a/openbb_platform/providers/yfinance/tests/.gitkeep +++ /dev/null diff --git a/openbb_platform/providers/yfinance/tests/test_yfinance_helpers.py b/openbb_platform/providers/yfinance/tests/test_yfinance_helpers.py new file mode 100644 index 00000000000..9c8b13bca67 --- /dev/null +++ b/openbb_platform/providers/yfinance/tests/test_yfinance_helpers.py @@ -0,0 +1,34 @@ +"""Test yfinance helpers.""" + +import pandas as pd +import pytest + +from providers.yfinance.openbb_yfinance.utils.helpers import ( + df_transform_numbers, + get_futures_data, +) + +# pylint: disable=redefined-outer-name, unused-argument + +MOCK_FUTURES_DATA = pd.DataFrame({"Ticker": ["ES", "NQ"], "Exchange": ["CME", "CME"]}) + + +@pytest.fixture +def mock_futures_csv(monkeypatch): + """Mock pd.read_csv to return predefined futures data.""" + monkeypatch.setattr(pd, "read_csv", lambda *args, **kwargs: MOCK_FUTURES_DATA) + + +def test_get_futures_data(mock_futures_csv): + df = get_futures_data() + assert not df.empty + assert df.equals(MOCK_FUTURES_DATA) + + +def test_df_transform_numbers(): + data = pd.DataFrame( + {"Value": ["1M", "2.5B", "3T"], "% Change": ["1%", "-2%", "3.5%"]} + ) + transformed = df_transform_numbers(data, ["Value", "% Change"]) + assert transformed["Value"].equals(pd.Series([1e6, 2.5e9, 3e12])) + assert transformed["% Change"].equals(pd.Series([1.0, -2.0, 3.5])) |