mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-10-27 12:40:35 +00:00
Add unit tests (#6213)
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -45,6 +45,7 @@ openbb_terminal/core/plots/assets/plotly*.js
|
||||
.dccache
|
||||
*rome.json
|
||||
**/node_modules/*
|
||||
.cursorignore
|
||||
# pyinstaller artifacts
|
||||
*.pyo
|
||||
**/dist/*
|
||||
|
||||
@@ -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
|
||||
@@ -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,
|
||||
)
|
||||
@@ -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!")
|
||||
@@ -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)
|
||||
@@ -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
|
||||
@@ -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]))
|
||||
Reference in New Issue
Block a user