[Flask] Add generated tests (#4209)

This commit is contained in:
Christophe Bornet
2016-11-19 09:31:31 +01:00
committed by wing328
parent b02d505ad9
commit df15799839
17 changed files with 806 additions and 38 deletions

View File

@@ -10,7 +10,7 @@ This example uses the [Connexion](https://github.com/zalando/connexion) library
To run the server, please execute the following:
```
sudo pip install -U connexion typing # install Connexion and Typing from PyPI
sudo pip install -U connexion flask_testing typing # install Connexion from PyPI
python app.py
```

View File

@@ -1,6 +1,6 @@
import connexion
from models.pet import Pet
from models.api_response import ApiResponse
from models.pet import Pet
from datetime import date, datetime
from typing import List, Dict
from six import iteritems

View File

@@ -0,0 +1,14 @@
from flask_testing import TestCase
from app import JSONEncoder
import connexion
import logging
class BaseTestCase(TestCase):
def create_app(self):
logging.getLogger('connexion.operation').setLevel('ERROR')
app = connexion.App(__name__, specification_dir='../swagger/')
app.app.json_encoder = JSONEncoder
app.add_api('swagger.yaml')
return app.app

View File

@@ -0,0 +1,118 @@
# coding: utf-8
from __future__ import absolute_import
from models.api_response import ApiResponse
from models.pet import Pet
from . import BaseTestCase
from six import BytesIO
from flask import json
class TestPetController(BaseTestCase):
""" PetController integration test stubs """
def test_add_pet(self):
"""
Test case for add_pet
Add a new pet to the store
"""
body = Pet()
response = self.client.open('/v2/pet',
method='POST',
data=json.dumps(body),
content_type='application/json')
self.assert200(response, "Response body is : " + response.data.decode('utf-8'))
def test_delete_pet(self):
"""
Test case for delete_pet
Deletes a pet
"""
headers = [('apiKey', 'apiKey_example')]
response = self.client.open('/v2/pet/{petId}'.format(petId=789),
method='DELETE',
headers=headers)
self.assert200(response, "Response body is : " + response.data.decode('utf-8'))
def test_find_pets_by_status(self):
"""
Test case for find_pets_by_status
Finds Pets by status
"""
query_string = [('status', 'available')]
response = self.client.open('/v2/pet/findByStatus',
method='GET',
query_string=query_string)
self.assert200(response, "Response body is : " + response.data.decode('utf-8'))
def test_find_pets_by_tags(self):
"""
Test case for find_pets_by_tags
Finds Pets by tags
"""
query_string = [('tags', 'tags_example')]
response = self.client.open('/v2/pet/findByTags',
method='GET',
query_string=query_string)
self.assert200(response, "Response body is : " + response.data.decode('utf-8'))
def test_get_pet_by_id(self):
"""
Test case for get_pet_by_id
Find pet by ID
"""
response = self.client.open('/v2/pet/{petId}'.format(petId=789),
method='GET')
self.assert200(response, "Response body is : " + response.data.decode('utf-8'))
def test_update_pet(self):
"""
Test case for update_pet
Update an existing pet
"""
body = Pet()
response = self.client.open('/v2/pet',
method='PUT',
data=json.dumps(body),
content_type='application/json')
self.assert200(response, "Response body is : " + response.data.decode('utf-8'))
def test_update_pet_with_form(self):
"""
Test case for update_pet_with_form
Updates a pet in the store with form data
"""
data = dict(name='name_example',
status='status_example')
response = self.client.open('/v2/pet/{petId}'.format(petId=789),
method='POST',
data=data,
content_type='application/x-www-form-urlencoded')
self.assert200(response, "Response body is : " + response.data.decode('utf-8'))
def test_upload_file(self):
"""
Test case for upload_file
uploads an image
"""
data = dict(additionalMetadata='additionalMetadata_example',
file=(BytesIO(b'some file data'), 'file.txt'))
response = self.client.open('/v2/pet/{petId}/uploadImage'.format(petId=789),
method='POST',
data=data,
content_type='multipart/form-data')
self.assert200(response, "Response body is : " + response.data.decode('utf-8'))
if __name__ == '__main__':
import unittest
unittest.main()

View File

@@ -0,0 +1,60 @@
# coding: utf-8
from __future__ import absolute_import
from models.order import Order
from . import BaseTestCase
from six import BytesIO
from flask import json
class TestStoreController(BaseTestCase):
""" StoreController integration test stubs """
def test_delete_order(self):
"""
Test case for delete_order
Delete purchase order by ID
"""
response = self.client.open('/v2/store/order/{orderId}'.format(orderId='orderId_example'),
method='DELETE')
self.assert200(response, "Response body is : " + response.data.decode('utf-8'))
def test_get_inventory(self):
"""
Test case for get_inventory
Returns pet inventories by status
"""
response = self.client.open('/v2/store/inventory',
method='GET')
self.assert200(response, "Response body is : " + response.data.decode('utf-8'))
def test_get_order_by_id(self):
"""
Test case for get_order_by_id
Find purchase order by ID
"""
response = self.client.open('/v2/store/order/{orderId}'.format(orderId=5),
method='GET')
self.assert200(response, "Response body is : " + response.data.decode('utf-8'))
def test_place_order(self):
"""
Test case for place_order
Place an order for a pet
"""
body = Order()
response = self.client.open('/v2/store/order',
method='POST',
data=json.dumps(body),
content_type='application/json')
self.assert200(response, "Response body is : " + response.data.decode('utf-8'))
if __name__ == '__main__':
import unittest
unittest.main()

View File

@@ -0,0 +1,112 @@
# coding: utf-8
from __future__ import absolute_import
from models.user import User
from . import BaseTestCase
from six import BytesIO
from flask import json
class TestUserController(BaseTestCase):
""" UserController integration test stubs """
def test_create_user(self):
"""
Test case for create_user
Create user
"""
body = User()
response = self.client.open('/v2/user',
method='POST',
data=json.dumps(body),
content_type='application/json')
self.assert200(response, "Response body is : " + response.data.decode('utf-8'))
def test_create_users_with_array_input(self):
"""
Test case for create_users_with_array_input
Creates list of users with given input array
"""
body = [User()]
response = self.client.open('/v2/user/createWithArray',
method='POST',
data=json.dumps(body),
content_type='application/json')
self.assert200(response, "Response body is : " + response.data.decode('utf-8'))
def test_create_users_with_list_input(self):
"""
Test case for create_users_with_list_input
Creates list of users with given input array
"""
body = [User()]
response = self.client.open('/v2/user/createWithList',
method='POST',
data=json.dumps(body),
content_type='application/json')
self.assert200(response, "Response body is : " + response.data.decode('utf-8'))
def test_delete_user(self):
"""
Test case for delete_user
Delete user
"""
response = self.client.open('/v2/user/{username}'.format(username='username_example'),
method='DELETE')
self.assert200(response, "Response body is : " + response.data.decode('utf-8'))
def test_get_user_by_name(self):
"""
Test case for get_user_by_name
Get user by user name
"""
response = self.client.open('/v2/user/{username}'.format(username='username_example'),
method='GET')
self.assert200(response, "Response body is : " + response.data.decode('utf-8'))
def test_login_user(self):
"""
Test case for login_user
Logs user into the system
"""
query_string = [('username', 'username_example'),
('password', 'password_example')]
response = self.client.open('/v2/user/login',
method='GET',
query_string=query_string)
self.assert200(response, "Response body is : " + response.data.decode('utf-8'))
def test_logout_user(self):
"""
Test case for logout_user
Logs out current logged in user session
"""
response = self.client.open('/v2/user/logout',
method='GET')
self.assert200(response, "Response body is : " + response.data.decode('utf-8'))
def test_update_user(self):
"""
Test case for update_user
Updated user
"""
body = User()
response = self.client.open('/v2/user/{username}'.format(username='username_example'),
method='PUT',
data=json.dumps(body),
content_type='application/json')
self.assert200(response, "Response body is : " + response.data.decode('utf-8'))
if __name__ == '__main__':
import unittest
unittest.main()