From 2e5a64fd0bc0e707c5c088eec16bc255c40e070e Mon Sep 17 00:00:00 2001 From: Igor Radovanovic <74266147+IgorWounds@users.noreply.github.com> Date: Wed, 25 Oct 2023 16:52:15 +0200 Subject: [PATCH 1/2] Test tests --- .../tests/test_integration_tests_api.py | 7 +++++ .../tests/test_integration_tests_python.py | 7 +++++ .../utils/integration_tests_api_generator.py | 15 +++++----- .../tests/utils/integration_tests_testers.py | 30 +++++++++++++++++++ 4 files changed, 51 insertions(+), 8 deletions(-) diff --git a/openbb_platform/extensions/tests/test_integration_tests_api.py b/openbb_platform/extensions/tests/test_integration_tests_api.py index e1022c812182..4731c80b4446 100644 --- a/openbb_platform/extensions/tests/test_integration_tests_api.py +++ b/openbb_platform/extensions/tests/test_integration_tests_api.py @@ -4,6 +4,7 @@ from extensions.tests.utils.integration_tests_testers import ( check_missing_integration_test_params, check_missing_integration_test_providers, + check_missing_integration_tests, check_wrong_integration_test_params, get_integration_tests, get_module_functions, @@ -51,3 +52,9 @@ def test_charting_extension_function_coverage() -> None: ] assert missing_items == [], "\n".join(missing_items) + + +def test_missing_api_integration_tests() -> None: + """Check if there are missing tests.""" + missing = check_missing_integration_tests(test_type="api") + assert not missing, "\n".join(missing) diff --git a/openbb_platform/extensions/tests/test_integration_tests_python.py b/openbb_platform/extensions/tests/test_integration_tests_python.py index 9deede09e2ff..af7baee69e73 100644 --- a/openbb_platform/extensions/tests/test_integration_tests_python.py +++ b/openbb_platform/extensions/tests/test_integration_tests_python.py @@ -4,6 +4,7 @@ from extensions.tests.utils.integration_tests_testers import ( check_missing_integration_test_params, check_missing_integration_test_providers, + check_missing_integration_tests, check_wrong_integration_test_params, get_integration_tests, get_module_functions, @@ -51,3 +52,9 @@ def test_charting_extension_function_coverage() -> None: ] assert missing_items == [], "\n".join(missing_items) + + +def test_missing_api_integration_tests() -> None: + """Check if there are missing tests.""" + missing = check_missing_integration_tests(test_type="python") + assert not missing, "\n".join(missing) diff --git a/openbb_platform/extensions/tests/utils/integration_tests_api_generator.py b/openbb_platform/extensions/tests/utils/integration_tests_api_generator.py index 09b7638abf12..75b23dbd0f13 100644 --- a/openbb_platform/extensions/tests/utils/integration_tests_api_generator.py +++ b/openbb_platform/extensions/tests/utils/integration_tests_api_generator.py @@ -1,3 +1,4 @@ +"""Generate API integration tests.""" import argparse import os from typing import Dict, List, Literal, Type, get_type_hints @@ -25,7 +26,6 @@ def get_post_flat_params(hints: Dict[str, Type]): 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 += """ @@ -57,7 +57,6 @@ def write_test_w_template( chart: bool = False, ): """Write the test with the defined template.""" - params_str = ",\n".join([f"({params})" for params in params_list]) http_template_request = { @@ -103,6 +102,7 @@ def test_exists(route: str, path: str): return route.replace("/", "_")[1:] in f.read() +# pylint: disable=W0621 def write_commands_integration_tests( command_map: CommandMap, provider_interface: ProviderInterface, @@ -111,11 +111,11 @@ def write_commands_integration_tests( """Write the commands integration tests.""" commands_not_found = [] - commandmap_map = command_map.map - commandmap_models = command_map.commands_model + cm_map = command_map.map + cm_models = command_map.commands_model provider_interface_map = provider_interface.map - for route in commandmap_map: + for route in cm_map: http_method = get_http_method(api_paths, f"/api/v1{route}") menu = route.split("/")[1] @@ -128,7 +128,7 @@ def write_commands_integration_tests( if not http_method: commands_not_found.append(route) else: - hints = get_type_hints(commandmap_map[route]) + hints = get_type_hints(cm_map[route]) hints.pop("cc", None) hints.pop("return", None) @@ -136,7 +136,7 @@ def write_commands_integration_tests( [{k: "" for k in get_post_flat_params(hints)}] if http_method == "post" else get_test_params( - model_name=commandmap_models[route], + model_name=cm_models[route], provider_interface_map=provider_interface_map, ) ) @@ -154,7 +154,6 @@ def write_commands_integration_tests( def write_charting_extension_integration_tests(): """Write the charting extension integration tests.""" - functions = ChartingService.get_implemented_charting_functions() # we assume test file exists diff --git a/openbb_platform/extensions/tests/utils/integration_tests_testers.py b/openbb_platform/extensions/tests/utils/integration_tests_testers.py index 2c9b11c5e4ac..5dc18adb0d44 100644 --- a/openbb_platform/extensions/tests/utils/integration_tests_testers.py +++ b/openbb_platform/extensions/tests/utils/integration_tests_testers.py @@ -265,6 +265,36 @@ def check_integration_tests( return all_missing_items +def check_missing_integration_tests(test_type: Literal["api", "python"]) -> List[str]: + """Check if all endpoints have integration tests.""" + cm = CommandMap(coverage_sep=".") + routes = [route[1:].replace("/", "_") for route in cm.map] + missing_integration_tests: List[str] = [] + + if test_type == "api": + functions = get_module_functions(get_integration_tests(test_type="api")) + else: + functions = get_module_functions(get_integration_tests(test_type="python")) + + tested_functions = [ + function.replace("test_", "") + for function in functions + if function.startswith("test_") + ] + + for route in routes: + if route not in tested_functions: + # TODO: See how to handle edge cases that are excluded from the schema + # on purpose. This is currently on the econometrics router. + if test_type == "api" and "econometrics" in route: + continue + missing_integration_tests.append( + f"Missing {test_type} integration test for route {route}" + ) + + return missing_integration_tests + + def check_missing_integration_test_providers(functions: Dict[str, Any]) -> List[str]: """Check if there are any missing providers for integration tests.""" return check_integration_tests(functions, check_missing_providers) From ca0d1def14e81a0ea7131dfabee9c48741bfeab9 Mon Sep 17 00:00:00 2001 From: Igor Radovanovic <74266147+IgorWounds@users.noreply.github.com> Date: Wed, 25 Oct 2023 17:34:15 +0200 Subject: [PATCH 2/2] Add test --- .../ta/integration/test_ta_python.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/openbb_platform/extensions/ta/integration/test_ta_python.py b/openbb_platform/extensions/ta/integration/test_ta_python.py index e3541447751a..aecf55bf1913 100644 --- a/openbb_platform/extensions/ta/integration/test_ta_python.py +++ b/openbb_platform/extensions/ta/integration/test_ta_python.py @@ -608,6 +608,24 @@ def test_ta_adx(params, data_type, obb): assert len(result.results) > 0 +@pytest.mark.parametrize( + "params, data_type", + [ + ({"data": "", "index": "", "offset": ""}, "stocks"), + ({"data": "", "index": "date", "offset": "5"}, "crypto"), + ], +) +@pytest.mark.integration +def test_ta_ad(params, data_type, obb): + params = {p: v for p, v in params.items() if v} + params["data"] = get_data(data_type) + + result = obb.ta.ad(**params) + assert result + assert isinstance(result, OBBject) + assert len(result.results) > 0 + + @pytest.mark.parametrize( "params, data_type", [