"""Tests for generate_openapi script.""" import re from scripts.generate_openapi import append_parameter_descriptions from scripts.generate_openapi import ARGS from scripts.generate_openapi import edit_method_description from scripts.generate_openapi import REGEX_PATTERN from scripts.generate_openapi import RETURNS def test_regex_pattern(doc_comment): """Test regex pattern for doc strings.""" txt = doc_comment start_idx = txt.find(ARGS) end_idx = txt.find(RETURNS) txt = txt[start_idx:end_idx] actual = re.findall(REGEX_PATTERN, txt) expected = [ 'account_id (int): The id of an account', 'contract_id(int): the id of a contract', 'statement_period_id_start (int): the id of the start of a statement period', 'statement_period_id_end (int): the id of the end of a statement period', 'adjustment_type_id (int): the id of the adjustment_type' ] assert expected == actual def test_edit_method_description(doc_comment): """Test editing method description.""" actual = edit_method_description(doc_comment) expected = 'GET ledger adjustments for a specified account.' assert expected == actual def test_append_descriptions(doc_comment): """Test appending description.""" json_dict = { 'paths': { 'fake-endpoint': { 'get': { 'description': doc_comment, 'parameters': [ { 'name': 'account_id', 'in': 'path', 'required': 'true', 'schema': { 'type': 'integer' } }, { 'name': 'contract_id', 'in': 'query', 'required': 'false', 'schema': { 'type': 'integer' }, } ] } } } } actual = append_parameter_descriptions(json_dict) actual_parameters = actual['paths']['fake-endpoint']['get']['parameters'] expected = [ { 'name': 'account_id', 'in': 'path', 'required': 'true', 'schema': { 'type': 'integer' }, 'description': 'account_id (int): The id of an account' }, { 'name': 'contract_id', 'in': 'query', 'required': 'false', 'schema': { 'type': 'integer' }, 'description': 'contract_id(int): the id of a contract' } ] assert expected == actual_parameters