From b9287cf417805677d69b7f4769492f7119be0a2a Mon Sep 17 00:00:00 2001 From: geekerzp Date: Wed, 22 Jul 2015 11:00:51 +0800 Subject: [PATCH] Add to_str method in python client and add test cases. --- .../src/main/resources/python/model.mustache | 41 +++++++++++++++---- .../python/swagger_client/models/category.py | 41 +++++++++++++++---- .../python/swagger_client/models/order.py | 41 +++++++++++++++---- .../python/swagger_client/models/pet.py | 41 +++++++++++++++---- .../python/swagger_client/models/tag.py | 41 +++++++++++++++---- .../python/swagger_client/models/user.py | 41 +++++++++++++++---- .../petstore/python/tests/test_pet_model.py | 40 ++++++++++++++++++ 7 files changed, 238 insertions(+), 48 deletions(-) create mode 100644 samples/client/petstore/python/tests/test_pet_model.py diff --git a/modules/swagger-codegen/src/main/resources/python/model.mustache b/modules/swagger-codegen/src/main/resources/python/model.mustache index 2394cd955f3..7314e9ee9cd 100644 --- a/modules/swagger-codegen/src/main/resources/python/model.mustache +++ b/modules/swagger-codegen/src/main/resources/python/model.mustache @@ -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}} diff --git a/samples/client/petstore/python/swagger_client/models/category.py b/samples/client/petstore/python/swagger_client/models/category.py index cc9181a378d..ae03bb7ec78 100644 --- a/samples/client/petstore/python/swagger_client/models/category.py +++ b/samples/client/petstore/python/swagger_client/models/category.py @@ -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() diff --git a/samples/client/petstore/python/swagger_client/models/order.py b/samples/client/petstore/python/swagger_client/models/order.py index 386b0711e56..7498561372e 100644 --- a/samples/client/petstore/python/swagger_client/models/order.py +++ b/samples/client/petstore/python/swagger_client/models/order.py @@ -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() diff --git a/samples/client/petstore/python/swagger_client/models/pet.py b/samples/client/petstore/python/swagger_client/models/pet.py index ab21f38469d..62f7bea7fd2 100644 --- a/samples/client/petstore/python/swagger_client/models/pet.py +++ b/samples/client/petstore/python/swagger_client/models/pet.py @@ -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() diff --git a/samples/client/petstore/python/swagger_client/models/tag.py b/samples/client/petstore/python/swagger_client/models/tag.py index 98501f4e954..ea07acacfdb 100644 --- a/samples/client/petstore/python/swagger_client/models/tag.py +++ b/samples/client/petstore/python/swagger_client/models/tag.py @@ -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() diff --git a/samples/client/petstore/python/swagger_client/models/user.py b/samples/client/petstore/python/swagger_client/models/user.py index dd003f95426..d81a73318e7 100644 --- a/samples/client/petstore/python/swagger_client/models/user.py +++ b/samples/client/petstore/python/swagger_client/models/user.py @@ -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() diff --git a/samples/client/petstore/python/tests/test_pet_model.py b/samples/client/petstore/python/tests/test_pet_model.py new file mode 100644 index 00000000000..0b0df18a8d0 --- /dev/null +++ b/samples/client/petstore/python/tests/test_pet_model.py @@ -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())