Skip to content

Commit 24af186

Browse files
Use pytest-asyncio (#77)
Co-authored-by: DisboxApp <a> Co-authored-by: Samuel Colvin <s@muelcolvin.com>
1 parent 0f577cd commit 24af186

File tree

4 files changed

+34
-28
lines changed

4 files changed

+34
-28
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ include = ["src/python-fastui/fastui"]
1515
testpaths = "src/python-fastui/tests"
1616
xfail_strict = true
1717
filterwarnings = ["error"]
18+
asyncio_mode = "auto"

src/python-fastui/requirements/test.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ coverage
22
pytest
33
pytest-pretty
44
dirty-equals
5+
pytest-asyncio

src/python-fastui/requirements/test.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
#
2-
# This file is autogenerated by pip-compile with Python 3.11
2+
# This file is autogenerated by pip-compile with Python 3.10
33
# by the following command:
44
#
55
# pip-compile --output-file=src/python-fastui/requirements/test.txt --strip-extras src/python-fastui/requirements/test.in
66
#
7+
colorama==0.4.6
8+
# via pytest
79
coverage==7.3.2
810
# via -r src/python-fastui/requirements/test.in
911
dirty-equals==0.7.1.post0
1012
# via -r src/python-fastui/requirements/test.in
13+
exceptiongroup==1.2.0
14+
# via pytest
1115
iniconfig==2.0.0
1216
# via pytest
1317
markdown-it-py==3.0.0
@@ -23,10 +27,15 @@ pygments==2.17.2
2327
pytest==7.4.3
2428
# via
2529
# -r src/python-fastui/requirements/test.in
30+
# pytest-asyncio
2631
# pytest-pretty
32+
pytest-asyncio==0.23.2
33+
# via -r src/python-fastui/requirements/test.in
2734
pytest-pretty==1.2.0
2835
# via -r src/python-fastui/requirements/test.in
2936
pytz==2023.3.post1
3037
# via dirty-equals
3138
rich==13.7.0
3239
# via pytest-pretty
40+
tomli==2.0.1
41+
# via pytest

src/python-fastui/tests/test_forms.py

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import asyncio
21
from contextlib import asynccontextmanager
32
from io import BytesIO
43
from typing import List, Tuple, Union
@@ -11,9 +10,6 @@
1110
from starlette.datastructures import FormData, Headers, UploadFile
1211
from typing_extensions import Annotated
1312

14-
# todo use pytest-asyncio
15-
await_ = asyncio.run
16-
1713

1814
class SimpleForm(BaseModel):
1915
name: str
@@ -62,23 +58,23 @@ def test_simple_form_fields():
6258
}
6359

6460

65-
def test_simple_form_submit():
61+
async def test_simple_form_submit():
6662
form_dep = fastui_form(SimpleForm)
6763

6864
request = FakeRequest([('name', 'bar'), ('size', '123')])
6965

70-
m = await_(form_dep.dependency(request))
66+
m = await form_dep.dependency(request)
7167
assert isinstance(m, SimpleForm)
7268
assert m.model_dump() == {'name': 'bar', 'size': 123}
7369

7470

75-
def test_simple_form_submit_repeat():
71+
async def test_simple_form_submit_repeat():
7672
form_dep = fastui_form(SimpleForm)
7773

7874
request = FakeRequest([('name', 'bar'), ('size', '123'), ('size', '456')])
7975

8076
with pytest.raises(HTTPException) as exc_info:
81-
await_(form_dep.dependency(request))
77+
await form_dep.dependency(request)
8278

8379
# insert_assert(exc_info.value.detail)
8480
assert exc_info.value.detail == {
@@ -124,13 +120,12 @@ def test_w_nested_form_fields():
124120
}
125121

126122

127-
def test_w_nested_form_submit():
123+
async def test_w_nested_form_submit():
128124
form_dep = fastui_form(FormWithNested)
129125

130126
request = FakeRequest([('name', 'bar'), ('nested.x', '123')])
131127

132-
# todo use pytest-asyncio
133-
m = await_(form_dep.dependency(request))
128+
m = await form_dep.dependency(request)
134129
assert isinstance(m, FormWithNested)
135130
assert m.model_dump() == {'name': 'bar', 'nested': {'x': 123}}
136131

@@ -160,21 +155,21 @@ def test_file():
160155
}
161156

162157

163-
def test_file_submit():
158+
async def test_file_submit():
164159
file = UploadFile(BytesIO(b'foobar'), size=6, filename='testing.txt')
165160
request = FakeRequest([('profile_pic', file)])
166161

