| 1 |
import importlib |
| 2 |
|
| 3 |
import pytest |
| 4 |
from fastapi.testclient import TestClient |
| 5 |
from inline_snapshot import snapshot |
| 6 |
|
| 7 |
|
| 8 |
@pytest.fixture( |
| 9 |
name="client", |
| 10 |
params=[ |
| 11 |
"tutorial009_py310", |
| 12 |
], |
| 13 |
) |
| 14 |
def get_client(request: pytest.FixtureRequest): |
| 15 |
mod = importlib.import_module(f"docs_src.body_nested_models.{request.param}") |
| 16 |
|
| 17 |
client = TestClient(mod.app) |
| 18 |
return client |
| 19 |
|
| 20 |
|
| 21 |
def test_post_body(client: TestClient): |
| 22 |
data = {"2": 2.2, "3": 3.3} |
| 23 |
response = client.post("/index-weights/", json=data) |
| 24 |
assert response.status_code == 200, response.text |
| 25 |
assert response.json() == data |
| 26 |
|
| 27 |
|
| 28 |
def test_post_invalid_body(client: TestClient): |
| 29 |
data = {"foo": 2.2, "3": 3.3} |
| 30 |
response = client.post("/index-weights/", json=data) |
| 31 |
assert response.status_code == 422, response.text |
| 32 |
assert response.json() == { |
| 33 |
"detail": [ |
| 34 |
{ |
| 35 |
"type": "int_parsing", |
| 36 |
"loc": ["body", "foo", "[key]"], |
| 37 |
"msg": "Input should be a valid integer, unable to parse string as an integer", |
| 38 |
"input": "foo", |
| 39 |
} |
| 40 |
] |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
def test_openapi_schema(client: TestClient): |
| 45 |
response = client.get("/openapi.json") |
| 46 |
assert response.status_code == 200, response.text |
| 47 |
assert response.json() == snapshot( |
| 48 |
{ |
| 49 |
"openapi": "3.1.0", |
| 50 |
"info": {"title": "FastAPI", "version": "0.1.0"}, |
| 51 |
"paths": { |
| 52 |
"/index-weights/": { |
| 53 |
"post": { |
| 54 |
"responses": { |
| 55 |
"200": { |
| 56 |
"description": "Successful Response", |
| 57 |
"content": {"application/json": {"schema": {}}}, |
| 58 |
}, |
| 59 |
"422": { |
| 60 |
"description": "Validation Error", |
| 61 |
"content": { |
| 62 |
"application/json": { |
| 63 |
"schema": { |
| 64 |
"$ref": "#/components/schemas/HTTPValidationError" |
| 65 |
} |
| 66 |
} |
| 67 |
}, |
| 68 |
}, |
| 69 |
}, |
| 70 |
"summary": "Create Index Weights", |
| 71 |
"operationId": "create_index_weights_index_weights__post", |
| 72 |
"requestBody": { |
| 73 |
"content": { |
| 74 |
"application/json": { |
| 75 |
"schema": { |
| 76 |
"title": "Weights", |
| 77 |
"type": "object", |
| 78 |
"additionalProperties": {"type": "number"}, |
| 79 |
} |
| 80 |
} |
| 81 |
}, |
| 82 |
"required": True, |
| 83 |
}, |
| 84 |
} |
| 85 |
} |
| 86 |
}, |
| 87 |
"components": { |
| 88 |
"schemas": { |
| 89 |
"ValidationError": { |
| 90 |
"title": "ValidationError", |
| 91 |
"required": ["loc", "msg", "type"], |
| 92 |
"type": "object", |
| 93 |
"properties": { |
| 94 |
"loc": { |
| 95 |
"title": "Location", |
| 96 |
"type": "array", |
| 97 |
"items": { |
| 98 |
"anyOf": [{"type": "string"}, {"type": "integer"}] |
| 99 |
}, |
| 100 |
}, |
| 101 |
"msg": {"title": "Message", "type": "string"}, |
| 102 |
"type": {"title": "Error Type", "type": "string"}, |
| 103 |
"input": {"title": "Input"}, |
| 104 |
"ctx": {"title": "Context", "type": "object"}, |
| 105 |
}, |
| 106 |
}, |
| 107 |
"HTTPValidationError": { |
| 108 |
"title": "HTTPValidationError", |
| 109 |
"type": "object", |
| 110 |
"properties": { |
| 111 |
"detail": { |
| 112 |
"title": "Detail", |
| 113 |
"type": "array", |
| 114 |
"items": { |
| 115 |
"$ref": "#/components/schemas/ValidationError" |
| 116 |
}, |
| 117 |
} |
| 118 |
}, |
| 119 |
}, |
| 120 |
} |
| 121 |
}, |
| 122 |
} |
| 123 |
) |