Skip to content

Commit

Permalink
Use pytest-asyncio.
Browse files Browse the repository at this point in the history
  • Loading branch information
DisboxApp committed Dec 6, 2023
1 parent 0f577cd commit 03cab97
Showing 1 changed file with 44 additions and 26 deletions.
70 changes: 44 additions & 26 deletions src/python-fastui/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,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 +59,27 @@ def test_simple_form_fields():
}


def test_simple_form_submit():
@pytest.mark.asyncio
@pytest.mark.filterwarnings('ignore::DeprecationWarning')
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():
@pytest.mark.asyncio
@pytest.mark.filterwarnings('ignore::DeprecationWarning')
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 +125,14 @@ def test_w_nested_form_fields():
}


def test_w_nested_form_submit():
@pytest.mark.asyncio
@pytest.mark.filterwarnings('ignore::DeprecationWarning')
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 +162,25 @@ def test_file():
}


def test_file_submit():
@pytest.mark.asyncio
@pytest.mark.filterwarnings('ignore::DeprecationWarning')
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():
@pytest.mark.asyncio
@pytest.mark.filterwarnings('ignore::DeprecationWarning')
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 +214,36 @@ def test_file_constrained():
}


def test_file_constrained_submit():
@pytest.mark.asyncio
@pytest.mark.filterwarnings('ignore::DeprecationWarning')
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():
@pytest.mark.asyncio
@pytest.mark.filterwarnings('ignore::DeprecationWarning')
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():
@pytest.mark.asyncio
@pytest.mark.filterwarnings('ignore::DeprecationWarning')
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 +257,15 @@ def test_file_constrained_submit_too_big():
}


def test_file_constrained_submit_wrong_type():
@pytest.mark.asyncio
@pytest.mark.filterwarnings('ignore::DeprecationWarning')
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 +307,22 @@ def test_multiple_files():
}


def test_multiple_files_single():
@pytest.mark.asyncio
@pytest.mark.filterwarnings('ignore::DeprecationWarning')
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():
@pytest.mark.asyncio
@pytest.mark.filterwarnings('ignore::DeprecationWarning')
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 03cab97

Please sign in to comment.