Skip to content

Commit

Permalink
Clean up the newly created ETF extension (OpenBB-finance#5611)
Browse files Browse the repository at this point in the history
* Add ETF to dev_install bundle

* Add integration tests for openbb-etf

* Patch integration test generator scripts to produce lintable code

* Add py.typed markers for extensions
  • Loading branch information
piiq authored Oct 27, 2023
1 parent deebdce commit 2764b7d
Show file tree
Hide file tree
Showing 20 changed files with 119 additions and 10 deletions.
1 change: 1 addition & 0 deletions openbb_platform/dev_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
openbb-crypto = { path = "./extensions/crypto", develop = true }
openbb-economy = { path = "./extensions/economy", develop = true }
openbb-etf = { path = "./extensions/etf", develop = true }
openbb-forex = { path = "./extensions/forex", develop = true }
openbb-fixedincome = { path = "./extensions/fixedincome", develop = true }
openbb-news = { path = "./extensions/news", develop = true }
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
51 changes: 51 additions & 0 deletions openbb_platform/extensions/etf/integration/test_etf_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import base64

import pytest
import requests
from openbb_core.env import Env
from openbb_provider.utils.helpers import get_querystring


@pytest.fixture(scope="session")
def headers():
userpass = f"{Env().API_USERNAME}:{Env().API_PASSWORD}"
userpass_bytes = userpass.encode("ascii")
base64_bytes = base64.b64encode(userpass_bytes)

return {"Authorization": f"Basic {base64_bytes.decode('ascii')}"}


# pylint: disable=redefined-outer-name


@pytest.mark.parametrize(
"params",
[({})],
)
@pytest.mark.integration
def test_etf_search(params, headers):
params = {p: v for p, v in params.items() if v}

query_str = get_querystring(params, [])
url = f"http://0.0.0.0:8000/api/v1/etf/search?{query_str}"
result = requests.get(url, headers=headers, timeout=10)
assert isinstance(result, requests.Response)
assert result.status_code == 200


@pytest.mark.parametrize(
"params",
[
({"symbol": "IOO", "start_date": "2023-01-01", "end_date": "2023-06-06"}),
({"symbol": "MISL", "start_date": "2023-01-01", "end_date": "2023-06-06"}),
],
)
@pytest.mark.integration
def test_etf_historical(params, headers):
params = {p: v for p, v in params.items() if v}

query_str = get_querystring(params, [])
url = f"http://0.0.0.0:8000/api/v1/etf/historical?{query_str}"
result = requests.get(url, headers=headers, timeout=10)
assert isinstance(result, requests.Response)
assert result.status_code == 200
49 changes: 49 additions & 0 deletions openbb_platform/extensions/etf/integration/test_etf_python.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""Test etf extension."""
import pytest
from openbb_core.app.model.obbject import OBBject


@pytest.fixture(scope="session")
def obb(pytestconfig): # pylint: disable=inconsistent-return-statements
"""Fixture to setup obb."""

if pytestconfig.getoption("markexpr") != "not integration":
import openbb # pylint: disable=import-outside-toplevel

return openbb.obb


# pylint: disable=redefined-outer-name


@pytest.mark.parametrize(
"params",
[
({}),
],
)
@pytest.mark.integration
def test_etf_search(params, obb):
params = {p: v for p, v in params.items() if v}

result = obb.etf.search(**params)
assert result
assert isinstance(result, OBBject)
assert len(result.results) > 0


@pytest.mark.parametrize(
"params",
[
({"symbol": "IOO", "start_date": "2023-01-01", "end_date": "2023-06-06"}),
({"symbol": "MISL", "start_date": "2023-01-01", "end_date": "2023-06-06"}),
],
)
@pytest.mark.integration
def test_etf_historical(params, obb):
params = {p: v for p, v in params.items() if v}

result = obb.etf.historical(**params)
assert result
assert isinstance(result, OBBject)
assert len(result.results) > 0
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ def write_init_test_template(http_method: str, path: str):
"""Write some common initialization for the tests with the defined template."""
http_template_imports = {"get": "", "post": "import json"}
template = http_template_imports[http_method]
template += """
template += """import base64
import pytest
import requests
from openbb_provider.utils.helpers import get_querystring
import base64
from openbb_core.env import Env
from openbb_provider.utils.helpers import get_querystring
@pytest.fixture(scope="session")
Expand All @@ -43,6 +43,10 @@ def headers():
base64_bytes = base64.b64encode(userpass_bytes)
return {"Authorization": f"Basic {base64_bytes.decode('ascii')}"}
# pylint: disable=redefined-outer-name
"""

with open(path, "w") as f:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ def create_integration_test_files(extensions: List[PosixPath]) -> None:
for extension in extensions:
extension_name = extension.name
test_file_name = f"test_{extension_name}_python.py"
if not (extension / "integration").exists():
(extension / "integration").mkdir()
test_file = extension / "integration" / test_file_name
if not test_file.exists():
with open(test_file, "w", encoding="utf-8", newline="\n") as f:
Expand All @@ -60,15 +62,19 @@ def create_integration_test_files(extensions: List[PosixPath]) -> None:
import pytest
from openbb_core.app.model.obbject import OBBject
@pytest.fixture(scope="session")
def obb(pytestconfig):
def obb(pytestconfig): # pylint: disable=inconsistent-return-statements
"""Fixture to setup obb."""
if pytestconfig.getoption("markexpr") != "not integration":
import openbb
import openbb # pylint: disable=import-outside-toplevel
return openbb.obb
'''
# pylint: disable=redefined-outer-name
'''
)


Expand Down Expand Up @@ -163,9 +169,7 @@ def test_exists(command_name: str, path: str):

def write_to_file_w_template(test_file, params_list, full_command, test_name, extra=""):
"""Write to file with template."""
params = ""
for test_params in params_list:
params += f"({test_params}),\n"
params = ",\n".join(f"({test_params})" for test_params in params_list)

if not test_exists(command_name=full_command, path=test_file):
with open(test_file, "a", encoding="utf-8", newline="\n") as f:
Expand Down Expand Up @@ -270,7 +274,7 @@ def write_commands_integration_tests() -> None:

def write_charting_extension_integration_tests():
"""Write charting extension integration tests."""
import openbb_charting # pylint: disable=W0611
import openbb_charting # pylint: disable=import-outside-toplevel

functions = ChartingService.get_implemented_charting_functions()

Expand Down
Empty file added openbb_platform/openbb/py.typed
Empty file.
Empty file.
Empty file.

0 comments on commit 2764b7d

Please sign in to comment.