167-
m = await_(fastui_form(FormWithFile).dependency(request))
162+
m = await fastui_form(FormWithFile).dependency(request)
168163
assert m.model_dump() == {'profile_pic': file}
169164

170165

171-
def test_file_submit_repeat():
166+
async def test_file_submit_repeat():
172167
file1 = UploadFile(BytesIO(b'foobar'), size=6, filename='testing1.txt')
173168
file2 = UploadFile(BytesIO(b'foobar'), size=6, filename='testing2.txt')
174169
request = FakeRequest([('profile_pic', file1), ('profile_pic', file2)])
175170

176171
with pytest.raises(HTTPException) as exc_info:
177-
await_(fastui_form(FormWithFile).dependency(request))
172+
await fastui_form(FormWithFile).dependency(request)
178173

179174
# insert_assert(exc_info.value.detail)
180175
assert exc_info.value.detail == {
@@ -208,30 +203,30 @@ def test_file_constrained():
208203
}
209204

210205

211-
def test_file_constrained_submit():
206+
async def test_file_constrained_submit():
212207
headers = Headers({'content-type': 'image/png'})
213208
file = UploadFile(BytesIO(b'foobar'), size=16_000, headers=headers)
214209
request = FakeRequest([('profile_pic', file)])
215210

216-
m = await_(fastui_form(FormWithFileConstraint).dependency(request))
211+
m = await fastui_form(FormWithFileConstraint).dependency(request)
217212
assert m.model_dump() == {'profile_pic': file}
218213

219214

220-
def test_file_constrained_submit_filename():
215+
async def test_file_constrained_submit_filename():
221216
file = UploadFile(BytesIO(b'foobar'), size=16_000, filename='image.png')
222217
request = FakeRequest([('profile_pic', file)])
223218

224-
m = await_(fastui_form(FormWithFileConstraint).dependency(request))
219+
m = await fastui_form(FormWithFileConstraint).dependency(request)
225220
assert m.model_dump() == {'profile_pic': file}
226221

227222

228-
def test_file_constrained_submit_too_big():
223+
async def test_file_constrained_submit_too_big():
229224
headers = Headers({'content-type': 'image/png'})
230225
file = UploadFile(BytesIO(b'foobar'), size=16_001, filename='image.png', headers=headers)
231226
request = FakeRequest([('profile_pic', file)])
232227

233228
with pytest.raises(HTTPException) as exc_info:
234-
await_(fastui_form(FormWithFileConstraint).dependency(request))
229+
await fastui_form(FormWithFileConstraint).dependency(request)
235230

236231
# insert_assert(exc_info.value.detail)
237232
assert exc_info.value.detail == {
@@ -245,13 +240,13 @@ def test_file_constrained_submit_too_big():
245240
}
246241

247242

248-
def test_file_constrained_submit_wrong_type():
243+
async def test_file_constrained_submit_wrong_type():
249244
headers = Headers({'content-type': 'text/plain'})
250245
file = UploadFile(BytesIO(b'foobar'), size=16, filename='testing.txt', headers=headers)
251246
request = FakeRequest([('profile_pic', file)])
252247

253248
with pytest.raises(HTTPException) as exc_info:
254-
await_(fastui_form(FormWithFileConstraint).dependency(request))
249+
await fastui_form(FormWithFileConstraint).dependency(request)
255250

256251
# insert_assert(exc_info.value.detail)
257252
assert exc_info.value.detail == {
@@ -293,18 +288,18 @@ def test_multiple_files():
293288
}
294289

295290

296-
def test_multiple_files_single():
291+
async def test_multiple_files_single():
297292
file = UploadFile(BytesIO(b'foobar'), size=16_000, filename='image.png')
298293
request = FakeRequest([('files', file)])
299294

300-
m = await_(fastui_form(FormMultipleFiles).dependency(request))
295+
m = await fastui_form(FormMultipleFiles).dependency(request)
301296
assert m.model_dump() == {'files': [file]}
302297

303298

304-
def test_multiple_files_multiple():
299+
async def test_multiple_files_multiple():
305300
file1 = UploadFile(BytesIO(b'foobar'), size=6, filename='image1.png')
306301
file2 = UploadFile(BytesIO(b'foobar'), size=6, filename='image2.png')
307302
request = FakeRequest([('files', file1), ('files', file2)])
308303

309-
m = await_(fastui_form(FormMultipleFiles).dependency(request))
304+
m = await fastui_form(FormMultipleFiles).dependency(request)
310305
assert m.model_dump() == {'files': [file1, file2]}

0 commit comments

Comments
 (0)