From 74a232c830de5620ffe93cdf13cc8d9b57ce957e Mon Sep 17 00:00:00 2001 From: Igor Radovanovic <74266147+IgorWounds@users.noreply.github.com> Date: Thu, 28 Sep 2023 15:30:16 +0200 Subject: [PATCH] Test provider coverage of the python interface integration tests --- .../tests/test_integration_tests.py | 15 ++++ .../tests/utils/integration_tests_testers.py | 80 +++++++++++++++++++ pytest.ini | 1 + 3 files changed, 96 insertions(+) create mode 100644 openbb_sdk/extensions/tests/test_integration_tests.py create mode 100644 openbb_sdk/extensions/tests/utils/integration_tests_testers.py diff --git a/openbb_sdk/extensions/tests/test_integration_tests.py b/openbb_sdk/extensions/tests/test_integration_tests.py new file mode 100644 index 00000000000..7908a19076e --- /dev/null +++ b/openbb_sdk/extensions/tests/test_integration_tests.py @@ -0,0 +1,15 @@ +"""Test the integration tests.""" +from extensions.tests.utils.integration_tests_testers import ( + check_missing_integration_test_providers, + get_module_functions, + get_python_integration_tests, +) + + +def test_python_interface_integration_tests() -> None: + """Test if there are any missing providers for integration tests.""" + integration_tests = get_python_integration_tests() + functions = get_module_functions(integration_tests) + missing_providers = check_missing_integration_test_providers(functions) + + assert not missing_providers, "\n".join(missing_providers) diff --git a/openbb_sdk/extensions/tests/utils/integration_tests_testers.py b/openbb_sdk/extensions/tests/utils/integration_tests_testers.py new file mode 100644 index 00000000000..c085475246c --- /dev/null +++ b/openbb_sdk/extensions/tests/utils/integration_tests_testers.py @@ -0,0 +1,80 @@ +"""Test if there are any missing providers for python interface integration tests.""" +import importlib.util +import inspect +import os +from typing import Any, Dict, List + +from extensions.tests.utils.integration_tests_generator import find_extensions +from sdk.core.openbb_core.app.provider_interface import ProviderInterface +from sdk.core.openbb_core.app.router import CommandMap + + +def get_python_integration_tests() -> List[Any]: + """Get integration tests for the python interface.""" + integration_tests: List[Any] = [] + + for extension in find_extensions(): + integration_folder = os.path.join(extension, "integration") + for file in os.listdir(integration_folder): + if file.endswith("_python.py"): + file_path = os.path.join(integration_folder, file) + module_name = file[:-3] # Remove .py from file name + + spec = importlib.util.spec_from_file_location(module_name, file_path) + if spec: + module = importlib.util.module_from_spec(spec) + if spec.loader: + spec.loader.exec_module(module) + integration_tests.append(module) + + return integration_tests + + +def get_module_functions(module_list: List[Any]) -> Dict[str, Any]: + """Get all functions from a list of modules.""" + functions = {} + for module in module_list: + for name, obj in inspect.getmembers(module): + if inspect.isfunction(obj): + functions[name] = obj + return functions + + +def check_missing_integration_test_providers(functions: Dict[str, Any]) -> List[str]: + """Check if there are any missing providers for integration tests.""" + pi = ProviderInterface() + provider_interface_map = pi.map + cm = CommandMap(coverage_sep=".") + + missing_providers: List[str] = [] + + for command, model in cm.commands_model.items(): + for function in functions: + if command[1:].replace(".", "_") == function.replace("test_", ""): + command_params: Dict[str, Dict[str, dict]] = provider_interface_map[ + model + ] + function_params: List[dict] = functions[function].pytestmark[1].args[1] + + providers = list(command_params.keys()) + providers.remove("openbb") + + for test_params in function_params: + provider = test_params.get("provider", None) + if provider: + try: # noqa + providers.remove(provider) + except ValueError: + pass + + if providers: + # if there is only one provider left and the length of the + # test_params is 1, we can ignore because it is picked up by default + if len(providers) == 1 and len(function_params) == 1: + pass + else: + missing_providers.append( + f"Missing providers for {function}: {providers}" + ) + + return missing_providers diff --git a/pytest.ini b/pytest.ini index 3454f71f138..1b499680ac9 100644 --- a/pytest.ini +++ b/pytest.ini @@ -2,6 +2,7 @@ addopts = -p no:warnings markers = linux: tests that are not stable on Windows + integration: OpenBB Platform integration test marker testpaths = tests openbb_sdk/**/tests