Skip to content

Commit

Permalink
Added unit tests to demonstrate the problems of spec-first#975
Browse files Browse the repository at this point in the history
    - Taken mostly from existing PR: spec-first#987
  • Loading branch information
ddurham2 committed Apr 22, 2020
1 parent 818a259 commit b131e21
Show file tree
Hide file tree
Showing 4 changed files with 273 additions and 1 deletion.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def read_version(package):
swagger_ui_require = 'swagger-ui-bundle>=0.0.2'
flask_require = 'flask>=1.0.4'
aiohttp_require = [
'aiohttp>=2.3.10',
'aiohttp>=2.3.10,<4',
'aiohttp-jinja2>=0.14.0'
]

Expand Down
105 changes: 105 additions & 0 deletions tests/aiohttp/test_aiohttp_multipart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import asyncio
import os

import aiohttp
import pytest

from connexion import AioHttpApp

try:
import ujson as json
except ImportError:
import json


@pytest.fixture
def aiohttp_app(aiohttp_api_spec_dir):
app = AioHttpApp(__name__, port=5001,
specification_dir=aiohttp_api_spec_dir,
debug=True)
app.add_api(
'openapi_multipart.yaml',
validate_responses=True,
strict_validation=True,
pythonic_params=True,
pass_context_arg_name='request_ctx',
)
return app


@asyncio.coroutine
def test_single_file_upload(aiohttp_app, aiohttp_client):
app_client = yield from aiohttp_client(aiohttp_app.app)

resp = yield from app_client.post(
'/v1.0/upload_file',
data=aiohttp.FormData(fields=[('funky_funky', open(__file__, 'rb'))])(),
)

data = yield from resp.json()
assert resp.status == 200
assert data['fileName'] == f'{__name__}.py'


@asyncio.coroutine
def test_many_files_upload(aiohttp_app, aiohttp_client):
app_client = yield from aiohttp_client(aiohttp_app.app)

dir_name = os.path.dirname(__file__)
files_field = [('files', open(f'{dir_name}/{file_name}', 'rb')) for file_name in os.listdir(dir_name) if file_name.endswith('.py')]

form_data = aiohttp.FormData(fields=files_field)

resp = yield from app_client.post(
'/v1.0/upload_files',
data=form_data(),
)

data = yield from resp.json()

assert resp.status == 200
assert data['filesCount'] == len(files_field)


@asyncio.coroutine
def test_mixed_multipart_single_file(aiohttp_app, aiohttp_client):
app_client = yield from aiohttp_client(aiohttp_app.app)

form_data = aiohttp.FormData()
form_data.add_field('dirName', os.path.dirname(__file__))
form_data.add_field('funky_funky', open(__file__, 'rb'))

resp = yield from app_client.post(
'/v1.0/mixed_single_file',
data=form_data(),
)

data = yield from resp.json()

assert resp.status == 200
assert data['dirName'] == os.path.dirname(__file__)
assert data['fileName'] == f'{__name__}.py'


@asyncio.coroutine
def test_mixed_multipart_many_files(aiohttp_app, aiohttp_client):
app_client = yield from aiohttp_client(aiohttp_app.app)

dir_name = os.path.dirname(__file__)
files_field = [('files', open(f'{dir_name}/{file_name}', 'rb')) for file_name in os.listdir(dir_name) if file_name.endswith('.py')]

form_data = aiohttp.FormData(fields=files_field)
form_data.add_field('dirName', os.path.dirname(__file__))
form_data.add_field('testCount', str(len(files_field)))

resp = yield from app_client.post(
'/v1.0/mixed_many_files',
data=form_data(),
)

data = yield from resp.json()

assert resp.status == 200
assert data['dirName'] == os.path.dirname(__file__)
assert data['testCount'] == len(files_field)
assert data['filesCount'] == len(files_field)
35 changes: 35 additions & 0 deletions tests/fakeapi/aiohttp_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,38 @@ def get_date():
@asyncio.coroutine
def get_uuid():
return ConnexionResponse(body={'value': uuid.UUID(hex='e7ff66d0-3ec2-4c4e-bed0-6e4723c24c51')})


@asyncio.coroutine
def aiohttp_multipart_single_file(funky_funky):
return aiohttp.web.json_response(
data={'fileName': funky_funky.filename},
)


@asyncio.coroutine
def aiohttp_multipart_many_files(files):
return aiohttp.web.json_response(
data={'filesCount': len(files)},
)


@asyncio.coroutine
def aiohttp_multipart_mixed_single_file(dir_name, funky_funky):
return aiohttp.web.json_response(
data={
'dirName': dir_name,
'fileName': funky_funky.filename,
},
)


@asyncio.coroutine
def aiohttp_multipart_mixed_many_files(dir_name, test_count, files):
return aiohttp.web.json_response(
data={
'filesCount': len(files),
'dirName': dir_name,
'testCount': test_count,
},
)
132 changes: 132 additions & 0 deletions tests/fixtures/aiohttp/openapi_multipart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
---
openapi: 3.0.0
servers:
- url: /v1.0
info:
title: "{{title}}"
version: "1.0"
paths:
"/mixed_single_file":
post:
summary: Reads multipart data
description: Handles multipart data reading
operationId: fakeapi.aiohttp_handlers.aiohttp_multipart_mixed_single_file
responses:
"200":
description: OK response
content:
'application/json':
schema:
type: object
properties:
dirName:
type: string
fileName:
type: string
default:
description: unexpected error
requestBody:
required: true
content:
multipart/form-data:
schema:
type: object
properties:
dirName:
type: string
funky_funky:
type: string
format: binary
"/mixed_many_files":
post:
summary: Reads multipart data
description: Handles multipart data reading
operationId: fakeapi.aiohttp_handlers.aiohttp_multipart_mixed_many_files
responses:
"200":
description: OK response
content:
'application/json':
schema:
type: object
properties:
dirName:
type: string
testCount:
type: number
filesCount:
type: number
default:
description: unexpected error
requestBody:
required: true
content:
multipart/form-data:
schema:
type: object
properties:
dirName:
type: string
testCount:
type: number
files:
type: array
items:
type: string
format: binary
"/upload_file":
post:
summary: Uploads single file
description: Handles multipart file upload.
operationId: fakeapi.aiohttp_handlers.aiohttp_multipart_single_file
responses:
"200":
description: OK response
content:
'application/json':
schema:
type: object
properties:
fileName:
type: string
default:
description: unexpected error
requestBody:
required: true
content:
multipart/form-data:
schema:
type: object
properties:
funky_funky:
type: string
format: binary
"/upload_files":
post:
summary: Uploads many files
description: Handles multipart file upload.
operationId: fakeapi.aiohttp_handlers.aiohttp_multipart_many_files
responses:
"200":
description: OK response
content:
'application/json':
schema:
type: object
properties:
filesCount:
type: number
default:
description: unexpected error
requestBody:
required: true
content:
multipart/form-data:
schema:
type: object
properties:
file:
type: array
items:
type: string
format: binary

0 comments on commit b131e21

Please sign in to comment.