Skip to content

Commit

Permalink
Use pytest-asyncio (#77)
Browse files Browse the repository at this point in the history
Co-authored-by: DisboxApp <a>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
  • Loading branch information
WolfDWyc and samuelcolvin committed Dec 6, 2023
1 parent 0f577cd commit 24af186
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 28 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ include = ["src/python-fastui/fastui"]
testpaths = "src/python-fastui/tests"
xfail_strict = true
filterwarnings = ["error"]
asyncio_mode = "auto"
1 change: 1 addition & 0 deletions src/python-fastui/requirements/test.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ coverage
pytest
pytest-pretty
dirty-equals
pytest-asyncio
11 changes: 10 additions & 1 deletion src/python-fastui/requirements/test.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
#
# This file is autogenerated by pip-compile with Python 3.11
# This file is autogenerated by pip-compile with Python 3.10
# by the following command:
#
# pip-compile --output-file=src/python-fastui/requirements/test.txt --strip-extras src/python-fastui/requirements/test.in
#
colorama==0.4.6
# via pytest
coverage==7.3.2
# via -r src/python-fastui/requirements/test.in
dirty-equals==0.7.1.post0
# via -r src/python-fastui/requirements/test.in
exceptiongroup==1.2.0
# via pytest
iniconfig==2.0.0
# via pytest
markdown-it-py==3.0.0
Expand All @@ -23,10 +27,15 @@ pygments==2.17.2
pytest==7.4.3
# via
# -r src/python-fastui/requirements/test.in
# pytest-asyncio
# pytest-pretty
pytest-asyncio==0.23.2
# via -r src/python-fastui/requirements/test.in
pytest-pretty==1.2.0
# via -r src/python-fastui/requirements/test.in
pytz==2023.3.post1
# via dirty-equals
rich==13.7.0
# via pytest-pretty
tomli==2.0.1
# via pytest
49 changes: 22 additions & 27 deletions src/python-fastui/tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import asyncio
from contextlib import asynccontextmanager
from io import BytesIO
from typing import List, Tuple, Union
Expand All @@ -11,9 +10,6 @@
from starlette.datastructures import FormData, Headers, UploadFile
from typing_extensions import Annotated

# todo use pytest-asyncio
await_ = asyncio.run


class SimpleForm(BaseModel):
name: str
Expand Down Expand Up @@ -62,23 +58,23 @@ def test_simple_form_fields():
}


def test_simple_form_submit():
async def test_simple_form_submit():
form_dep = fastui_form(SimpleForm)

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

m = await_(form_dep.dependency(request))
m = await form_dep.dependency(request)
assert isinstance(m, SimpleForm)
assert m.model_dump() == {'name': 'bar', 'size': 123}


def test_simple_form_submit_repeat():
async def test_simple_form_submit_repeat():
form_dep = fastui_form(SimpleForm)

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

with pytest.raises(HTTPException) as exc_info:
await_(form_dep.dependency(request))
await form_dep.dependency(request)

# insert_assert(exc_info.value.detail)
assert exc_info.value.detail == {
Expand Down Expand Up @@ -124,13 +120,12 @@ def test_w_nested_form_fields():
}


def test_w_nested_form_submit():
async def test_w_nested_form_submit():
form_dep = fastui_form(FormWithNested)

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

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

Expand Down Expand Up @@ -160,21 +155,21 @@ def test_file():
}


def test_file_submit():
async def test_file_submit():
file = UploadFile(BytesIO(b'foobar'), size=6, filename='testing.txt')
request = FakeRequest([('profile_pic', file)])

m = await_(fastui_form(FormWithFile).dependency(request))
m = await fastui_form(FormWithFile).dependency(request)
assert m.model_dump() == {'profile_pic': file}


def test_file_submit_repeat():
async def test_file_submit_repeat():
file1 = UploadFile(BytesIO(b'foobar'), size=6, filename='testing1.txt')
file2 = UploadFile(BytesIO(b'foobar'), size=6, filename='testing2.txt')
request = FakeRequest([('profile_pic', file1), ('profile_pic', file2)])

with pytest.raises(HTTPException) as exc_info:
await_(fastui_form(FormWithFile).dependency(request))
await fastui_form(FormWithFile).dependency(request)

# insert_assert(exc_info.value.detail)
assert exc_info.value.detail == {
Expand Down Expand Up @@ -208,30 +203,30 @@ def test_file_constrained():
}


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

m = await_(fastui_form(FormWithFileConstraint).dependency(request))
m = await fastui_form(FormWithFileConstraint).dependency(request)
assert m.model_dump() == {'profile_pic': file}


def test_file_constrained_submit_filename():
async def test_file_constrained_submit_filename():
file = UploadFile(BytesIO(b'foobar'), size=16_000, filename='image.png')
request = FakeRequest([('profile_pic', file)])

m = await_(fastui_form(FormWithFileConstraint).dependency(request))
m = await fastui_form(FormWithFileConstraint).dependency(request)
assert m.model_dump() == {'profile_pic': file}


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

with pytest.raises(HTTPException) as exc_info:
await_(fastui_form(FormWithFileConstraint).dependency(request))
await fastui_form(FormWithFileConstraint).dependency(request)

# insert_assert(exc_info.value.detail)
assert exc_info.value.detail == {
Expand All @@ -245,13 +240,13 @@ def test_file_constrained_submit_too_big():
}


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

with pytest.raises(HTTPException) as exc_info:
await_(fastui_form(FormWithFileConstraint).dependency(request))
await fastui_form(FormWithFileConstraint).dependency(request)

# insert_assert(exc_info.value.detail)
assert exc_info.value.detail == {
Expand Down Expand Up @@ -293,18 +288,18 @@ def test_multiple_files():
}


def test_multiple_files_single():
async def test_multiple_files_single():
file = UploadFile(BytesIO(b'foobar'), size=16_000, filename='image.png')
request = FakeRequest([('files', file)])

m = await_(fastui_form(FormMultipleFiles).dependency(request))
m = await fastui_form(FormMultipleFiles).dependency(request)
assert m.model_dump() == {'files': [file]}


def test_multiple_files_multiple():
async def test_multiple_files_multiple():
file1 = UploadFile(BytesIO(b'foobar'), size=6, filename='image1.png')
file2 = UploadFile(BytesIO(b'foobar'), size=6, filename='image2.png')
request = FakeRequest([('files', file1), ('files', file2)])

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

0 comments on commit 24af186

Please sign in to comment.