diff --git a/modules/swagger-codegen/src/main/resources/python/__init__model.mustache b/modules/swagger-codegen/src/main/resources/python/__init__model.mustache index 728aacbb9ab6..93d18228d252 100644 --- a/modules/swagger-codegen/src/main/resources/python/__init__model.mustache +++ b/modules/swagger-codegen/src/main/resources/python/__init__model.mustache @@ -2,6 +2,10 @@ """Add all of the modules in the current directory to __all__""" import os +{{#models}}{{#model}} +from .{{classVarName}} import {{classname}} +{{/model}}{{/models}} + __all__ = [] for module in os.listdir(os.path.dirname(__file__)): diff --git a/modules/swagger-codegen/src/main/resources/python/swagger.mustache b/modules/swagger-codegen/src/main/resources/python/swagger.mustache index 76857b09ab78..10ac54d527d1 100644 --- a/modules/swagger-codegen/src/main/resources/python/swagger.mustache +++ b/modules/swagger-codegen/src/main/resources/python/swagger.mustache @@ -17,8 +17,7 @@ import datetime import mimetypes import random import string - -from models import * +import models class ApiClient(object): @@ -211,7 +210,7 @@ class ApiClient(object): if (objClass in ['int', 'float', 'long', 'dict', 'list', 'str', 'bool', 'datetime']): objClass = eval(objClass) else: # not a native type, must be model class - objClass = eval(objClass + '.' + objClass) + objClass = eval('models.' + objClass) if objClass in [int, long, float, dict, list, str, bool]: return objClass(obj) diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/models/__init__.py b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/models/__init__.py index 728aacbb9ab6..ce94c72a70c0 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/models/__init__.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/models/__init__.py @@ -2,6 +2,18 @@ """Add all of the modules in the current directory to __all__""" import os + +from .user import User + +from .category import Category + +from .pet import Pet + +from .tag import Tag + +from .order import Order + + __all__ = [] for module in os.listdir(os.path.dirname(__file__)): diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/swagger.py b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/swagger.py index 76857b09ab78..10ac54d527d1 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/swagger.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/swagger.py @@ -17,8 +17,7 @@ import datetime import mimetypes import random import string - -from models import * +import models class ApiClient(object): @@ -211,7 +210,7 @@ class ApiClient(object): if (objClass in ['int', 'float', 'long', 'dict', 'list', 'str', 'bool', 'datetime']): objClass = eval(objClass) else: # not a native type, must be model class - objClass = eval(objClass + '.' + objClass) + objClass = eval('models.' + objClass) if objClass in [int, long, float, dict, list, str, bool]: return objClass(obj) diff --git a/samples/client/petstore/python/SwaggerPetstore-python/pom.xml b/samples/client/petstore/python/SwaggerPetstore-python/pom.xml new file mode 100644 index 000000000000..c06d3a1cdf63 --- /dev/null +++ b/samples/client/petstore/python/SwaggerPetstore-python/pom.xml @@ -0,0 +1,72 @@ + + 4.0.0 + com.wordnik + PythonPetstoreClientTests + pom + 1.0-SNAPSHOT + Python Swagger Petstore Client + + + + maven-dependency-plugin + + + package + + copy-dependencies + + + ${project.build.directory} + + + + + + org.codehaus.mojo + exec-maven-plugin + 1.2.1 + + + nose-install + pre-integration-test + + exec + + + pip + + install + nose + --user + + + + + nose-test + integration-test + + exec + + + nosetests + + -v + + + + + + + + + + + + + + + + + + + diff --git a/samples/client/petstore/python/SwaggerPetstore-python/testfiles/foo.png b/samples/client/petstore/python/SwaggerPetstore-python/testfiles/foo.png new file mode 100644 index 000000000000..a9b12cf5927a Binary files /dev/null and b/samples/client/petstore/python/SwaggerPetstore-python/testfiles/foo.png differ diff --git a/samples/client/petstore/python/SwaggerPetstore-python/tests/__init__.py b/samples/client/petstore/python/SwaggerPetstore-python/tests/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/samples/client/petstore/python/SwaggerPetstore-python/tests/test_pet_api.py b/samples/client/petstore/python/SwaggerPetstore-python/tests/test_pet_api.py new file mode 100644 index 000000000000..b6088ae34b95 --- /dev/null +++ b/samples/client/petstore/python/SwaggerPetstore-python/tests/test_pet_api.py @@ -0,0 +1,113 @@ +# coding: utf-8 + +""" +Run the tests. +$ pip install nose (optional) +$ cd SwaggerPetstore-python +$ nosetests -v +""" + +import os +import time +import unittest +import urllib2 + +import SwaggerPetstore + +HOST = 'http://petstore.swagger.io/v2' + + +class PetApiTests(unittest.TestCase): + + def setUp(self): + self.api_client = SwaggerPetstore.ApiClient(HOST) + self.pet_api = SwaggerPetstore.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 = SwaggerPetstore.Category() + self.category.id = 1010 + self.category.name = "dog" + self.tag = SwaggerPetstore.Tag() + self.tag.id = 1010 + self.tag.name = "blank" + self.pet = SwaggerPetstore.Pet() + self.pet.id = 1010 + self.pet.name = "hello kity" + self.pet.photo_urls = ["sample urls"] + 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_1_add_pet(self): + try: + self.pet_api.add_pet(body=self.pet) + except (urllib2.HTTPError, urllib2.URLError) as e: + self.fail("add_pet() raised {0} unexpectedly".format(type(e))) + + def test_2_get_pet_by_id(self): + self.assertEqual( + dir(self.pet_api.get_pet_by_id(pet_id=self.pet.id)), + dir(self.pet) + ) + + def test_3_update_pet(self): + try: + self.pet.name = "hello kity with updated" + self.pet_api.update_pet(body=self.pet) + except (urllib2.HTTPError, urllib2.URLError) as e: + self.fail("update_pet() raised {0} unexpectedly".format(type(e))) + + def test_4_find_pets_by_status(self): + self.assertIn( + dir(self.pet), + map(dir, self.pet_api.find_pets_by_status(status=["sold"])) + ) + + def test_5_find_pets_by_tags(self): + self.assertIn( + dir(self.pet), + map(dir, self.pet_api.find_pets_by_tags(tags=["blank"])) + ) + + def test_6_update_pet_with_form(self): + try: + name = "hello kity with form updated" + status = "pending" + self.pet_api.update_pet_with_form( + pet_id=self.pet.id, name=name, status=status + ) + except (urllib2.HTTPError, urllib2.URLError) as e: + self.fail("update_pet_with_form() raised {0} unexpectedly".format(type(e))) + + def test_7_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 (urllib2.HTTPError, urllib2.URLError) as e: + self.fail("upload_file() raised {0} unexpectedly".format(type(e))) + + def test_8_delete_pet(self): + try: + api_key = "special-key" + self.pet_api.delete_pet(pet_id=self.pet.id, api_key=api_key) + except (urllib2.HTTPError, urllib2.URLError) as e: + self.fail("delete_pet() raised {0} unexpectedly".format(type(e))) + + +if __name__ == '__main__': + unittest.main()