Skip to content

Commit

Permalink
add test for post method
Browse files Browse the repository at this point in the history
  • Loading branch information
mrtedn21 committed Nov 15, 2023
1 parent ff8540c commit 74d572c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
2 changes: 2 additions & 0 deletions martin_eden/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ async def _get_response_for_post_method(
response = await controller(**{
dataclass_name: dataclass_from_dict(dataclass_object, request_data)
})
if isinstance(response, (list, dict)):
response = json.dumps(response)
response = self._response_of_controller_to_str(controller, response)
return response

Expand Down
9 changes: 9 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,12 @@ async def get_users(query_params: list) -> str:
@register_route('/test/', 'get')
async def get_openapi_schema() -> str:
return 'test'


@register_route(
'/test/', 'post',
request_schema=TestSchema(),
response_schema=TestSchema(),
)
async def create_test(test: TestDataclass) -> list[TestDataclass]:
return [test.pk, test.name, test.age]
19 changes: 17 additions & 2 deletions tests/test_http_message_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ async def test_openapi_schema(http_get_request):
'name': 'test__name__like',
'schema': {'type': 'string'},
} in openapi_result['paths']['/test_query/']['get']['parameters']
assert openapi_result['paths']['/test/'] == {
'get': {'operationId': 'test_get'},
assert openapi_result['paths']['/test/']['get'] == {
'operationId': 'test_get'
}


Expand All @@ -106,3 +106,18 @@ async def test_query_params(http_get_request, query_param_source, query_param_re

parser = HttpHeadersParser(response.decode('utf8'))
assert parser.body == query_param_result


@pytest.mark.asyncio
async def test_post_method(http_get_request):
http_get_request = (
http_get_request
.replace(b'/users/', b'/test/')
.replace(b'GET', b'POST')
+ b'\n{"pk": 1, "name": "martin", "age": 30}'
)
handler = HttpMessageHandler(http_get_request)
response = await handler.handle_request()

parser = HttpHeadersParser(response.decode('utf8'))
assert parser.body == '[1, "martin", 30]'

0 comments on commit 74d572c

Please sign in to comment.