diff options
author | Igor Radovanovic <74266147+IgorWounds@users.noreply.github.com> | 2023-09-15 15:19:42 +0200 |
---|---|---|
committer | Igor Radovanovic <74266147+IgorWounds@users.noreply.github.com> | 2023-09-15 15:19:42 +0200 |
commit | ed0bc37038870476789b534425e858c69d0becb8 (patch) | |
tree | 727e0ea0c6ad92ff1d8c57ee6a76f9dfb09fcf14 | |
parent | d3270d2824d2f87a45afddd5faede17adadcde6f (diff) |
Working POC
-rw-r--r-- | openbb_sdk/providers/utils/credentials_schema.py | 27 | ||||
-rw-r--r-- | openbb_sdk/providers/utils/unit_test_generator.py | 42 | ||||
-rw-r--r-- | openbb_sdk/sdk/provider/pyproject.toml | 1 |
3 files changed, 62 insertions, 8 deletions
diff --git a/openbb_sdk/providers/utils/credentials_schema.py b/openbb_sdk/providers/utils/credentials_schema.py new file mode 100644 index 00000000000..1b0f14432ad --- /dev/null +++ b/openbb_sdk/providers/utils/credentials_schema.py @@ -0,0 +1,27 @@ +"""Provider credentials schema used for unit test.""" + +from typing import Dict + +test_credentials: Dict[str, Dict[str, str]] = { + "benzinga": { + "token": "MOCK_TOKEN", + }, + "alpha_vantage": { + "api_key": "MOCK_API_KEY", + }, + "fmp": { + "apikey": "MOCK_API_KEY", + }, + "polygon": { + "apiKey": "MOCK_API_KEY", + }, + "quandl": { + "api_key": "MOCK_API_KEY", + }, + "fred": { + "api_key": "MOCK_API_KEY", + }, + "intrinio": { + "api_key": "MOCK_API_KEY", + }, +} diff --git a/openbb_sdk/providers/utils/unit_test_generator.py b/openbb_sdk/providers/utils/unit_test_generator.py index f448ef700d0..c16c92eab45 100644 --- a/openbb_sdk/providers/utils/unit_test_generator.py +++ b/openbb_sdk/providers/utils/unit_test_generator.py @@ -6,6 +6,8 @@ from openbb_core.app.provider_interface import ProviderInterface from openbb_provider.abstract.fetcher import Fetcher from pydantic.fields import ModelField +from openbb_sdk.providers.utils.credentials_schema import test_credentials + def get_provider_fetchers( available_providers: List[str], @@ -29,7 +31,7 @@ def generate_fetcher_unit_tests(path: str) -> None: """Generate the fetcher unit tests in the provider test folders.""" if not os.path.exists(path): with open(path, "w") as f: - f.write("import pytest\n\n") + f.write("import pytest\nfrom openbb import obb\n") def get_test_params(param_fields: Dict[str, ModelField]) -> Dict[str, Any]: @@ -44,6 +46,8 @@ def get_test_params(param_fields: Dict[str, ModelField]) -> Dict[str, Any]: "symbols": ["AAPL", "MSFT"], "start_date": "2023-01-01", "end_date": "2023-06-06", + "country": "Portugal", + "countries": ["Portugal", "Spain"], } if field_name in example_dict: test_params[field_name] = example_dict[field_name] @@ -62,19 +66,38 @@ def get_test_params(param_fields: Dict[str, ModelField]) -> Dict[str, Any]: return test_params +def write_test_credentials(path: str, provider: str) -> None: + """Write the mocked credentials to the provider test folders.""" + credentials: Dict[str, str] = test_credentials.get(provider, {}) + + template = """ +test_credentials = obb.user.credentials.__dict__ + + +@pytest.fixture(scope="module") +def vcr_config(): + return {{ + "filter_headers": [("User-Agent", None)], + "filter_query_parameters": [ + {credentials_str}, + ], + }} + +""" + with open(path, "a") as f: + f.write(template.format(credentials_str=str(credentials))) + + def write_fetcher_unit_tests() -> None: """Write the fetcher unit tests to the provider test folders.""" provider_interface = ProviderInterface() available_providers = provider_interface.available_providers provider_interface_map = provider_interface.map - # for testing we will only send in two providers - available_providers = ["alpha_vantage", "benzinga"] - test_template = """ -def test_{fetcher_name}(): +@pytest.mark.record_http +def test_{fetcher_name}(credentials=test_credentials): params = {params} - credentials = {credentials} fetcher = {fetcher_name}() result = fetcher.test(params, credentials) @@ -108,7 +131,6 @@ def test_{fetcher_name}(): for model_name, fetcher_dict in provider_fetchers.items(): for fetcher_name, path in fetcher_dict.items(): - # Add logic here to grab the necessary standardized params and credentials test_params = get_test_params( param_fields=provider_interface_map[model_name]["openbb"][ "QueryParams" @@ -117,6 +139,10 @@ def test_{fetcher_name}(): if "forex" in fetcher_name.lower() and "symbol" in test_params: test_params["symbol"] = "EUR/USD" + if "crypto" in fetcher_name.lower() and "symbol" in test_params: + test_params["symbol"] = "BTC/USD" + if "indices" in fetcher_name.lower() and "symbol" in test_params: + test_params["symbol"] = "SPY" with open(path, "a") as f: test_code = test_template.format( @@ -125,4 +151,4 @@ def test_{fetcher_name}(): credentials={}, ) f.write(test_code) - f.write("\n") + f.write("\n\n") diff --git a/openbb_sdk/sdk/provider/pyproject.toml b/openbb_sdk/sdk/provider/pyproject.toml index b6b80f0f68e..3a1d49b7284 100644 --- a/openbb_sdk/sdk/provider/pyproject.toml +++ b/openbb_sdk/sdk/provider/pyproject.toml @@ -12,6 +12,7 @@ pydantic = { extras = ["dotenv"], version = "^1.10" } requests = "^2.31.0" urllib3 = "<2.0.0" importlib-metadata = "^6.8.0" +pytest-recorder = "^0.2.3" [build-system] requires = ["poetry-core"] |