Ignore:
Timestamp:
May 19, 2020, 8:59:28 PM (7 years ago)
Author:
Joachim Langenbach <joachim.langenbach@…>
Branches:
debian/bullseye, master
Children:
24617e1
Parents:
d5199c9
Message:
  • Switched from CamelCase to camel_case naming
    • Added mapservice-webclient to configure script (Closes #33)
    • Switched from unittest to pytest tests
    • Fixed testing errors with post datas by introducing x-fail vendor extension
    • Added x-vendor extension to mark a dependency on mapservice for an operation and the controller_test.mustache adds needed fixtures
    • Copying conftest.py from *_impl/tests into */test (it's a workaround to get the flask app fixtures)
    • Fixed controller_test.mustache, to test the first response with set status code
File:
1 edited

Legend:

Unmodified
Added
Removed
  • buildhelpers/codegen/templates/controller_test.mustache

    rd5199c9 r9f0f35e  
    11# coding: utf-8
    22
    3 from __future__ import absolute_import
    4 import unittest
    5 
    6 from flask import json
    7 from six import BytesIO
     3import pytest
     4import json
    85
    96{{#imports}}{{import}}  # noqa: E501
    107{{/imports}}
    11 from . import BaseTestCase
    128
     9# {{classFilename}} integration test stubs
     10{{#operations}}
     11{{#operation}}
     12{{#vendorExtensions.x-skip-test}}
     13@pytest.mark.skip(reason="{{reason}}")
     14{{/vendorExtensions.x-skip-test}}
     15{{#vendorExtensions.x-xfail-test}}
     16@pytest.mark.xfail(reason=('Reason: '
     17    '{{reason}}'))  # noqa: E501
     18{{/vendorExtensions.x-xfail-test}}
     19def test_{{operationId}}({{#vendorExtensions.x-mapservice-required}}mapservice, {{/vendorExtensions.x-mapservice-required}}client):
     20    """Test case for {{{operationId}}}
     21    {{{summary}}}
     22    """
     23    {{#bodyParam}}
     24    {{paramName}} = {{{example}}}
     25    {{/bodyParam}}
     26    {{#queryParams}}
     27    {{#-first}}query_string = [{{/-first}}{{^-first}}                {{/-first}}('{{paramName}}', '{{{example}}}'){{#hasMore}},{{/hasMore}}{{#-last}}]{{/-last}}
     28    {{/queryParams}}
     29    headers = { {{#vendorExtensions.x-prefered-produce}}
     30        'Accept': '{{mediaType}}',{{/vendorExtensions.x-prefered-produce}}{{#vendorExtensions.x-prefered-consume}}
     31        'Content-Type': '{{mediaType}}',{{/vendorExtensions.x-prefered-consume}}{{#headerParams}}
     32        '{{paramName}}': '{{{example}}}',{{/headerParams}}{{#authMethods}}
     33        {{#isBasicBasic}}'Authorization': 'Basic YXV0b3Rlc3RzOmF1dG90ZXN0cw==',{{/isBasicBasic}}
     34        {{/authMethods}}
     35    }
     36    {{#formParams}}
     37    {{#-first}}data = dict({{/-first}}{{^-first}}            {{/-first}}{{paramName}}={{{example}}}{{#hasMore}},{{/hasMore}}{{#-last}}){{/-last}}
     38    {{/formParams}}
    1339
    14 class {{#operations}}Test{{classname}}(BaseTestCase):
    15     """{{classname}} integration test stubs"""
    16 
    17     {{#operation}}
    18     {{#vendorExtensions.x-skip-test}}
    19     @unittest.skip("{{reason}}")
    20     {{/vendorExtensions.x-skip-test}}
    21     def test_{{operationId}}(self):
    22         """Test case for {{{operationId}}}
    23         {{{summary}}}
    24         """
    25         {{#bodyParam}}
    26         {{paramName}} = {{{example}}}
    27         {{/bodyParam}}
    28         {{#queryParams}}
    29         {{#-first}}query_string = [{{/-first}}{{^-first}}                {{/-first}}('{{paramName}}', '{{{example}}}'){{#hasMore}},{{/hasMore}}{{#-last}}]{{/-last}}
    30         {{/queryParams}}
    31         headers = { {{#vendorExtensions.x-prefered-produce}}
    32             'Accept': '{{mediaType}}',{{/vendorExtensions.x-prefered-produce}}{{#vendorExtensions.x-prefered-consume}}
    33             'Content-Type': '{{mediaType}}',{{/vendorExtensions.x-prefered-consume}}{{#headerParams}}
    34             '{{paramName}}': '{{{example}}}',{{/headerParams}}{{#authMethods}}
    35             {{#isBasicBasic}}'Authorization': 'Basic YXV0b3Rlc3RzOmF1dG90ZXN0cw==',{{/isBasicBasic}}
    36             {{/authMethods}}
    37         }
    38         {{#formParams}}
    39         {{#-first}}data = dict({{/-first}}{{^-first}}            {{/-first}}{{paramName}}={{{example}}}{{#hasMore}},{{/hasMore}}{{#-last}}){{/-last}}
    40         {{/formParams}}
    41         response = self.client.open(
    42             '{{#contextPath}}{{{.}}}{{/contextPath}}{{{path}}}'{{#pathParams}}{{#-first}}.format({{/-first}}{{paramName}}='{{{example}}}'{{#hasMore}}, {{/hasMore}}{{^hasMore}}){{/hasMore}}{{/pathParams}},
    43             method='{{httpMethod}}',
    44             headers=headers{{#bodyParam}},
    45             data=json.dumps({{paramName}}){{^consumes}},
    46             content_type='application/json'{{/consumes}}{{/bodyParam}}{{#formParams}}{{#-first}},
    47             data=data{{/-first}}{{/formParams}}{{#consumes}}{{#-first}},
    48             content_type='{{{mediaType}}}'{{/-first}}{{/consumes}}{{#queryParams}}{{#-first}},
    49             query_string=query_string{{/-first}}{{/queryParams}})
    50         self.assert200(response,
    51                        'Response body is : ' + response.data.decode('utf-8'))
    52 
    53    {{/operation}}
     40    {{#responses}}
     41    {{#-first}}
     42    response = client.open(
     43        '{{#contextPath}}{{{.}}}{{/contextPath}}{{{path}}}'{{#pathParams}}{{#-first}}.format({{/-first}}{{paramName}}='{{{example}}}'{{#hasMore}}, {{/hasMore}}{{^hasMore}}){{/hasMore}}{{/pathParams}},
     44        method='{{httpMethod}}',
     45        headers=headers{{#bodyParam}},
     46        data=json.dumps({{paramName}}){{^consumes}},
     47        content_type='application/json'{{/consumes}}{{/bodyParam}}{{#formParams}}{{#-first}},
     48        data=data{{/-first}}{{/formParams}}{{#consumes}}{{#-first}},
     49        content_type='{{{mediaType}}}'{{/-first}}{{/consumes}}{{#queryParams}}{{#-first}},
     50        query_string=query_string{{/-first}}{{/queryParams}})
     51    try:
     52        response_data = response.data.decode('utf-8')
     53    except Exception as e:
     54        response_data = 'Failed to decode body with {}'.format(repr(e))
     55    assert response.status_code == {{code}}, \
     56        'Response body is : {}'.format(response_data)
     57    {{/-first}}
     58    {{^-first}}
     59    # Skipping responses with status code {{code}}
     60    {{/-first}}
     61    {{/responses}}
     62   
     63   
     64{{/operation}}
    5465{{/operations}}
    55 
    56 if __name__ == '__main__':
    57     unittest.main()
Note: See TracChangeset for help on using the changeset viewer.