From 2ead0fa8246e802cb829916214b13ceabdb2bd2f Mon Sep 17 00:00:00 2001 From: 100RABHpy Date: Fri, 2 Apr 2021 15:58:50 +0530 Subject: [PATCH] openapi: Refactor the way we find uncalled curl test functions. In "test_curl_examples.py" we find the functions that registered but never called. To improve readablity, now we have the full implementation in curl_param_value_generators, rather than inspecting its fields from another module. --- zerver/openapi/curl_param_value_generators.py | 10 ++++++++++ zerver/openapi/test_curl_examples.py | 11 ++--------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/zerver/openapi/curl_param_value_generators.py b/zerver/openapi/curl_param_value_generators.py index e21385c74c..4cd6ce318c 100644 --- a/zerver/openapi/curl_param_value_generators.py +++ b/zerver/openapi/curl_param_value_generators.py @@ -55,6 +55,16 @@ def openapi_param_value_generator( return wrapper +def assert_all_helper_functions_called() -> None: + """Throws an exception if any registered helpers were not called by tests""" + if REGISTERED_GENERATOR_FUNCTIONS == CALLED_GENERATOR_FUNCTIONS: + return + + uncalled_functions = str(REGISTERED_GENERATOR_FUNCTIONS - CALLED_GENERATOR_FUNCTIONS) + + raise Exception(f"Registered curl API generators were not called: {uncalled_functions}") + + def patch_openapi_example_values( entry: str, params: List[Dict[str, Any]], diff --git a/zerver/openapi/test_curl_examples.py b/zerver/openapi/test_curl_examples.py index 72a0f9b43e..90be1939d0 100644 --- a/zerver/openapi/test_curl_examples.py +++ b/zerver/openapi/test_curl_examples.py @@ -16,10 +16,7 @@ from zulip import Client from zerver.models import get_realm from zerver.openapi import markdown_extension -from zerver.openapi.curl_param_value_generators import ( - CALLED_GENERATOR_FUNCTIONS, - REGISTERED_GENERATOR_FUNCTIONS, -) +from zerver.openapi.curl_param_value_generators import assert_all_helper_functions_called def test_generated_curl_examples_for_success(client: Client, owner_client: Client) -> None: @@ -117,8 +114,4 @@ To learn more about the test itself, see zerver/openapi/test_curl_examples.py. ) raise - if REGISTERED_GENERATOR_FUNCTIONS != CALLED_GENERATOR_FUNCTIONS: - raise Exception( - "Some registered generator functions were not called:\n" - " " + str(REGISTERED_GENERATOR_FUNCTIONS - CALLED_GENERATOR_FUNCTIONS) - ) + assert_all_helper_functions_called()