1
from decimal import Decimal
4
from dirty_equals import IsDict, IsOneOf
5
from fastapi import FastAPI
6
from fastapi.testclient import TestClient
7
from fastapi.utils import match_pydantic_error_url
8
from pydantic import BaseModel, condecimal
15
age: condecimal(gt=Decimal(0.0))
19
def save_item_no_body(item: List[Item]):
23
client = TestClient(app)
26
def test_put_correct_body():
27
response = client.post("/items/", json=[{"name": "Foo", "age": 5}])
28
assert response.status_code == 200, response.text
29
assert response.json() == {
43
def test_jsonable_encoder_requiring_error():
44
response = client.post("/items/", json=[{"name": "Foo", "age": -1.0}])
45
assert response.status_code == 422, response.text
46
assert response.json() == IsDict(
50
"type": "greater_than",
51
"loc": ["body", 0, "age"],
52
"msg": "Input should be greater than 0",
55
"url": match_pydantic_error_url("greater_than"),
64
"ctx": {"limit_value": 0.0},
65
"loc": ["body", 0, "age"],
66
"msg": "ensure this value is greater than 0",
67
"type": "value_error.number.not_gt",
74
def test_put_incorrect_body_multiple():
75
response = client.post("/items/", json=[{"age": "five"}, {"age": "six"}])
76
assert response.status_code == 422, response.text
77
assert response.json() == IsDict(
82
"loc": ["body", 0, "name"],
83
"msg": "Field required",
84
"input": {"age": "five"},
85
"url": match_pydantic_error_url("missing"),
88
"type": "decimal_parsing",
89
"loc": ["body", 0, "age"],
90
"msg": "Input should be a valid decimal",
92
"url": match_pydantic_error_url("decimal_parsing"),
96
"loc": ["body", 1, "name"],
97
"msg": "Field required",
98
"input": {"age": "six"},
99
"url": match_pydantic_error_url("missing"),
102
"type": "decimal_parsing",
103
"loc": ["body", 1, "age"],
104
"msg": "Input should be a valid decimal",
106
"url": match_pydantic_error_url("decimal_parsing"),
115
"loc": ["body", 0, "name"],
116
"msg": "field required",
117
"type": "value_error.missing",
120
"loc": ["body", 0, "age"],
121
"msg": "value is not a valid decimal",
122
"type": "type_error.decimal",
125
"loc": ["body", 1, "name"],
126
"msg": "field required",
127
"type": "value_error.missing",
130
"loc": ["body", 1, "age"],
131
"msg": "value is not a valid decimal",
132
"type": "type_error.decimal",
139
def test_openapi_schema():
140
response = client.get("/openapi.json")
141
assert response.status_code == 200, response.text
142
assert response.json() == {
144
"info": {"title": "FastAPI", "version": "0.1.0"},
150
"description": "Successful Response",
151
"content": {"application/json": {"schema": {}}},
154
"description": "Validation Error",
156
"application/json": {
158
"$ref": "#/components/schemas/HTTPValidationError"
164
"summary": "Save Item No Body",
165
"operationId": "save_item_no_body_items__post",
168
"application/json": {
172
"items": {"$ref": "#/components/schemas/Item"},
185
"required": ["name", "age"],
188
"name": {"title": "Name", "type": "string"},
193
{"exclusiveMinimum": 0.0, "type": "number"},
202
"exclusiveMinimum": 0.0,
209
"title": "ValidationError",
210
"required": ["loc", "msg", "type"],
217
"anyOf": [{"type": "string"}, {"type": "integer"}]
220
"msg": {"title": "Message", "type": "string"},
221
"type": {"title": "Error Type", "type": "string"},
224
"HTTPValidationError": {
225
"title": "HTTPValidationError",
231
"items": {"$ref": "#/components/schemas/ValidationError"},