Skip to content

Commit 92ded72

Browse files
committed
added test to fix coverage
1 parent 1b13503 commit 92ded72

File tree

3 files changed

+62
-14
lines changed

3 files changed

+62
-14
lines changed

ellar/common/params/resolvers/parameter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ async def get_request_body(self, ctx: IExecutionContext) -> t.Any:
143143
return body_bytes
144144
except json.JSONDecodeError as e:
145145
raise RequestValidationError([ErrorWrapper(e, ("body", e.pos))]) from e
146-
except Exception as e:
146+
except Exception as e: # pragma: no cover
147147
raise HTTPException(
148148
status_code=400, detail="There was an error parsing the body"
149149
) from e

tests/test_routing/test_body_schema.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,38 @@ def test_get_with_body(test_client_factory):
107107
body = {"name": "Foo", "description": "Some description", "price": 5.5}
108108
response = client.post("/product", json=body)
109109
assert response.json() == body
110+
111+
112+
def test_body_fails_for_invalid_json_data(test_client_factory):
113+
client = test_client_factory(app)
114+
body = """
115+
{
116+
"name": "John",
117+
"age": 30,
118+
"is_student": True,
119+
"favorite_colors": ["red", "blue", "green"],
120+
"address": {
121+
"street": "123 Main St",
122+
"city": "Some City",
123+
"zip": "12345"
124+
},
125+
"unterminated_quote": "This string is not properly terminated,
126+
}
127+
"""
128+
response = client.post("/product", data=body)
129+
assert response.json() == {
130+
"detail": [
131+
{
132+
"loc": ["body", 56],
133+
"msg": "Expecting value: line 5 column 19 (char 56)",
134+
"type": "value_error.jsondecode",
135+
"ctx": {
136+
"msg": "Expecting value",
137+
"doc": '\n{\n "name": "John",\n "age": 30,\n "is_student": True,\n "favorite_colors": ["red", "blue", "green"],\n "address": {\n "street": "123 Main St",\n "city": "Some City",\n "zip": "12345"\n },\n "unterminated_quote": "This string is not properly terminated,\n}\n',
138+
"pos": 56,
139+
"lineno": 5,
140+
"colno": 19,
141+
},
142+
}
143+
]
144+
}

tests/test_routing/test_body_union_schema.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Union
22

3-
from ellar.common import post
3+
from ellar.common import Body, post
44
from ellar.common.serializer import serialize_object
55
from ellar.openapi import OpenAPIDocumentBuilder
66
from ellar.testing import Test
@@ -11,12 +11,14 @@
1111

1212

1313
@post("/items/")
14-
def save_union_body(item: Union[OtherItem, Item]):
15-
return {"item": item}
14+
def save_union_body_and_embedded_body(
15+
item: Union[OtherItem, Item], qty: int = Body(12)
16+
):
17+
return {"item": item, "qty": qty}
1618

1719

1820
app = tm.create_application()
19-
app.router.append(save_union_body)
21+
app.router.append(save_union_body_and_embedded_body)
2022

2123
client = tm.get_test_client()
2224

@@ -32,11 +34,7 @@ def save_union_body(item: Union[OtherItem, Item]):
3234
"content": {
3335
"application/json": {
3436
"schema": {
35-
"title": "Item",
36-
"anyOf": [
37-
{"$ref": "#/components/schemas/OtherItem"},
38-
{"$ref": "#/components/schemas/Item"},
39-
],
37+
"$ref": "#/components/schemas/body_save_union_body_items__post"
4038
}
4139
}
4240
},
@@ -104,6 +102,21 @@ def save_union_body(item: Union[OtherItem, Item]):
104102
"type": {"title": "Error Type", "type": "string"},
105103
},
106104
},
105+
"body_save_union_body_items__post": {
106+
"title": "body_save_union_body_items__post",
107+
"required": ["item"],
108+
"type": "object",
109+
"properties": {
110+
"item": {
111+
"title": "Item",
112+
"anyOf": [
113+
{"$ref": "#/components/schemas/OtherItem"},
114+
{"$ref": "#/components/schemas/Item"},
115+
],
116+
},
117+
"qty": {"title": "Qty", "type": "integer", "default": 12},
118+
},
119+
},
107120
}
108121
},
109122
"tags": [],
@@ -116,12 +129,12 @@ def test_item_openapi_schema():
116129

117130

118131
def test_post_other_item():
119-
response = client.post("/items/", json={"price": 100})
132+
response = client.post("/items/", json={"item": {"price": 100}})
120133
assert response.status_code == 200, response.text
121-
assert response.json() == {"item": {"price": 100}}
134+
assert response.json() == {"item": {"price": 100}, "qty": 12}
122135

123136

124137
def test_post_item():
125-
response = client.post("/items/", json={"name": "Foo"})
138+
response = client.post("/items/", json={"item": {"name": "Foo"}})
126139
assert response.status_code == 200, response.text
127-
assert response.json() == {"item": {"name": "Foo"}}
140+
assert response.json() == {"item": {"name": "Foo"}, "qty": 12}

0 commit comments

Comments
 (0)