mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-10 19:32:43 +00:00
Add to_str method in python client and add test cases.
This commit is contained in:
@@ -18,6 +18,8 @@ Copyright 2015 SmartBear Software
|
||||
"""
|
||||
{{#models}}
|
||||
{{#model}}
|
||||
from pprint import pformat
|
||||
from six import iteritems
|
||||
|
||||
|
||||
class {{classname}}(object):
|
||||
@@ -64,14 +66,37 @@ class {{classname}}(object):
|
||||
{{/isEnum}}self._{{name}} = {{name}}
|
||||
|
||||
{{/vars}}
|
||||
def __repr__(self):
|
||||
properties = []
|
||||
for p in self.__dict__:
|
||||
if p != 'swaggerTypes' and p != 'attributeMap':
|
||||
properties.append('{prop}={val!r}'
|
||||
.format(prop=p, val=self.__dict__[p]))
|
||||
def to_dict(self):
|
||||
"""
|
||||
Return model properties dict
|
||||
"""
|
||||
result = {}
|
||||
|
||||
return '<{name} {props}>'.format(name=__name__,
|
||||
props=' '.join(properties))
|
||||
for name, prop in iteritems(self.__dict__):
|
||||
if name == "attribute_map" or name == "swagger_types":
|
||||
continue
|
||||
if isinstance(prop, list):
|
||||
result[name[1:]] = list(map(
|
||||
lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
|
||||
prop
|
||||
))
|
||||
elif hasattr(prop, "to_dict"):
|
||||
result[name[1:]] = prop.to_dict()
|
||||
else:
|
||||
result[name[1:]] = prop
|
||||
|
||||
return result
|
||||
|
||||
def to_str(self):
|
||||
"""
|
||||
Return model properties str
|
||||
"""
|
||||
return pformat(self.to_dict())
|
||||
|
||||
def __repr__(self):
|
||||
"""
|
||||
For `print` and `pprint`
|
||||
"""
|
||||
return self.to_str()
|
||||
{{/model}}
|
||||
{{/models}}
|
||||
|
||||
@@ -16,6 +16,8 @@ Copyright 2015 SmartBear Software
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
"""
|
||||
from pprint import pformat
|
||||
from six import iteritems
|
||||
|
||||
|
||||
class Category(object):
|
||||
@@ -61,12 +63,35 @@ class Category(object):
|
||||
def name(self, name):
|
||||
self._name = name
|
||||
|
||||
def __repr__(self):
|
||||
properties = []
|
||||
for p in self.__dict__:
|
||||
if p != 'swaggerTypes' and p != 'attributeMap':
|
||||
properties.append('{prop}={val!r}'
|
||||
.format(prop=p, val=self.__dict__[p]))
|
||||
def to_dict(self):
|
||||
"""
|
||||
Return model properties dict
|
||||
"""
|
||||
result = {}
|
||||
|
||||
return '<{name} {props}>'.format(name=__name__,
|
||||
props=' '.join(properties))
|
||||
for name, prop in iteritems(self.__dict__):
|
||||
if name == "attribute_map" or name == "swagger_types":
|
||||
continue
|
||||
if isinstance(prop, list):
|
||||
result[name[1:]] = list(map(
|
||||
lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
|
||||
prop
|
||||
))
|
||||
elif hasattr(prop, "to_dict"):
|
||||
result[name[1:]] = prop.to_dict()
|
||||
else:
|
||||
result[name[1:]] = prop
|
||||
|
||||
return result
|
||||
|
||||
def to_str(self):
|
||||
"""
|
||||
Return model properties str
|
||||
"""
|
||||
return pformat(self.to_dict())
|
||||
|
||||
def __repr__(self):
|
||||
"""
|
||||
For `print` and `pprint`
|
||||
"""
|
||||
return self.to_str()
|
||||
|
||||
@@ -16,6 +16,8 @@ Copyright 2015 SmartBear Software
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
"""
|
||||
from pprint import pformat
|
||||
from six import iteritems
|
||||
|
||||
|
||||
class Order(object):
|
||||
@@ -111,12 +113,35 @@ class Order(object):
|
||||
def complete(self, complete):
|
||||
self._complete = complete
|
||||
|
||||
def __repr__(self):
|
||||
properties = []
|
||||
for p in self.__dict__:
|
||||
if p != 'swaggerTypes' and p != 'attributeMap':
|
||||
properties.append('{prop}={val!r}'
|
||||
.format(prop=p, val=self.__dict__[p]))
|
||||
def to_dict(self):
|
||||
"""
|
||||
Return model properties dict
|
||||
"""
|
||||
result = {}
|
||||
|
||||
return '<{name} {props}>'.format(name=__name__,
|
||||
props=' '.join(properties))
|
||||
for name, prop in iteritems(self.__dict__):
|
||||
if name == "attribute_map" or name == "swagger_types":
|
||||
continue
|
||||
if isinstance(prop, list):
|
||||
result[name[1:]] = list(map(
|
||||
lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
|
||||
prop
|
||||
))
|
||||
elif hasattr(prop, "to_dict"):
|
||||
result[name[1:]] = prop.to_dict()
|
||||
else:
|
||||
result[name[1:]] = prop
|
||||
|
||||
return result
|
||||
|
||||
def to_str(self):
|
||||
"""
|
||||
Return model properties str
|
||||
"""
|
||||
return pformat(self.to_dict())
|
||||
|
||||
def __repr__(self):
|
||||
"""
|
||||
For `print` and `pprint`
|
||||
"""
|
||||
return self.to_str()
|
||||
|
||||
@@ -16,6 +16,8 @@ Copyright 2015 SmartBear Software
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
"""
|
||||
from pprint import pformat
|
||||
from six import iteritems
|
||||
|
||||
|
||||
class Pet(object):
|
||||
@@ -111,12 +113,35 @@ class Pet(object):
|
||||
)
|
||||
self._status = status
|
||||
|
||||
def __repr__(self):
|
||||
properties = []
|
||||
for p in self.__dict__:
|
||||
if p != 'swaggerTypes' and p != 'attributeMap':
|
||||
properties.append('{prop}={val!r}'
|
||||
.format(prop=p, val=self.__dict__[p]))
|
||||
def to_dict(self):
|
||||
"""
|
||||
Return model properties dict
|
||||
"""
|
||||
result = {}
|
||||
|
||||
return '<{name} {props}>'.format(name=__name__,
|
||||
props=' '.join(properties))
|
||||
for name, prop in iteritems(self.__dict__):
|
||||
if name == "attribute_map" or name == "swagger_types":
|
||||
continue
|
||||
if isinstance(prop, list):
|
||||
result[name[1:]] = list(map(
|
||||
lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
|
||||
prop
|
||||
))
|
||||
elif hasattr(prop, "to_dict"):
|
||||
result[name[1:]] = prop.to_dict()
|
||||
else:
|
||||
result[name[1:]] = prop
|
||||
|
||||
return result
|
||||
|
||||
def to_str(self):
|
||||
"""
|
||||
Return model properties str
|
||||
"""
|
||||
return pformat(self.to_dict())
|
||||
|
||||
def __repr__(self):
|
||||
"""
|
||||
For `print` and `pprint`
|
||||
"""
|
||||
return self.to_str()
|
||||
|
||||
@@ -16,6 +16,8 @@ Copyright 2015 SmartBear Software
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
"""
|
||||
from pprint import pformat
|
||||
from six import iteritems
|
||||
|
||||
|
||||
class Tag(object):
|
||||
@@ -61,12 +63,35 @@ class Tag(object):
|
||||
def name(self, name):
|
||||
self._name = name
|
||||
|
||||
def __repr__(self):
|
||||
properties = []
|
||||
for p in self.__dict__:
|
||||
if p != 'swaggerTypes' and p != 'attributeMap':
|
||||
properties.append('{prop}={val!r}'
|
||||
.format(prop=p, val=self.__dict__[p]))
|
||||
def to_dict(self):
|
||||
"""
|
||||
Return model properties dict
|
||||
"""
|
||||
result = {}
|
||||
|
||||
return '<{name} {props}>'.format(name=__name__,
|
||||
props=' '.join(properties))
|
||||
for name, prop in iteritems(self.__dict__):
|
||||
if name == "attribute_map" or name == "swagger_types":
|
||||
continue
|
||||
if isinstance(prop, list):
|
||||
result[name[1:]] = list(map(
|
||||
lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
|
||||
prop
|
||||
))
|
||||
elif hasattr(prop, "to_dict"):
|
||||
result[name[1:]] = prop.to_dict()
|
||||
else:
|
||||
result[name[1:]] = prop
|
||||
|
||||
return result
|
||||
|
||||
def to_str(self):
|
||||
"""
|
||||
Return model properties str
|
||||
"""
|
||||
return pformat(self.to_dict())
|
||||
|
||||
def __repr__(self):
|
||||
"""
|
||||
For `print` and `pprint`
|
||||
"""
|
||||
return self.to_str()
|
||||
|
||||
@@ -16,6 +16,8 @@ Copyright 2015 SmartBear Software
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
"""
|
||||
from pprint import pformat
|
||||
from six import iteritems
|
||||
|
||||
|
||||
class User(object):
|
||||
@@ -127,12 +129,35 @@ class User(object):
|
||||
def user_status(self, user_status):
|
||||
self._user_status = user_status
|
||||
|
||||
def __repr__(self):
|
||||
properties = []
|
||||
for p in self.__dict__:
|
||||
if p != 'swaggerTypes' and p != 'attributeMap':
|
||||
properties.append('{prop}={val!r}'
|
||||
.format(prop=p, val=self.__dict__[p]))
|
||||
def to_dict(self):
|
||||
"""
|
||||
Return model properties dict
|
||||
"""
|
||||
result = {}
|
||||
|
||||
return '<{name} {props}>'.format(name=__name__,
|
||||
props=' '.join(properties))
|
||||
for name, prop in iteritems(self.__dict__):
|
||||
if name == "attribute_map" or name == "swagger_types":
|
||||
continue
|
||||
if isinstance(prop, list):
|
||||
result[name[1:]] = list(map(
|
||||
lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
|
||||
prop
|
||||
))
|
||||
elif hasattr(prop, "to_dict"):
|
||||
result[name[1:]] = prop.to_dict()
|
||||
else:
|
||||
result[name[1:]] = prop
|
||||
|
||||
return result
|
||||
|
||||
def to_str(self):
|
||||
"""
|
||||
Return model properties str
|
||||
"""
|
||||
return pformat(self.to_dict())
|
||||
|
||||
def __repr__(self):
|
||||
"""
|
||||
For `print` and `pprint`
|
||||
"""
|
||||
return self.to_str()
|
||||
|
||||
40
samples/client/petstore/python/tests/test_pet_model.py
Normal file
40
samples/client/petstore/python/tests/test_pet_model.py
Normal file
@@ -0,0 +1,40 @@
|
||||
# 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
|
||||
|
||||
|
||||
class PetModelTests(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.pet = swagger_client.Pet()
|
||||
self.pet.name = "test name"
|
||||
self.pet.id = 1
|
||||
self.pet.photo_urls = ["string"]
|
||||
self.pet.status = "available"
|
||||
cate = swagger_client.Category()
|
||||
cate.id = 1
|
||||
cate.name = "dog"
|
||||
self.pet.category = cate
|
||||
tag = swagger_client.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"
|
||||
" 'photo_urls': ['string'],\n"
|
||||
" 'status': 'available',\n"
|
||||
" 'tags': [{'id': 1, 'name': None}]}")
|
||||
self.assertEqual(data, self.pet.to_str())
|
||||
Reference in New Issue
Block a user