Skip to content

Commit

Permalink
HH-200459 add handler-return to example app
Browse files Browse the repository at this point in the history
  • Loading branch information
bokshitsky committed Dec 4, 2023
1 parent fb0e201 commit 523d57c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
6 changes: 6 additions & 0 deletions examples/example_app/pages/handler_return_dict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import frontik.handler


class Page(frontik.handler.PageHandler):
def get_page(self) -> dict:
return {'str_field': 'Привет'}
12 changes: 12 additions & 0 deletions examples/example_app/pages/handler_return_pydantic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from pydantic import BaseModel

import frontik.handler


class ResponseModel(BaseModel):
str_field: str


class Page(frontik.handler.PageHandler):
def get_page(self) -> ResponseModel:
return ResponseModel(str_field='Привет')
2 changes: 2 additions & 0 deletions examples/frontik.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ stderr_log = True
jinja_template_root = 'templates'

consul_enabled=False

debug=True
7 changes: 5 additions & 2 deletions tests/test_handler_returned_value_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@
class _PydanticModel(BaseModel):
int_field: int
bool_field: bool
str_field: str


class ReturnPydanticModelHandler(PageHandler):
async def get_page(self) -> _PydanticModel:
return _PydanticModel(int_field=1, bool_field=True)
return _PydanticModel(int_field=1, bool_field=True, str_field='Ну привет')


class ReturnDictHandler(PageHandler):
async def get_page(self) -> dict:
return {'is_dict': True}
return {'is_dict': True, 'msg': 'Ну привет'}


class TestApplication(FrontikApplication):
Expand All @@ -40,8 +41,10 @@ def frontik_app(self) -> TestApplication:
async def test_get_dict(self):
resp = await self.fetch_json('/return_dict')
assert resp['is_dict'] is True
assert resp['msg'] == 'Ну привет'

async def test_get_pydantic_model(self):
resp = await self.fetch_json('/return_pydantic')
assert resp['int_field'] == 1
assert resp['bool_field'] is True
assert resp['str_field'] == 'Ну привет'

0 comments on commit 523d57c

Please sign in to comment.