From 44c25619d73ca24d16a809b0517a393b6dcfbb4e Mon Sep 17 00:00:00 2001 From: Suyash Vardhan Mathur Date: Fri, 4 Jun 2021 17:10:46 +0530 Subject: [PATCH] curl_examples: Add testing for template endpoints. This commit adds support for testing of those endpoints whose .md files would be deleted in favour of their pages to be generated automatically by the template. curl examples for such endpoints must exist in accordance to the pattern of template, which can be used to run the tests for them. --- zerver/openapi/test_curl_examples.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/zerver/openapi/test_curl_examples.py b/zerver/openapi/test_curl_examples.py index 4fbcbe54db..3edbd6e080 100644 --- a/zerver/openapi/test_curl_examples.py +++ b/zerver/openapi/test_curl_examples.py @@ -22,6 +22,7 @@ from zerver.openapi.curl_param_value_generators import ( AUTHENTICATION_LINE, assert_all_helper_functions_called, ) +from zerver.openapi.openapi import get_endpoint_from_operationid def test_generated_curl_examples_for_success(client: Client) -> None: @@ -49,7 +50,8 @@ def test_generated_curl_examples_for_success(client: Client) -> None: file_name = os.path.join(settings.DEPLOY_ROOT, "templates/zerver/api/", article_name) curl_commands_to_test = [] - with open(file_name) as f: + if os.path.exists(file_name): + f = open(file_name, "r") for line in f: # Set AUTHENTICATION_LINE to default_authentication_line. # Set this every iteration, because deactivate_own_user @@ -59,6 +61,17 @@ def test_generated_curl_examples_for_success(client: Client) -> None: # {generate_code_example(curl, ...} if line.startswith("{generate_code_example(curl"): curl_commands_to_test.append(line) + else: + # If the file doesn't exist, then it has been + # deleted and its page is generated by the + # template. Thus, the curl example would just + # a single one following the template's pattern. + endpoint_path, endpoint_method = get_endpoint_from_operationid( + endpoint.replace("-", "_") + ) + endpoint_string = endpoint_path + ":" + endpoint_method + command = f"{{generate_code_example(curl)|{endpoint_string}|example}}" + curl_commands_to_test.append(command) for line in curl_commands_to_test: # To do an end-to-end test on the documentation examples