python.api_test.handlebars Maven / Gradle / Ivy
# coding: utf-8
{{>partial_header}}
import unittest
from unittest.mock import patch
import urllib3
import {{packageName}}
from {{packageName}}.paths.{{operation.nickname}} import {{operation.httpMethod}} # noqa: E501
from {{packageName}} import configuration, schemas, api_client
from .. import ApiTestMixin
{{#with operation}}
class Test{{operationIdSnakeCase}}(ApiTestMixin, unittest.TestCase):
"""
{{operationIdSnakeCase}} unit test stubs
{{#if summary}}
{{{summary}}} # noqa: E501
{{/if}}
"""
_configuration = configuration.Configuration()
def setUp(self):
used_api_client = api_client.ApiClient(configuration=self._configuration)
self.api = {{httpMethod}}.ApiFor{{httpMethod}}(api_client=used_api_client) # noqa: E501
def tearDown(self):
pass
{{#each responses}}
{{#if @first}}
response_status = {{code}}
{{#if content}}
{{#each content}}
{{#if this.testCases}}
{{#each testCases}}
{{#with this }}
def test_{{@key}}_{{#if valid}}passes{{else}}fails{{/if}}(self):
# {{description}}
accept_content_type = '{{{../@key}}}'
with patch.object(urllib3.PoolManager, 'request') as mock_request:
payload = (
{{#with data}}
{{> model_templates/payload_renderer endChar='' }}
{{/with}}
)
mock_request.return_value = self.response(
self.json_bytes(payload),
status=self.response_status
)
{{#if valid}}
{{> api_test_partial }}
assert isinstance(api_response.response, urllib3.HTTPResponse)
assert isinstance(api_response.body, {{httpMethod}}.{{schema.baseName}})
deserialized_response_body = {{httpMethod}}.{{schema.baseName}}.from_openapi_data_oapg(
payload,
_configuration=self._configuration
)
assert api_response.body == deserialized_response_body
{{else}}
with self.assertRaises(({{packageName}}.ApiValueError, {{packageName}}.ApiTypeError)):
self.api.{{httpMethod}}(
accept_content_types=(accept_content_type,)
)
self.assert_pool_manager_request_called_with(
mock_request,
self._configuration.host + '{{{path}}}',
method='{{httpMethod}}'.upper(),
content_type=None,
accept_content_type=accept_content_type,
)
{{/if}}
{{/with}}
{{/each}}
{{/if}}
{{/each}}
{{else}}
response_body = ''
{{/if}}
{{/if}}
{{/each}}
{{#if bodyParam}}
{{#with bodyParam}}
{{#if required}}
{{#each content}}
{{#if this.testCases}}
{{#each testCases}}
{{#with this }}
def test_{{@key}}_{{#if valid}}passes{{else}}fails{{/if}}(self):
content_type = '{{{../@key}}}'
# {{description}}
with patch.object(urllib3.PoolManager, 'request') as mock_request:
payload = (
{{#with data}}
{{> model_templates/payload_renderer endChar='' }}
{{/with}}
)
{{#if valid}}
body = {{httpMethod}}.{{schema.baseName}}.from_openapi_data_oapg(
payload,
_configuration=self._configuration
)
mock_request.return_value = self.response(
self.json_bytes(self.response_body),
status=self.response_status
)
{{> api_test_partial }}
assert isinstance(api_response.response, urllib3.HTTPResponse)
assert isinstance(api_response.body, schemas.Unset)
{{else}}
with self.assertRaises(({{packageName}}.ApiValueError, {{packageName}}.ApiTypeError)):
body = {{httpMethod}}.{{schema.baseName}}.from_openapi_data_oapg(
payload,
_configuration=self._configuration
)
self.api.{{httpMethod}}(body=body)
{{/if}}
{{/with}}
{{/each}}
{{/if}}
{{/each}}
{{/if}}
{{/with}}
{{else}}
{{/if}}
{{/with}}
if __name__ == '__main__':
unittest.main()