Skip to content

Commit

Permalink
[python-asyncio] tests and fixes (#7235)
Browse files Browse the repository at this point in the history
* fix: creating ssl context and passing args/files/forms

* feat: python-async add tests

* chore: rebuild petstore sample for python, tornado and asyncio

* feat: add python asyncio to travis

* feat: print coverage (python-asyncio)
  • Loading branch information
tomplus authored and wing328 committed Feb 1, 2018
1 parent 157e6b7 commit f6e0e29
Show file tree
Hide file tree
Showing 20 changed files with 392 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ class RESTClientObject(object):
# if not set certificate file, use Mozilla's root certificates.
ca_certs = certifi.where()

ssl_context = ssl.SSLContext()
ssl_context.load_verify_locations(cafile=ca_certs)
ssl_context = ssl.create_default_context(cafile=ca_certs)
if configuration.cert_file:
ssl_context.load_cert_chain(
configuration.cert_file, keyfile=configuration.key_file
Expand Down Expand Up @@ -113,21 +112,34 @@ class RESTClientObject(object):
"timeout": timeout,
"headers": headers
}

if query_params:
args["url"] += '?' + urlencode(query_params)

# For `POST`, `PUT`, `PATCH`, `OPTIONS`, `DELETE`
if method in ['POST', 'PUT', 'PATCH', 'OPTIONS', 'DELETE']:
if query_params:
url += '?' + urlencode(query_params)
if re.search('json', headers['Content-Type'], re.IGNORECASE):
if body is not None:
body = json.dumps(body)
args["data"] = body
elif headers['Content-Type'] == 'application/x-www-form-urlencoded': # noqa: E501
args["data"] = aiohttp.FormData(post_params)
elif headers['Content-Type'] == 'multipart/form-data':
# must del headers['Content-Type'], or the correct
# Content-Type which generated by aiohttp
del headers['Content-Type']
data = aiohttp.FormData()
for k, v in post_params.items():
data.add_field(k, v)
for param in post_params:
k, v = param
if isinstance(v, tuple) and len(v) == 3:
data.add_field(k,
value=v[1],
filename=v[0],
content_type=v[2])
else:
data.add_field(k, v)
args["data"] = data
elif headers['Content-Type'] == 'multipart/form-data':
args["data"] = post_params

# Pass a `bytes` parameter directly in the body to support
# other content types than Json when `body` argument is provided
# in serialized form
Expand All @@ -139,8 +151,6 @@ class RESTClientObject(object):
arguments. Please check that your arguments match
declared content type."""
raise ApiException(status=0, reason=msg)
else:
args["data"] = query_params

async with self.pool_manager.request(**args) as r:
data = await r.text()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
{{^asyncio}}
coverage>=4.0.3
nose>=1.3.7
{{/asyncio}}
{{#asyncio}}
pytest>=3.3.1
pytest-cov>=2.5.1
{{/asyncio}}
pluggy>=0.3.1
py>=1.4.31
randomize>=0.13
14 changes: 12 additions & 2 deletions modules/swagger-codegen/src/main/resources/python/tox.mustache
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
[tox]
{{^asyncio}}
envlist = py27, py3
{{/asyncio}}
{{#asyncio}}
envlist = py3
{{/asyncio}}

[testenv]
deps=-r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt

commands=
{{^asyncio}}
nosetests \
[]
[]
{{/asyncio}}
{{#asyncio}}
pytest -v --cov petstore_api
{{/asyncio}}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,7 @@
<module>samples/client/petstore/javascript</module>
<module>samples/client/petstore/python</module>
<module>samples/client/petstore/python-tornado</module>
<module>samples/client/petstore/python-asyncio</module>
<module>samples/client/petstore/typescript-fetch/builds/default</module>
<module>samples/client/petstore/typescript-fetch/builds/es6-target</module>
<module>samples/client/petstore/typescript-fetch/builds/with-npm-version</module>
Expand Down
18 changes: 18 additions & 0 deletions samples/client/petstore/python-asyncio/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

REQUIREMENTS_FILE=dev-requirements.txt
REQUIREMENTS_OUT=dev-requirements.txt.log
SETUP_OUT=*.egg-info
VENV=.venv

clean:
rm -rf $(REQUIREMENTS_OUT)
rm -rf $(SETUP_OUT)
rm -rf $(VENV)
rm -rf .tox
rm -rf .coverage
find . -name "*.py[oc]" -delete
find . -name "__pycache__" -delete

test-all: clean
bash ./test_python3.sh
4 changes: 4 additions & 0 deletions samples/client/petstore/python-asyncio/dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
tox
coverage
randomize
flake8
4 changes: 2 additions & 2 deletions samples/client/petstore/python-asyncio/docs/FakeApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,10 @@ configuration.password = 'YOUR_PASSWORD'

# create an instance of the API class
api_instance = petstore_api.FakeApi(petstore_api.ApiClient(configuration))
number = 8.14 # float | None
number = 3.4 # float | None
double = 1.2 # float | None
pattern_without_delimiter = 'pattern_without_delimiter_example' # str | None
byte = 'B' # str | None
byte = 'byte_example' # str | None
integer = 56 # int | None (optional)
int32 = 56 # int | None (optional)
int64 = 789 # int | None (optional)
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/python-asyncio/git_push.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ git_remote=`git remote`
if [ "$git_remote" = "" ]; then # git remote not defined

if [ "$GIT_TOKEN" = "" ]; then
echo "[INFO] \$GIT_TOKEN (environment variable) is not set. Using the git crediential in your environment."
echo "[INFO] \$GIT_TOKEN (environment variable) is not set. Using the git credential in your environment."
git remote add origin https://github.com/${git_user_id}/${git_repo_id}.git
else
git remote add origin https://${git_user_id}:${GIT_TOKEN}@github.com/${git_user_id}/${git_repo_id}.git
Expand Down
30 changes: 20 additions & 10 deletions samples/client/petstore/python-asyncio/petstore_api/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ def __init__(self, configuration, pools_size=4, maxsize=4):
# if not set certificate file, use Mozilla's root certificates.
ca_certs = certifi.where()

ssl_context = ssl.SSLContext()
ssl_context.load_verify_locations(cafile=ca_certs)
ssl_context = ssl.create_default_context(cafile=ca_certs)
if configuration.cert_file:
ssl_context.load_cert_chain(
configuration.cert_file, keyfile=configuration.key_file
Expand Down Expand Up @@ -122,21 +121,34 @@ async def request(self, method, url, query_params=None, headers=None,
"timeout": timeout,
"headers": headers
}

if query_params:
args["url"] += '?' + urlencode(query_params)

# For `POST`, `PUT`, `PATCH`, `OPTIONS`, `DELETE`
if method in ['POST', 'PUT', 'PATCH', 'OPTIONS', 'DELETE']:
if query_params:
url += '?' + urlencode(query_params)
if re.search('json', headers['Content-Type'], re.IGNORECASE):
if body is not None:
body = json.dumps(body)
args["data"] = body
elif headers['Content-Type'] == 'application/x-www-form-urlencoded': # noqa: E501
args["data"] = aiohttp.FormData(post_params)
elif headers['Content-Type'] == 'multipart/form-data':
# must del headers['Content-Type'], or the correct
# Content-Type which generated by aiohttp
del headers['Content-Type']
data = aiohttp.FormData()
for k, v in post_params.items():
data.add_field(k, v)
for param in post_params:
k, v = param
if isinstance(v, tuple) and len(v) == 3:
data.add_field(k,
value=v[1],
filename=v[0],
content_type=v[2])
else:
data.add_field(k, v)
args["data"] = data
elif headers['Content-Type'] == 'multipart/form-data':
args["data"] = post_params

# Pass a `bytes` parameter directly in the body to support
# other content types than Json when `body` argument is provided
# in serialized form
Expand All @@ -148,8 +160,6 @@ async def request(self, method, url, query_params=None, headers=None,
arguments. Please check that your arguments match
declared content type."""
raise ApiException(status=0, reason=msg)
else:
args["data"] = query_params

async with self.pool_manager.request(**args) as r:
data = await r.text()
Expand Down
46 changes: 46 additions & 0 deletions samples/client/petstore/python-asyncio/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>io.swagger</groupId>
<artifactId>PythonAsyncioClientTests</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>Python Asyncio Petstore Client</name>
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<id>pytest-test</id>
<phase>integration-test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>make</executable>
<arguments>
<argument>test-all</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
4 changes: 2 additions & 2 deletions samples/client/petstore/python-asyncio/test-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
coverage>=4.0.3
nose>=1.3.7
pytest>=3.3.1
pytest-cov>=2.5.1
pluggy>=0.3.1
py>=1.4.31
randomize>=0.13
32 changes: 32 additions & 0 deletions samples/client/petstore/python-asyncio/test_python3.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash

REQUIREMENTS_FILE=dev-requirements.txt
REQUIREMENTS_OUT=dev-requirements.txt.log
SETUP_OUT=*.egg-info
VENV=.venv
DEACTIVE=false

export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8

### set virtualenv
if [ -z "$VIRTUAL_ENV" ]; then
virtualenv $VENV --no-site-packages --always-copy --python python3
source $VENV/bin/activate
DEACTIVE=true
fi

### install dependencies
pip install -r $REQUIREMENTS_FILE | tee -a $REQUIREMENTS_OUT
python setup.py develop

### run tests
tox || exit 1

### static analysis of code
flake8 --show-source petstore_api/

### deactivate virtualenv
#if [ $DEACTIVE == true ]; then
# deactivate
#fi
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
Loading

0 comments on commit f6e0e29

Please sign in to comment.