Skip to content

Commit

Permalink
fix: fix pydantic#170 issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ninoseki committed Feb 8, 2024
1 parent 3e12da5 commit d0d7543
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
8 changes: 7 additions & 1 deletion src/python-fastui/fastui/forms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import typing as _t
from contextlib import asynccontextmanager
from itertools import groupby
from mimetypes import MimeTypes
from operator import itemgetter
Expand Down Expand Up @@ -34,7 +35,8 @@ def __class_getitem__(cls, model: _t.Type[FormModel]) -> fastapi_params.Depends:


def fastui_form(model: _t.Type[FormModel]) -> fastapi_params.Depends:
async def run_fastui_form(request: fastapi.Request):
@asynccontextmanager
async def run_fastui_form_with_context(request: fastapi.Request):
async with request.form() as form_data:
model_data = unflatten(form_data)

Expand All @@ -46,6 +48,10 @@ async def run_fastui_form(request: fastapi.Request):
detail={'form': e.errors(include_input=False, include_url=False, include_context=False)},
)

async def run_fastui_form(request: fastapi.Request):
async with run_fastui_form_with_context(request) as obj:
yield obj

return fastapi.Depends(run_fastui_form)


Expand Down
26 changes: 13 additions & 13 deletions src/python-fastui/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ async def test_simple_form_submit():

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

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

Expand All @@ -105,7 +105,7 @@ async def test_simple_form_submit_repeat():
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).__anext__()

# insert_assert(exc_info.value.detail)
assert exc_info.value.detail == {
Expand Down Expand Up @@ -156,7 +156,7 @@ async def test_w_nested_form_submit():

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

m = await form_dep.dependency(request)
m = await form_dep.dependency(request).__anext__()
assert isinstance(m, FormWithNested)
assert m.model_dump() == {'name': 'bar', 'nested': {'x': 123}}

Expand Down Expand Up @@ -190,7 +190,7 @@ 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).__anext__()
assert m.model_dump() == {'profile_pic': file}


Expand All @@ -200,7 +200,7 @@ async def test_file_submit_repeat():
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).__anext__()

# insert_assert(exc_info.value.detail)
assert exc_info.value.detail == {
Expand Down Expand Up @@ -239,15 +239,15 @@ async def test_file_constrained_submit():
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).__anext__()
assert m.model_dump() == {'profile_pic': file}


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).__anext__()
assert m.model_dump() == {'profile_pic': file}


Expand All @@ -257,7 +257,7 @@ async def test_file_constrained_submit_too_big():
request = FakeRequest([('profile_pic', file)])

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

# insert_assert(exc_info.value.detail)
assert exc_info.value.detail == {
Expand All @@ -277,7 +277,7 @@ async def test_file_constrained_submit_wrong_type():
request = FakeRequest([('profile_pic', file)])

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

# insert_assert(exc_info.value.detail)
assert exc_info.value.detail == {
Expand Down Expand Up @@ -323,7 +323,7 @@ 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).__anext__()
assert m.model_dump() == {'files': [file]}


Expand All @@ -332,7 +332,7 @@ async def test_multiple_files_multiple():
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).__anext__()
assert m.model_dump() == {'files': [file1, file2]}


Expand Down Expand Up @@ -379,7 +379,7 @@ def test_fixed_tuple():
async def test_fixed_tuple_submit():
request = FakeRequest([('foo.0', 'bar'), ('foo.1', '123'), ('foo.2', '456')])

m = await fastui_form(FixedTuple).dependency(request)
m = await fastui_form(FixedTuple).dependency(request).__anext__()
assert m.model_dump() == {'foo': ('bar', 123, 456)}


Expand Down Expand Up @@ -426,7 +426,7 @@ def test_fixed_tuple_nested():
async def test_fixed_tuple_nested_submit():
request = FakeRequest([('bar.foo.0', 'bar'), ('bar.foo.1', '123'), ('bar.foo.2', '456')])

m = await fastui_form(NestedTuple).dependency(request)
m = await fastui_form(NestedTuple).dependency(request).__anext__()
assert m.model_dump() == {'bar': {'foo': ('bar', 123, 456)}}


Expand Down

0 comments on commit d0d7543

Please sign in to comment.