Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fixing unit tests after after reverting _get_body_argument behavior
  • Loading branch information
ddurham2 committed Feb 16, 2022
commit 40a65f7b7efa2980af3e54ab956025ae3edd09b2
16 changes: 8 additions & 8 deletions tests/aiohttp/test_aiohttp_multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@ async def test_single_file_upload(aiohttp_app, aiohttp_client):

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

data = await resp.json()
assert resp.status == 200
assert data['fileName'] == f'{__name__}.py'
assert data['content'] == Path(__file__).read_bytes().decode('utf8')
assert data['myfile_content'] == Path(__file__).read_bytes().decode('utf8')


async def test_many_files_upload(aiohttp_app, aiohttp_client):
app_client = await aiohttp_client(aiohttp_app.app)

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

Expand All @@ -61,7 +61,7 @@ async def test_many_files_upload(aiohttp_app, aiohttp_client):

assert resp.status == 200
assert data['files_count'] == len(files_field)
assert data['contents'] == [
assert data['myfiles_content'] == [
Path(f'{dir_name}/{file_name}').read_bytes().decode('utf8') \
for file_name in sorted(os.listdir(dir_name)) if file_name.endswith('.py')
]
Expand All @@ -72,7 +72,7 @@ async def test_mixed_multipart_single_file(aiohttp_app, aiohttp_client):

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

resp = await app_client.post(
'/v1.0/mixed_single_file',
Expand All @@ -84,7 +84,7 @@ async def test_mixed_multipart_single_file(aiohttp_app, aiohttp_client):
assert resp.status == 200
assert data['dir_name'] == os.path.dirname(__file__)
assert data['fileName'] == f'{__name__}.py'
assert data['content'] == Path(__file__).read_bytes().decode('utf8')
assert data['myfile_content'] == Path(__file__).read_bytes().decode('utf8')



Expand All @@ -93,7 +93,7 @@ async def test_mixed_multipart_many_files(aiohttp_app, aiohttp_client):

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

Expand All @@ -112,7 +112,7 @@ async def test_mixed_multipart_many_files(aiohttp_app, aiohttp_client):
assert data['dir_name'] == os.path.dirname(__file__)
assert data['test_count'] == len(files_field)
assert data['files_count'] == len(files_field)
assert data['contents'] == [
assert data['myfiles_content'] == [
Path(f'{dir_name}/{file_name}').read_bytes().decode('utf8') \
for file_name in sorted(os.listdir(dir_name)) if file_name.endswith('.py')
]
4 changes: 2 additions & 2 deletions tests/api/test_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,8 @@ def test_args_kwargs(simple_app):
data=json.dumps(body),
headers={'Content-Type': 'application/json'})
assert resp.status_code == 200
# having only kwargs and no explicit x-body-name, the handler would have been passed 'body' and the individual params from body
assert json.loads(resp.data.decode('utf-8', 'replace')) == {'body': {'foo': 'a', 'bar': 'b'}, 'foo': 'a', 'bar': 'b'}
# having only kwargs, the handler would have been passed 'body'
assert json.loads(resp.data.decode('utf-8', 'replace')) == {'body': {'foo': 'a', 'bar': 'b'}, }


def test_param_sanitization(simple_app):
Expand Down
31 changes: 17 additions & 14 deletions tests/fakeapi/aiohttp_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,40 +107,43 @@ async def get_uuid():
return ConnexionResponse(body={'value': uuid.UUID(hex='e7ff66d0-3ec2-4c4e-bed0-6e4723c24c51')})


async def aiohttp_multipart_single_file(funky_funky):
async def aiohttp_multipart_single_file(myfile):
return aiohttp.web.json_response(
data={
'fileName': funky_funky.filename,
'content': funky_funky.file.read().decode('utf8')
},
'fileName': myfile.filename,
'myfile_content': myfile.file.read().decode('utf8')
},
)


async def aiohttp_multipart_many_files(files):
async def aiohttp_multipart_many_files(myfiles):
return aiohttp.web.json_response(
data={
'files_count': len(files),
'contents': [ f.file.read().decode('utf8') for f in files ]
},
'files_count': len(myfiles),
'myfiles_content': [ f.file.read().decode('utf8') for f in myfiles ]
},
)


