forked from loafle/openapi-generator-original
Generate the python into python/ folder directly
This commit is contained in:
0
samples/client/petstore/python/tests/__init__.py
Normal file
0
samples/client/petstore/python/tests/__init__.py
Normal file
133
samples/client/petstore/python/tests/test_api_client.py
Normal file
133
samples/client/petstore/python/tests/test_api_client.py
Normal file
@@ -0,0 +1,133 @@
|
||||
# coding: utf-8
|
||||
|
||||
"""
|
||||
Run the tests.
|
||||
$ pip install nose (optional)
|
||||
$ cd SwaggerPetstore-python
|
||||
$ nosetests -v
|
||||
"""
|
||||
|
||||
import os
|
||||
import time
|
||||
import unittest
|
||||
|
||||
import swagger_client
|
||||
import swagger_client.configuration
|
||||
|
||||
HOST = 'http://petstore.swagger.io/v2'
|
||||
|
||||
|
||||
class ApiClientTests(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.api_client = swagger_client.ApiClient(HOST)
|
||||
|
||||
def test_configuration(self):
|
||||
swagger_client.configuration.api_key['api_key'] = '123456'
|
||||
swagger_client.configuration.api_key_prefix['api_key'] = 'PREFIX'
|
||||
swagger_client.configuration.username = 'test_username'
|
||||
swagger_client.configuration.password = 'test_password'
|
||||
|
||||
header_params = {'test1': 'value1'}
|
||||
query_params = {'test2': 'value2'}
|
||||
auth_settings = ['api_key', 'unknown']
|
||||
|
||||
# test prefix
|
||||
self.assertEqual('PREFIX', swagger_client.configuration.api_key_prefix['api_key'])
|
||||
|
||||
# update parameters based on auth setting
|
||||
self.api_client.update_params_for_auth(header_params, query_params, auth_settings)
|
||||
|
||||
# test api key auth
|
||||
self.assertEqual(header_params['test1'], 'value1')
|
||||
self.assertEqual(header_params['api_key'], 'PREFIX 123456')
|
||||
self.assertEqual(query_params['test2'], 'value2')
|
||||
|
||||
# test basic auth
|
||||
self.assertEqual('test_username', swagger_client.configuration.username)
|
||||
self.assertEqual('test_password', swagger_client.configuration.password)
|
||||
|
||||
def test_select_header_accept(self):
|
||||
accepts = ['APPLICATION/JSON', 'APPLICATION/XML']
|
||||
accept = self.api_client.select_header_accept(accepts)
|
||||
self.assertEqual(accept, 'application/json')
|
||||
|
||||
accepts = ['application/json', 'application/xml']
|
||||
accept = self.api_client.select_header_accept(accepts)
|
||||
self.assertEqual(accept, 'application/json')
|
||||
|
||||
accepts = ['application/xml', 'application/json']
|
||||
accept = self.api_client.select_header_accept(accepts)
|
||||
self.assertEqual(accept, 'application/json')
|
||||
|
||||
accepts = ['text/plain', 'application/xml']
|
||||
accept = self.api_client.select_header_accept(accepts)
|
||||
self.assertEqual(accept, 'text/plain, application/xml')
|
||||
|
||||
accepts = []
|
||||
accept = self.api_client.select_header_accept(accepts)
|
||||
self.assertEqual(accept, None)
|
||||
|
||||
def test_select_header_content_type(self):
|
||||
content_types = ['APPLICATION/JSON', 'APPLICATION/XML']
|
||||
content_type = self.api_client.select_header_content_type(content_types)
|
||||
self.assertEqual(content_type, 'application/json')
|
||||
|
||||
content_types = ['application/json', 'application/xml']
|
||||
content_type = self.api_client.select_header_content_type(content_types)
|
||||
self.assertEqual(content_type, 'application/json')
|
||||
|
||||
content_types = ['application/xml', 'application/json']
|
||||
content_type = self.api_client.select_header_content_type(content_types)
|
||||
self.assertEqual(content_type, 'application/json')
|
||||
|
||||
content_types = ['text/plain', 'application/xml']
|
||||
content_type = self.api_client.select_header_content_type(content_types)
|
||||
self.assertEqual(content_type, 'text/plain')
|
||||
|
||||
content_types = []
|
||||
content_type = self.api_client.select_header_content_type(content_types)
|
||||
self.assertEqual(content_type, 'application/json')
|
||||
|
||||
def test_deserialize_to_dict(self):
|
||||
# dict(str, Pet)
|
||||
json = {
|
||||
'pet': {
|
||||
"id": 0,
|
||||
"category": {
|
||||
"id": 0,
|
||||
"name": "string"
|
||||
},
|
||||
"name": "doggie",
|
||||
"photoUrls": [
|
||||
"string"
|
||||
],
|
||||
"tags": [
|
||||
{
|
||||
"id": 0,
|
||||
"name": "string"
|
||||
}
|
||||
],
|
||||
"status": "available"
|
||||
}
|
||||
}
|
||||
|
||||
data = self.api_client.deserialize(json, 'dict(str, Pet)')
|
||||
self.assertTrue(isinstance(data, dict))
|
||||
self.assertTrue(isinstance(data['pet'], SwaggerPetstore.Pet))
|
||||
|
||||
# dict(str, int)
|
||||
json = {
|
||||
'integer': 1
|
||||
}
|
||||
|
||||
data = self.api_client.deserialize(json, 'dict(str, int)')
|
||||
self.assertTrue(isinstance(data, dict))
|
||||
self.assertTrue(isinstance(data['integer'], int))
|
||||
|
||||
def test_deserialize_to_object(self):
|
||||
data = self.api_client.deserialize("", "object")
|
||||
self.assertTrue(type(data) == object)
|
||||
|
||||
|
||||
|
||||
76
samples/client/petstore/python/tests/test_api_exception.py
Normal file
76
samples/client/petstore/python/tests/test_api_exception.py
Normal file
@@ -0,0 +1,76 @@
|
||||
# coding: utf-8
|
||||
|
||||
"""
|
||||
Run the tests.
|
||||
$ pip install nose (optional)
|
||||
$ cd swagger_client-python
|
||||
$ nosetests -v
|
||||
"""
|
||||
|
||||
import os
|
||||
import time
|
||||
import unittest
|
||||
|
||||
import swagger_client
|
||||
from swagger_client.rest import ApiException
|
||||
|
||||
|
||||
class ApiExceptionTests(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.api_client = swagger_client.ApiClient()
|
||||
self.pet_api = swagger_client.PetApi(self.api_client)
|
||||
self.setUpModels()
|
||||
|
||||
def setUpModels(self):
|
||||
self.category = swagger_client.Category()
|
||||
self.category.id = int(time.time())
|
||||
self.category.name = "dog"
|
||||
self.tag = swagger_client.Tag()
|
||||
self.tag.id = int(time.time())
|
||||
self.tag.name = "blank"
|
||||
self.pet = swagger_client.Pet()
|
||||
self.pet.id = int(time.time())
|
||||
self.pet.name = "hello kity"
|
||||
self.pet.photo_urls = ["http://foo.bar.com/1", "http://foo.bar.com/2"]
|
||||
self.pet.status = "sold"
|
||||
self.pet.category = self.category
|
||||
self.pet.tags = [self.tag]
|
||||
|
||||
def tearDown(self):
|
||||
time.sleep(1)
|
||||
|
||||
def test_404_error(self):
|
||||
self.pet_api.add_pet(body=self.pet)
|
||||
self.pet_api.delete_pet(pet_id=self.pet.id)
|
||||
|
||||
with self.assertRaisesRegexp(ApiException, "Pet not found"):
|
||||
self.pet_api.get_pet_by_id(pet_id=self.pet.id)
|
||||
|
||||
try:
|
||||
self.pet_api.get_pet_by_id(pet_id=self.pet.id)
|
||||
except ApiException as e:
|
||||
self.assertEqual(e.status, 404)
|
||||
self.assertEqual(e.reason, "Not Found")
|
||||
self.assertDictEqual(e.body, {'message': 'Pet not found', 'code': 1, 'type': 'error'})
|
||||
|
||||
def test_500_error(self):
|
||||
self.pet_api.add_pet(body=self.pet)
|
||||
|
||||
with self.assertRaisesRegexp(ApiException, "Internal Server Error"):
|
||||
self.pet_api.upload_file(
|
||||
pet_id=self.pet.id,
|
||||
additional_metadata="special",
|
||||
file=None
|
||||
)
|
||||
|
||||
try:
|
||||
self.pet_api.upload_file(
|
||||
pet_id=self.pet.id,
|
||||
additional_metadata="special",
|
||||
file=None
|
||||
)
|
||||
except ApiException as e:
|
||||
self.assertEqual(e.status, 500)
|
||||
self.assertEqual(e.reason, "Internal Server Error")
|
||||
self.assertRegexpMatches(e.body, "Error 500 Internal Server Error")
|
||||
147
samples/client/petstore/python/tests/test_pet_api.py
Normal file
147
samples/client/petstore/python/tests/test_pet_api.py
Normal file
@@ -0,0 +1,147 @@
|
||||
# coding: utf-8
|
||||
|
||||
"""
|
||||
Run the tests.
|
||||
$ pip install nose (optional)
|
||||
$ cd swagger_client-python
|
||||
$ nosetests -v
|
||||
"""
|
||||
|
||||
import os
|
||||
import time
|
||||
import unittest
|
||||
|
||||
import swagger_client
|
||||
from swagger_client.rest import ApiException
|
||||
|
||||
HOST = 'http://petstore.swagger.io/v2'
|
||||
|
||||
|
||||
class PetApiTests(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.api_client = swagger_client.ApiClient(HOST)
|
||||
self.pet_api = swagger_client.PetApi(self.api_client)
|
||||
self.setUpModels()
|
||||
self.setUpFiles()
|
||||
|
||||
def tearDown(self):
|
||||
# sleep 1 sec between two every 2 tests
|
||||
time.sleep(1)
|
||||
|
||||
def setUpModels(self):
|
||||
self.category = swagger_client.Category()
|
||||
self.category.id = int(time.time())
|
||||
self.category.name = "dog"
|
||||
self.tag = swagger_client.Tag()
|
||||
self.tag.id = int(time.time())
|
||||
self.tag.name = "blank"
|
||||
self.pet = swagger_client.Pet()
|
||||
self.pet.id = int(time.time())
|
||||
self.pet.name = "hello kity"
|
||||
self.pet.photo_urls = ["http://foo.bar.com/1", "http://foo.bar.com/2"]
|
||||
self.pet.status = "sold"
|
||||
self.pet.category = self.category
|
||||
self.pet.tags = [self.tag]
|
||||
|
||||
def setUpFiles(self):
|
||||
self.test_file_dir = os.path.join(os.path.dirname(__file__), "..", "testfiles")
|
||||
self.test_file_dir = os.path.realpath(self.test_file_dir)
|
||||
self.foo = os.path.join(self.test_file_dir, "foo.png")
|
||||
|
||||
def test_create_api_instance(self):
|
||||
pet_api = swagger_client.PetApi()
|
||||
pet_api2 = swagger_client.PetApi()
|
||||
api_client3 = swagger_client.ApiClient()
|
||||
api_client3.user_agent = 'api client 3'
|
||||
api_client4 = swagger_client.ApiClient()
|
||||
api_client4.user_agent = 'api client 4'
|
||||
pet_api3 = swagger_client.PetApi(api_client3)
|
||||
|
||||
# same default api client
|
||||
self.assertEqual(pet_api.api_client, pet_api2.api_client)
|
||||
# confirm using the default api client in the config module
|
||||
self.assertEqual(pet_api.api_client, swagger_client.configuration.api_client)
|
||||
# 2 different api clients are not the same
|
||||
self.assertNotEqual(api_client3, api_client4)
|
||||
# customized pet api not using the default api client
|
||||
self.assertNotEqual(pet_api3.api_client, swagger_client.configuration.api_client)
|
||||
# customized pet api not using the old pet api's api client
|
||||
self.assertNotEqual(pet_api3.api_client, pet_api2.api_client)
|
||||
|
||||
def test_add_pet_and_get_pet_by_id(self):
|
||||
self.pet_api.add_pet(body=self.pet)
|
||||
|
||||
fetched = self.pet_api.get_pet_by_id(pet_id=self.pet.id)
|
||||
self.assertIsNotNone(fetched)
|
||||
self.assertEqual(self.pet.id, fetched.id)
|
||||
self.assertIsNotNone(fetched.category)
|
||||
self.assertEqual(self.pet.category.name, fetched.category.name)
|
||||
|
||||
def test_update_pet(self):
|
||||
self.pet.name = "hello kity with updated"
|
||||
self.pet_api.update_pet(body=self.pet)
|
||||
|
||||
fetched = self.pet_api.get_pet_by_id(pet_id=self.pet.id)
|
||||
self.assertIsNotNone(fetched)
|
||||
self.assertEqual(self.pet.id, fetched.id)
|
||||
self.assertEqual(self.pet.name, fetched.name)
|
||||
self.assertIsNotNone(fetched.category)
|
||||
self.assertEqual(fetched.category.name, self.pet.category.name)
|
||||
|
||||
def test_find_pets_by_status(self):
|
||||
self.pet_api.add_pet(body=self.pet)
|
||||
|
||||
self.assertIn(
|
||||
self.pet.id,
|
||||
list(map(lambda x: getattr(x, 'id'), self.pet_api.find_pets_by_status(status=[self.pet.status])))
|
||||
)
|
||||
|
||||
def test_find_pets_by_tags(self):
|
||||
self.pet_api.add_pet(body=self.pet)
|
||||
|
||||
self.assertIn(
|
||||
self.pet.id,
|
||||
list(map(lambda x: getattr(x, 'id'), self.pet_api.find_pets_by_tags(tags=[self.tag.name])))
|
||||
)
|
||||
|
||||
def test_update_pet_with_form(self):
|
||||
self.pet_api.add_pet(body=self.pet)
|
||||
|
||||
name = "hello kity with form updated"
|
||||
status = "pending"
|
||||
self.pet_api.update_pet_with_form(pet_id=self.pet.id, name=name, status=status)
|
||||
|
||||
fetched = self.pet_api.get_pet_by_id(pet_id=self.pet.id)
|
||||
self.assertEqual(self.pet.id, fetched.id)
|
||||
self.assertEqual(name, fetched.name)
|
||||
self.assertEqual(status, fetched.status)
|
||||
|
||||
def test_upload_file(self):
|
||||
try:
|
||||
additional_metadata = "special"
|
||||
self.pet_api.upload_file(
|
||||
pet_id=self.pet.id,
|
||||
additional_metadata=additional_metadata,
|
||||
file=self.foo
|
||||
)
|
||||
except ApiException as e:
|
||||
self.fail("upload_file() raised {0} unexpectedly".format(type(e)))
|
||||
|
||||
def test_delete_pet(self):
|
||||
self.pet_api.add_pet(body=self.pet)
|
||||
self.pet_api.delete_pet(pet_id=self.pet.id, api_key="special-key")
|
||||
|
||||
try:
|
||||
self.pet_api.get_pet_by_id(pet_id=self.pet.id)
|
||||
raise "expected an error"
|
||||
except ApiException as e:
|
||||
self.assertEqual(404, e.status)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user