Skip to content

Commit 0ad25d3

Browse files
committed
unit test improvements -- now testing the contents of the files we upload too
1 parent 5b69e90 commit 0ad25d3

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

tests/aiohttp/test_aiohttp_multipart.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import aiohttp
44
import pytest
5+
from pathlib import Path
56

67
from connexion import AioHttpApp
78

@@ -37,13 +38,17 @@ async def test_single_file_upload(aiohttp_app, aiohttp_client):
3738
data = await resp.json()
3839
assert resp.status == 200
3940
assert data['fileName'] == f'{__name__}.py'
41+
assert data['content'] == Path(__file__).read_bytes().decode('utf8')
4042

4143

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

4547
dir_name = os.path.dirname(__file__)
46-
files_field = [('files', open(f'{dir_name}/{file_name}', 'rb')) for file_name in os.listdir(dir_name) if file_name.endswith('.py')]
48+
files_field = [
49+
('files', open(f'{dir_name}/{file_name}', 'rb')) \
50+
for file_name in os.listdir(dir_name) if file_name.endswith('.py')
51+
]
4752

4853
form_data = aiohttp.FormData(fields=files_field)
4954

@@ -75,13 +80,18 @@ async def test_mixed_multipart_single_file(aiohttp_app, aiohttp_client):
7580
assert resp.status == 200
7681
assert data['dirName'] == os.path.dirname(__file__)
7782
assert data['fileName'] == f'{__name__}.py'
83+
assert data['content'] == Path(__file__).read_bytes().decode('utf8')
84+
7885

7986

8087
async def test_mixed_multipart_many_files(aiohttp_app, aiohttp_client):
8188
app_client = await aiohttp_client(aiohttp_app.app)
8289

8390
dir_name = os.path.dirname(__file__)
84-
files_field = [('files', open(f'{dir_name}/{file_name}', 'rb')) for file_name in os.listdir(dir_name) if file_name.endswith('.py')]
91+
files_field = [
92+
('files', open(f'{dir_name}/{file_name}', 'rb')) \
93+
for file_name in sorted(os.listdir(dir_name)) if file_name.endswith('.py')
94+
]
8595

8696
form_data = aiohttp.FormData(fields=files_field)
8797
form_data.add_field('dirName', os.path.dirname(__file__))
@@ -98,3 +108,7 @@ async def test_mixed_multipart_many_files(aiohttp_app, aiohttp_client):
98108
assert data['dirName'] == os.path.dirname(__file__)
99109
assert data['testCount'] == len(files_field)
100110
assert data['filesCount'] == len(files_field)
111+
assert data['contents'] == [
112+
Path(f'{dir_name}/{file_name}').read_bytes().decode('utf8') \
113+
for file_name in sorted(os.listdir(dir_name)) if file_name.endswith('.py')
114+
]

tests/fakeapi/aiohttp_handlers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ async def get_uuid():
8888

8989
async def aiohttp_multipart_single_file(funky_funky):
9090
return aiohttp.web.json_response(
91-
data={'fileName': funky_funky.filename},
91+
data={'fileName': funky_funky.filename, 'content': funky_funky.file.read().decode('utf8')},
9292
)
9393

9494

@@ -103,6 +103,7 @@ async def aiohttp_multipart_mixed_single_file(dir_name, funky_funky):
103103
data={
104104
'dirName': dir_name,
105105
'fileName': funky_funky.filename,
106+
'content': funky_funky.file.read().decode('utf8'),
106107
},
107108
)
108109

@@ -113,5 +114,6 @@ async def aiohttp_multipart_mixed_many_files(dir_name, test_count, files):
113114
'filesCount': len(files),
114115
'dirName': dir_name,
115116
'testCount': test_count,
117+
'contents': [ f.file.read().decode('utf8') for f in files ]
116118
},
117119
)

0 commit comments

Comments
 (0)