forked from loafle/openapi-generator-original
rename python-nextgen to python (#15504)
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
# coding: utf-8
|
||||
|
||||
# flake8: noqa
|
||||
|
||||
import unittest
|
||||
import weakref
|
||||
|
||||
from tests.util import async_test
|
||||
import petstore_api
|
||||
|
||||
|
||||
class TestApiClient(unittest.TestCase):
|
||||
|
||||
@async_test
|
||||
async def test_context_manager_closes_client(self):
|
||||
|
||||
async with petstore_api.ApiClient() as client:
|
||||
# thread pool
|
||||
self.assertIsNotNone(client.pool)
|
||||
pool_ref = weakref.ref(client._pool)
|
||||
self.assertIsNotNone(pool_ref())
|
||||
# pool_manager
|
||||
self.assertFalse(client.rest_client.pool_manager.closed)
|
||||
rest_pool_ref = client.rest_client.pool_manager
|
||||
|
||||
self.assertIsNone(pool_ref())
|
||||
self.assertTrue(rest_pool_ref.closed)
|
||||
@@ -0,0 +1,237 @@
|
||||
# coding: utf-8
|
||||
|
||||
# flake8: noqa
|
||||
|
||||
|
||||
import os
|
||||
import time
|
||||
import unittest
|
||||
|
||||
import petstore_api
|
||||
|
||||
|
||||
class ModelTests(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.pet = petstore_api.Pet(name="test name", photo_urls=["string"])
|
||||
self.pet.id = 1
|
||||
self.pet.status = "available"
|
||||
cate = petstore_api.Category(name="dog")
|
||||
cate.id = 1
|
||||
cate.name = "dog"
|
||||
self.pet.category = cate
|
||||
tag = petstore_api.Tag()
|
||||
tag.id = 1
|
||||
self.pet.tags = [tag]
|
||||
|
||||
def test_cat(self):
|
||||
self.cat = petstore_api.Cat(class_name="cat")
|
||||
self.assertEqual("cat", self.cat.class_name)
|
||||
self.assertEqual("red", self.cat.color)
|
||||
cat_str = "{'className': 'cat', 'color': 'red', 'declawed': None}"
|
||||
self.assertEqual(cat_str, self.cat.to_str())
|
||||
|
||||
def test_to_str(self):
|
||||
data = ("{'category': {'id': 1, 'name': 'dog'},\n"
|
||||
" 'id': 1,\n"
|
||||
" 'name': 'test name',\n"
|
||||
" 'photoUrls': ['string'],\n"
|
||||
" 'status': 'available',\n"
|
||||
" 'tags': [{'id': 1, 'name': None}]}")
|
||||
self.assertEqual(data, self.pet.to_str())
|
||||
|
||||
def test_equal(self):
|
||||
self.pet1 = petstore_api.Pet(name="test name", photo_urls=["string"])
|
||||
self.pet1.id = 1
|
||||
self.pet1.status = "available"
|
||||
cate1 = petstore_api.Category(name="dog")
|
||||
cate1.id = 1
|
||||
# cate1.name = "dog"
|
||||
self.pet.category = cate1
|
||||
tag1 = petstore_api.Tag()
|
||||
tag1.id = 1
|
||||
self.pet1.tags = [tag1]
|
||||
|
||||
self.pet2 = petstore_api.Pet(name="test name", photo_urls=["string"])
|
||||
self.pet2.id = 1
|
||||
self.pet2.status = "available"
|
||||
cate2 = petstore_api.Category(name="dog")
|
||||
cate2.id = 1
|
||||
cate2.name = "dog"
|
||||
self.pet.category = cate2
|
||||
tag2 = petstore_api.Tag()
|
||||
tag2.id = 1
|
||||
self.pet2.tags = [tag2]
|
||||
|
||||
self.assertTrue(self.pet1 == self.pet2)
|
||||
|
||||
# reset pet1 tags to empty array so that object comparison returns false
|
||||
self.pet1.tags = []
|
||||
self.assertFalse(self.pet1 == self.pet2)
|
||||
|
||||
def test_oneOf(self):
|
||||
# test new Pig
|
||||
new_pig = petstore_api.Pig()
|
||||
self.assertEqual("null", new_pig.to_json())
|
||||
self.assertEqual(None, new_pig.actual_instance)
|
||||
|
||||
# test from_json
|
||||
json_str = '{"className": "BasquePig", "color": "red"}'
|
||||
p = petstore_api.Pig.from_json(json_str)
|
||||
self.assertIsInstance(p.actual_instance, petstore_api.BasquePig)
|
||||
|
||||
# test init
|
||||
basque_pig = p.actual_instance
|
||||
pig2 = petstore_api.Pig(actual_instance=basque_pig)
|
||||
self.assertIsInstance(pig2.actual_instance, petstore_api.BasquePig)
|
||||
|
||||
# test failed init
|
||||
try:
|
||||
pig3 = petstore_api.Pig(actual_instance="123")
|
||||
self.assertTrue(False) # this line shouldn't execute
|
||||
except ValueError as e:
|
||||
self.assertTrue(
|
||||
"No match found when setting `actual_instance` in Pig with oneOf schemas: BasquePig, DanishPig" in str(e))
|
||||
|
||||
# failure
|
||||
try:
|
||||
p2 = petstore_api.Pig.from_json("1")
|
||||
self.assertTrue(False) # this line shouldn't execute
|
||||
except ValueError as e:
|
||||
error_message = (
|
||||
"No match found when deserializing the JSON string into Pig with oneOf schemas: BasquePig, DanishPig. "
|
||||
"Details: 1 validation error for BasquePig\n"
|
||||
"__root__\n"
|
||||
" BasquePig expected dict not int (type=type_error), 1 validation error for DanishPig\n"
|
||||
"__root__\n"
|
||||
" DanishPig expected dict not int (type=type_error)")
|
||||
self.assertEqual(str(e), error_message)
|
||||
|
||||
# test to_json
|
||||
self.assertEqual(p.to_json(), '{"className": "BasquePig", "color": "red"}')
|
||||
|
||||
# test nested property
|
||||
nested = petstore_api.WithNestedOneOf(size = 1, nested_pig = p)
|
||||
self.assertEqual(nested.to_json(), '{"size": 1, "nested_pig": {"className": "BasquePig", "color": "red"}}')
|
||||
|
||||
nested_json = nested.to_json()
|
||||
nested2 = petstore_api.WithNestedOneOf.from_json(nested_json)
|
||||
self.assertEqual(nested2.to_json(), nested_json)
|
||||
|
||||
def test_anyOf(self):
|
||||
# test new AnyOfPig
|
||||
new_anypig = petstore_api.AnyOfPig()
|
||||
self.assertEqual("null", new_anypig.to_json())
|
||||
self.assertEqual(None, new_anypig.actual_instance)
|
||||
|
||||
# test from_json
|
||||
json_str = '{"className": "BasquePig", "color": "red"}'
|
||||
p = petstore_api.AnyOfPig.from_json(json_str)
|
||||
self.assertIsInstance(p.actual_instance, petstore_api.BasquePig)
|
||||
|
||||
# test init
|
||||
basque_pig = p.actual_instance
|
||||
pig2 = petstore_api.Pig(actual_instance=basque_pig)
|
||||
self.assertIsInstance(pig2.actual_instance, petstore_api.BasquePig)
|
||||
|
||||
# test failed init
|
||||
try:
|
||||
pig3 = petstore_api.AnyOfPig(actual_instance="123")
|
||||
self.assertTrue(False) # this line shouldn't execute
|
||||
except ValueError as e:
|
||||
self.assertTrue(
|
||||
"No match found when setting the actual_instance in AnyOfPig with anyOf schemas: BasquePig, "
|
||||
"DanishPig" in str(e))
|
||||
|
||||
# failure
|
||||
try:
|
||||
p2 = petstore_api.AnyOfPig.from_json("1")
|
||||
self.assertTrue(False) # this line shouldn't execute
|
||||
except ValueError as e:
|
||||
error_message = (
|
||||
"No match found when deserializing the JSON string into AnyOfPig with anyOf schemas: BasquePig, "
|
||||
"DanishPig. Details: 1 validation error for BasquePig\n"
|
||||
"__root__\n"
|
||||
" BasquePig expected dict not int (type=type_error), 1 validation error for DanishPig\n"
|
||||
"__root__\n"
|
||||
" DanishPig expected dict not int (type=type_error)")
|
||||
self.assertEqual(str(e), error_message)
|
||||
|
||||
# test to_json
|
||||
self.assertEqual(p.to_json(), '{"className": "BasquePig", "color": "red"}')
|
||||
|
||||
def test_inheritance(self):
|
||||
dog = petstore_api.Dog(breed="bulldog", className="dog", color="white")
|
||||
self.assertEqual(dog.to_json(), '{"className": "dog", "color": "white", "breed": "bulldog"}')
|
||||
self.assertEqual(dog.to_dict(), {'breed': 'bulldog', 'className':
|
||||
'dog', 'color': 'white'})
|
||||
dog2 = petstore_api.Dog.from_json(dog.to_json())
|
||||
self.assertEqual(dog2.breed, 'bulldog')
|
||||
self.assertEqual(dog2.class_name, "dog")
|
||||
self.assertEqual(dog2.color, 'white')
|
||||
|
||||
def test_list(self):
|
||||
# should throw exception as var_123_list should be string
|
||||
try:
|
||||
l3 = petstore_api.List(var_123_list=123)
|
||||
self.assertTrue(False) # this line shouldn't execute
|
||||
except ValueError as e:
|
||||
#error_message = (
|
||||
# "1 validation error for List\n"
|
||||
# "123-list\n"
|
||||
# " str type expected (type=type_error.str)\n")
|
||||
self.assertTrue("str type expected" in str(e))
|
||||
|
||||
l = petstore_api.List(var_123_list="bulldog")
|
||||
self.assertEqual(l.to_json(), '{"123-list": "bulldog"}')
|
||||
self.assertEqual(l.to_dict(), {'123-list': 'bulldog'})
|
||||
l2 = petstore_api.List.from_json(l.to_json())
|
||||
self.assertEqual(l2.var_123_list, 'bulldog')
|
||||
|
||||
self.assertTrue(isinstance(l2, petstore_api.List))
|
||||
|
||||
def test_enum_ref_property(self):
|
||||
# test enum ref property
|
||||
# test to_json
|
||||
d = petstore_api.OuterObjectWithEnumProperty(value=petstore_api.OuterEnumInteger.NUMBER_1)
|
||||
self.assertEqual(d.to_json(), '{"value": 1}')
|
||||
d2 = petstore_api.OuterObjectWithEnumProperty(value=petstore_api.OuterEnumInteger.NUMBER_1, str_value=petstore_api.OuterEnum.DELIVERED)
|
||||
self.assertEqual(d2.to_json(), '{"str_value": "delivered", "value": 1}')
|
||||
# test from_json (round trip)
|
||||
d3 = petstore_api.OuterObjectWithEnumProperty.from_json(d2.to_json())
|
||||
self.assertEqual(d3.str_value, petstore_api.OuterEnum.DELIVERED)
|
||||
self.assertEqual(d3.value, petstore_api.OuterEnumInteger.NUMBER_1)
|
||||
self.assertEqual(d3.to_json(), '{"str_value": "delivered", "value": 1}')
|
||||
|
||||
def test_float_strict_type(self):
|
||||
# assigning 123 to float shouldn't throw an exception
|
||||
a = petstore_api.FormatTest(number=39.8, float=123, byte=bytes("string", 'utf-8'), date="2013-09-17", password="testing09876")
|
||||
self.assertEqual(a.float, 123.0)
|
||||
|
||||
json_str = "{\"number\": 34.5, \"float\": \"456\", \"date\": \"2013-12-08\", \"password\": \"empty1234567\", \"pattern_with_digits\": \"1234567890\", \"pattern_with_digits_and_delimiter\": \"image_123\", \"string_with_double_quote_pattern\": \"this is \\\"something\\\"\", \"string\": \"string\"}"
|
||||
# no exception thrown when assigning 456 (integer) to float type since strict is set to false
|
||||
f = petstore_api.FormatTest.from_json(json_str)
|
||||
self.assertEqual(f.float, 456.0)
|
||||
|
||||
def test_valdiator(self):
|
||||
# test regular expression
|
||||
a = petstore_api.FormatTest(number=123.45, byte=bytes("string", 'utf-8'), date="2013-09-17", password="testing09876")
|
||||
try:
|
||||
a.pattern_with_digits_and_delimiter = "123"
|
||||
self.assertTrue(False) # this line shouldn't execute
|
||||
except ValueError as e:
|
||||
self.assertTrue(r"must validate the regular expression /^image_\d{1,3}$/i" in str(e))
|
||||
|
||||
a.pattern_with_digits_and_delimiter = "IMAGE_123"
|
||||
self.assertEqual(a.pattern_with_digits_and_delimiter, "IMAGE_123")
|
||||
a.pattern_with_digits_and_delimiter = "image_123"
|
||||
self.assertEqual(a.pattern_with_digits_and_delimiter, "image_123")
|
||||
|
||||
def test_inline_enum_validator(self):
|
||||
self.pet = petstore_api.Pet(name="test name", photo_urls=["string"])
|
||||
self.pet.id = 1
|
||||
try:
|
||||
self.pet.status = "error"
|
||||
self.assertTrue(False) # this line shouldn't execute
|
||||
except ValueError as e:
|
||||
self.assertTrue("must be one of enum values ('available', 'pending', 'sold')" in str(e))
|
||||
@@ -0,0 +1,203 @@
|
||||
# coding: utf-8
|
||||
|
||||
# flake8: noqa
|
||||
|
||||
"""
|
||||
Run the tests.
|
||||
$ docker pull swaggerapi/petstore
|
||||
$ docker run -d -e SWAGGER_HOST=http://petstore.swagger.io -e SWAGGER_BASE_PATH=/v2 -p 80:8080 swaggerapi/petstore
|
||||
$ pytest -vv
|
||||
"""
|
||||
|
||||
import os
|
||||
import unittest
|
||||
import asyncio
|
||||
import pytest
|
||||
|
||||
import petstore_api
|
||||
from petstore_api import Configuration
|
||||
from petstore_api.rest import ApiException
|
||||
|
||||
from .util import id_gen, async_test
|
||||
|
||||
import json
|
||||
|
||||
import urllib3
|
||||
|
||||
HOST = 'http://localhost:80/v2'
|
||||
|
||||
|
||||
class TestPetApiTests(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
config = Configuration()
|
||||
config.host = HOST
|
||||
|
||||
self.api_client = petstore_api.ApiClient(config)
|
||||
self.pet_api = petstore_api.PetApi(self.api_client)
|
||||
self.setUpModels()
|
||||
self.setUpFiles()
|
||||
|
||||
def setUpModels(self):
|
||||
self.category = petstore_api.Category(id=id_gen(), name="dog")
|
||||
#self.category.id = id_gen()
|
||||
#self.category.name = "dog"
|
||||
self.tag = petstore_api.Tag()
|
||||
self.tag.id = id_gen()
|
||||
self.tag.name = "openapi-generator-python-pet-tag"
|
||||
self.pet = petstore_api.Pet(name="hello kity", photo_urls=["http://foo.bar.com/1", "http://foo.bar.com/2"])
|
||||
self.pet.id = id_gen()
|
||||
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_separate_default_client_instances(self):
|
||||
pet_api = petstore_api.PetApi()
|
||||
pet_api2 = petstore_api.PetApi()
|
||||
self.assertEqual(id(pet_api.api_client), id(pet_api2.api_client))
|
||||
|
||||
def test_separate_default_config_instances(self):
|
||||
pet_api = petstore_api.PetApi()
|
||||
pet_api2 = petstore_api.PetApi()
|
||||
self.assertEqual(id(pet_api.api_client.configuration), id(pet_api2.api_client.configuration))
|
||||
|
||||
@async_test
|
||||
async def test_async_with_result(self):
|
||||
await self.pet_api.add_pet(self.pet)
|
||||
|
||||
calls = [self.pet_api.get_pet_by_id(self.pet.id),
|
||||
self.pet_api.get_pet_by_id(self.pet.id)]
|
||||
|
||||
responses, _ = await asyncio.wait(calls)
|
||||
for response in responses:
|
||||
self.assertEqual(response.result().id, self.pet.id)
|
||||
self.assertEqual(len(responses), 2)
|
||||
|
||||
@async_test
|
||||
async def test_exception(self):
|
||||
await self.pet_api.add_pet(self.pet)
|
||||
|
||||
try:
|
||||
await self.pet_api.get_pet_by_id(9999999999999)
|
||||
except ApiException as e:
|
||||
exception = e
|
||||
|
||||
self.assertIsInstance(exception, ApiException)
|
||||
self.assertEqual(exception.status, 404)
|
||||
|
||||
@async_test
|
||||
async def test_add_pet_and_get_pet_by_id(self):
|
||||
await self.pet_api.add_pet(self.pet)
|
||||
|
||||
fetched = await 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)
|
||||
|
||||
@async_test
|
||||
async def test_add_pet_and_get_pet_by_id_with_http_info(self):
|
||||
await self.pet_api.add_pet(self.pet)
|
||||
|
||||
fetched = await self.pet_api.get_pet_by_id_with_http_info(pet_id=self.pet.id)
|
||||
self.assertIsNotNone(fetched)
|
||||
self.assertEqual(self.pet.id, fetched.data.id)
|
||||
self.assertIsNotNone(fetched.data.category)
|
||||
self.assertEqual(self.pet.category.name, fetched.data.category.name)
|
||||
|
||||
@async_test
|
||||
async def test_update_pet(self):
|
||||
self.pet.name = "hello kity with updated"
|
||||
await self.pet_api.update_pet(self.pet)
|
||||
|
||||
fetched = await 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)
|
||||
|
||||
@async_test
|
||||
async def test_find_pets_by_status(self):
|
||||
await self.pet_api.add_pet(self.pet)
|
||||
pets = await self.pet_api.find_pets_by_status(status=[self.pet.status])
|
||||
self.assertIn(
|
||||
self.pet.id,
|
||||
list(map(lambda x: getattr(x, 'id'), pets))
|
||||
)
|
||||
|
||||
@async_test
|
||||
async def test_find_pets_by_tags(self):
|
||||
await self.pet_api.add_pet(self.pet)
|
||||
pets = await self.pet_api.find_pets_by_tags(tags=[self.tag.name])
|
||||
self.assertIn(
|
||||
self.pet.id,
|
||||
list(map(lambda x: getattr(x, 'id'), pets))
|
||||
)
|
||||
|
||||
@async_test
|
||||
async def test_update_pet_with_form(self):
|
||||
await self.pet_api.add_pet(self.pet)
|
||||
|
||||
name = "hello kity with form updated"
|
||||
status = "pending"
|
||||
await self.pet_api.update_pet_with_form(pet_id=self.pet.id, name=name, status=status)
|
||||
|
||||
fetched = await 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)
|
||||
|
||||
@async_test
|
||||
async def test_upload_file(self):
|
||||
# upload file with form parameter
|
||||
try:
|
||||
additional_metadata = "special"
|
||||
await 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)))
|
||||
|
||||
# upload only file
|
||||
try:
|
||||
await self.pet_api.upload_file(pet_id=self.pet.id, file=self.foo)
|
||||
except ApiException as e:
|
||||
self.fail("upload_file() raised {0} unexpectedly".format(type(e)))
|
||||
|
||||
@async_test
|
||||
async def test_delete_pet(self):
|
||||
await self.pet_api.add_pet(self.pet)
|
||||
await self.pet_api.delete_pet(pet_id=self.pet.id, api_key="special-key")
|
||||
|
||||
try:
|
||||
await self.pet_api.get_pet_by_id(pet_id=self.pet.id)
|
||||
raise Exception("expected an error")
|
||||
except ApiException as e:
|
||||
self.assertEqual(404, e.status)
|
||||
|
||||
@async_test
|
||||
async def test_proxy(self):
|
||||
config = Configuration()
|
||||
# set not-existent proxy and catch an error to verify that
|
||||
# the client library (aiohttp) tried to use it.
|
||||
config.proxy = 'http://localhost:8080/proxy'
|
||||
async with petstore_api.ApiClient(config) as client:
|
||||
pet_api = petstore_api.PetApi(client)
|
||||
|
||||
with self.assertRaisesRegex(petstore_api.rest.aiohttp.client_exceptions.ClientProxyConnectionError,
|
||||
'Cannot connect to host localhost:8080'):
|
||||
await pet_api.get_pet_by_id(self.pet.id)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import logging
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
unittest.main()
|
||||
@@ -0,0 +1,114 @@
|
||||
# coding: utf-8
|
||||
|
||||
# flake8: noqa
|
||||
|
||||
import os
|
||||
import time
|
||||
import unittest
|
||||
|
||||
import petstore_api
|
||||
import json
|
||||
from pydantic import ValidationError
|
||||
|
||||
|
||||
class PetModelTests(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.pet = petstore_api.Pet(name="test name", photo_urls=["string"])
|
||||
self.pet.id = 1
|
||||
self.pet.status = "available"
|
||||
cate = petstore_api.Category(name="dog")
|
||||
cate.id = 1
|
||||
# cate.name = "dog"
|
||||
self.pet.category = cate
|
||||
tag = petstore_api.Tag()
|
||||
tag.id = 1
|
||||
self.pet.tags = [tag]
|
||||
|
||||
def test_to_str(self):
|
||||
data = ("{'category': {'id': 1, 'name': 'dog'},\n"
|
||||
" 'id': 1,\n"
|
||||
" 'name': 'test name',\n"
|
||||
" 'photoUrls': ['string'],\n"
|
||||
" 'status': 'available',\n"
|
||||
" 'tags': [{'id': 1, 'name': None}]}")
|
||||
self.assertEqual(data, self.pet.to_str())
|
||||
|
||||
def test_equal(self):
|
||||
self.pet1 = petstore_api.Pet(name="test name", photo_urls=["string"])
|
||||
self.pet1.id = 1
|
||||
self.pet1.status = "available"
|
||||
cate1 = petstore_api.Category(name="dog")
|
||||
cate1.id = 1
|
||||
# cate1.name = "dog"
|
||||
self.pet.category = cate1
|
||||
tag1 = petstore_api.Tag()
|
||||
tag1.id = 1
|
||||
self.pet1.tags = [tag1]
|
||||
|
||||
self.pet2 = petstore_api.Pet(name="test name", photo_urls=["string"])
|
||||
self.pet2.id = 1
|
||||
self.pet2.status = "available"
|
||||
cate2 = petstore_api.Category(name="dog")
|
||||
cate2.id = 1
|
||||
# cate2.name = "dog"
|
||||
self.pet.category = cate2
|
||||
tag2 = petstore_api.Tag()
|
||||
tag2.id = 1
|
||||
self.pet2.tags = [tag2]
|
||||
|
||||
self.assertTrue(self.pet1 == self.pet2)
|
||||
|
||||
# reset pet1 tags to empty array so that object comparison returns false
|
||||
self.pet1.tags = []
|
||||
self.assertFalse(self.pet1 == self.pet2)
|
||||
|
||||
# test from_json, to_json, to_dict, from_dict
|
||||
def test_from_to_methods(self):
|
||||
json_str = ("{\"category\": {\"id\": 1, \"name\": \"dog\"},\n"
|
||||
" \"id\": 1,\n"
|
||||
" \"name\": \"test name\",\n"
|
||||
" \"photoUrls\": [\"string\"],\n"
|
||||
" \"status\": \"available\",\n"
|
||||
" \"tags\": [{\"id\": 1, \"name\": \"None\"}]}")
|
||||
pet = petstore_api.Pet.from_json(json_str)
|
||||
self.assertEqual(pet.id, 1)
|
||||
self.assertEqual(pet.status, "available")
|
||||
self.assertEqual(pet.photo_urls, ["string"])
|
||||
self.assertEqual(pet.tags[0].id, 1)
|
||||
self.assertEqual(pet.tags[0].name, "None")
|
||||
self.assertEqual(pet.category.id, 1)
|
||||
# test to_json
|
||||
self.assertEqual(pet.to_json(),
|
||||
'{"id": 1, "category": {"id": 1, "name": "dog"}, "name": "test name", "photoUrls": ['
|
||||
'"string"], "tags": [{"id": 1, "name": "None"}], "status": "available"}')
|
||||
|
||||
# test to_dict
|
||||
self.assertEqual(pet.to_dict(),
|
||||
{"id": 1, "category": {"id": 1, "name": "dog"}, "name": "test name", "photoUrls": ["string"],
|
||||
"tags": [{"id": 1, "name": "None"}], "status": "available"})
|
||||
|
||||
# test from_dict
|
||||
pet2 = petstore_api.Pet.from_dict(pet.to_dict())
|
||||
self.assertEqual(pet2.id, 1)
|
||||
self.assertEqual(pet2.status, "available")
|
||||
self.assertEqual(pet2.photo_urls, ["string"])
|
||||
self.assertEqual(pet2.tags[0].id, 1)
|
||||
self.assertEqual(pet2.tags[0].name, "None")
|
||||
self.assertEqual(pet2.category.id, 1)
|
||||
|
||||
def test_unpack_operator(self):
|
||||
d = {"name": "required name", "id": 123, "photoUrls": ["https://a.com", "https://b.com"]}
|
||||
pet = petstore_api.Pet(**d)
|
||||
self.assertEqual(pet.to_json(), '{"id": 123, "name": "required name", "photoUrls": ["https://a.com", "https://b.com"]}')
|
||||
self.assertEqual(pet.to_dict(), {"id": 123, "name": "required name", "photoUrls": ["https://a.com", "https://b.com"]})
|
||||
|
||||
def test_optional_fields(self):
|
||||
pet = petstore_api.Pet(name="required name",
|
||||
photoUrls=["https://a.com",
|
||||
"https://b.com"])
|
||||
self.assertEqual(pet.to_json(), '{"name": "required name", "photoUrls": ["https://a.com", "https://b.com"]}')
|
||||
self.assertEqual(pet.to_dict(), {"name": "required name", "photoUrls": ["https://a.com", "https://b.com"]})
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
# flake8: noqa
|
||||
|
||||
import asyncio
|
||||
import random
|
||||
|
||||
|
||||
def id_gen(bits=32):
|
||||
""" Returns a n-bit randomly generated int """
|
||||
return int(random.getrandbits(bits))
|
||||
|
||||
|
||||
def async_test(f):
|
||||
def wrapper(*args, **kwargs):
|
||||
coro = asyncio.coroutine(f)
|
||||
future = coro(*args, **kwargs)
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.run_until_complete(future)
|
||||
return wrapper
|
||||
Reference in New Issue
Block a user