diff options
author | Igor Radovanovic <74266147+IgorWounds@users.noreply.github.com> | 2024-03-13 15:09:45 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-13 14:09:45 +0000 |
commit | c0f4f8528e0efe5144e6520a481a39bf05211918 (patch) | |
tree | c1e1f4c0cddf52b1ca6c4d7a068f0fa94c11862d | |
parent | 1e98990c586594ec7a8c566d2a422275dcb20bbb (diff) |
Improve the example test (#6203)
-rw-r--r-- | openbb_platform/extensions/tests/test_routers.py | 1 | ||||
-rw-r--r-- | openbb_platform/extensions/tests/utils/helpers.py | 12 | ||||
-rw-r--r-- | openbb_platform/extensions/tests/utils/router_testers.py | 23 |
3 files changed, 31 insertions, 5 deletions
diff --git a/openbb_platform/extensions/tests/test_routers.py b/openbb_platform/extensions/tests/test_routers.py index 863979dc4ff..cc16eed757f 100644 --- a/openbb_platform/extensions/tests/test_routers.py +++ b/openbb_platform/extensions/tests/test_routers.py @@ -25,6 +25,7 @@ def test_router_examples_rules() -> None: Rules: - All endpoints should have examples. - At least one example using all required parameters. + - All params are valid for the command. """ invalid_examples = check_router_command_examples() assert not invalid_examples, "\n".join(sorted(invalid_examples)) diff --git a/openbb_platform/extensions/tests/utils/helpers.py b/openbb_platform/extensions/tests/utils/helpers.py index 14ee9126f78..ac0d7aaaa9d 100644 --- a/openbb_platform/extensions/tests/utils/helpers.py +++ b/openbb_platform/extensions/tests/utils/helpers.py @@ -271,3 +271,15 @@ def get_required_fields(model: str) -> List[str]: """Get the required fields of a model.""" fields = pi.map[model]["openbb"]["QueryParams"]["fields"] return [field for field, info in fields.items() if info.is_required()] + + +def get_all_fields(model: str) -> List[str]: + """Get all the fields of a model.""" + all_fields: List[str] = [] + info = pi.map[model] + # for every key, grab the fields + for _, provider_info in info.items(): + for field, _ in provider_info["QueryParams"]["fields"].items(): + all_fields.append(field) + + return all_fields diff --git a/openbb_platform/extensions/tests/utils/router_testers.py b/openbb_platform/extensions/tests/utils/router_testers.py index 918b9e7850d..27639b779ef 100644 --- a/openbb_platform/extensions/tests/utils/router_testers.py +++ b/openbb_platform/extensions/tests/utils/router_testers.py @@ -11,6 +11,7 @@ from extensions.tests.utils.helpers import ( collect_routers, find_decorator, find_missing_router_function_models, + get_all_fields, get_decorator_details, get_required_fields, import_routers, @@ -84,16 +85,28 @@ def check_api( examples: str, router_name: str, model: Optional[str], function: Any ) -> List[str]: """Check for API examples.""" - # Check if the endpoint has at least 1 example with all required fields api_example_violation: List[str] = [] parsed_examples = parse_example_string(examples) if model and "APIEx" in parsed_examples: - required_fields = get_required_fields(model.strip("'")) + required_fields = set(get_required_fields(model.strip("'"))) + all_fields = get_all_fields(model.strip("'")) + all_fields.append("provider") + required_fields_met = False + for api_example in parsed_examples["APIEx"]: params = ast.literal_eval(api_example.get("params", "{}")) - if len(set(params.keys()) - set(required_fields) - {"provider"}) == 0: - break - else: + if not required_fields_met and required_fields.issubset(params.keys()): + required_fields_met = True + + # Check for unsupported parameters + for param in params: + if param not in all_fields: + api_example_violation.append( + f"'{router_name}' > '{function.__name__}': param '{param}' is not supported by the command." + ) + + # If after checking all examples, required fields are still not met + if not required_fields_met: api_example_violation.append( f"'{router_name}' > '{function.__name__}': missing example with required fields only > {required_fields}" ) |