async def aiohttp_multipart_mixed_single_file(dir_name, funky_funky):
async def aiohttp_multipart_mixed_single_file(myfile, body):
dir_name = body['dir_name']
return aiohttp.web.json_response(
data={
'dir_name': dir_name,
'fileName': funky_funky.filename,
'content': funky_funky.file.read().decode('utf8'),
'fileName': myfile.filename,
'myfile_content': myfile.file.read().decode('utf8'),
},
)


async def aiohttp_multipart_mixed_many_files(dir_name, test_count, files):
async def aiohttp_multipart_mixed_many_files(myfiles, body):
dir_name = body['dir_name']
test_count = body['test_count']
return aiohttp.web.json_response(
data={
'files_count': len(files),
'files_count': len(myfiles),
'dir_name': dir_name,
'test_count': test_count,
'contents': [ f.file.read().decode('utf8') for f in files ]
'myfiles_content': [ f.file.read().decode('utf8') for f in myfiles ]
},
)
64 changes: 32 additions & 32 deletions tests/fixtures/aiohttp/openapi_multipart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ info:
title: "{{title}}"
version: "1.0"
paths:
"/mixed_single_file":
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, I just moved the bottom two endpoints to the top because that's the same order they are in in the test handler and test requester files

"/upload_file":
post:
summary: Reads multipart data
description: Handles multipart data reading
operationId: fakeapi.aiohttp_handlers.aiohttp_multipart_mixed_single_file
summary: Uploads single file
description: Handles multipart file upload.
operationId: fakeapi.aiohttp_handlers.aiohttp_multipart_single_file
responses:
"200":
description: OK response
Expand All @@ -19,8 +19,6 @@ paths:
schema:
type: object
properties:
dir_name:
type: string
fileName:
type: string
default:
Expand All @@ -32,16 +30,14 @@ paths:
schema:
type: object
properties:
dir_name:
type: string
funky_funky:
myfile:
type: string
format: binary
"/mixed_many_files":
"/upload_files":
post:
summary: Reads multipart data
description: Handles multipart data reading
operationId: fakeapi.aiohttp_handlers.aiohttp_multipart_mixed_many_files
summary: Uploads many files
description: Handles multipart file upload.
operationId: fakeapi.aiohttp_handlers.aiohttp_multipart_many_files
responses:
"200":
description: OK response
Expand All @@ -50,10 +46,6 @@ paths:
schema:
type: object
properties:
dir_name:
type: string
test_count:
type: number
files_count:
type: number
default:
Expand All @@ -65,20 +57,16 @@ paths:
schema:
type: object
properties:
dir_name:
type: string
test_count:
type: number
files:
myfiles:
type: array
items:
type: string
format: binary
"/upload_file":
"/mixed_single_file":
post:
summary: Uploads single file
description: Handles multipart file upload.
operationId: fakeapi.aiohttp_handlers.aiohttp_multipart_single_file
summary: Reads multipart data
description: Handles multipart data reading
operationId: fakeapi.aiohttp_handlers.aiohttp_multipart_mixed_single_file
responses:
"200":
description: OK response
Expand All @@ -87,6 +75,8 @@ paths:
schema:
type: object
properties:
dir_name:
type: string
fileName:
type: string
default:
Expand All @@ -98,14 +88,16 @@ paths:
schema:
type: object
properties:
funky_funky:
dir_name:
type: string
myfile:
type: string
format: binary
"/upload_files":
"/mixed_many_files":
post:
summary: Uploads many files
description: Handles multipart file upload.
operationId: fakeapi.aiohttp_handlers.aiohttp_multipart_many_files
summary: Reads multipart data
description: Handles multipart data reading
operationId: fakeapi.aiohttp_handlers.aiohttp_multipart_mixed_many_files
responses:
"200":
description: OK response
Expand All @@ -114,6 +106,10 @@ paths:
schema:
type: object
properties:
dir_name:
type: string
test_count:
type: number
files_count:
type: number
default:
Expand All @@ -125,7 +121,11 @@ paths:
schema:
type: object
properties:
file:
dir_name:
type: string
test_count:
type: number
myfiles:
type: array
items:
type: string
Expand Down