| 1 |
import importlib |
| 2 |
|
| 3 |
import pytest |
| 4 |
from fastapi.testclient import TestClient |
| 5 |
from inline_snapshot import snapshot |
| 6 |
|
| 7 |
from ...utils import needs_py310 |
| 8 |
|
| 9 |
|
| 10 |
@pytest.fixture( |
| 11 |
name="client", |
| 12 |
params=[ |
| 13 |
pytest.param("tutorial001_py310", marks=needs_py310), |
| 14 |
pytest.param("tutorial001_an_py310", marks=needs_py310), |
| 15 |
], |
| 16 |
) |
| 17 |
def get_client(request: pytest.FixtureRequest) -> TestClient: |
| 18 |
mod = importlib.import_module( |
| 19 |
f"docs_src.path_params_numeric_validations.{request.param}" |
| 20 |
) |
| 21 |
return TestClient(mod.app) |
| 22 |
|
| 23 |
|
| 24 |
@pytest.mark.parametrize( |
| 25 |
"path,expected_response", |
| 26 |
[ |
| 27 |
("/items/42", {"item_id": 42}), |
| 28 |
("/items/123?item-query=somequery", {"item_id": 123, "q": "somequery"}), |
| 29 |
], |
| 30 |
) |
| 31 |
def test_read_items(client: TestClient, path, expected_response): |
| 32 |
response = client.get(path) |
| 33 |
assert response.status_code == 200, response.text |
| 34 |
assert response.json() == expected_response |
| 35 |
|
| 36 |
|
| 37 |
def test_read_items_invalid_item_id(client: TestClient): |
| 38 |
response = client.get("/items/invalid_id") |
| 39 |
assert response.status_code == 422, response.text |
| 40 |
assert response.json() == { |
| 41 |
"detail": [ |
| 42 |
{ |
| 43 |
"loc": ["path", "item_id"], |
| 44 |
"input": "invalid_id", |
| 45 |
"msg": "Input should be a valid integer, unable to parse string as an integer", |
| 46 |
"type": "int_parsing", |
| 47 |
} |
| 48 |
] |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
def test_openapi_schema(client: TestClient): |
| 53 |
response = client.get("/openapi.json") |
| 54 |
assert response.status_code == 200, response.text |
| 55 |
assert response.json() == snapshot( |
| 56 |
{ |
| 57 |
"openapi": "3.1.0", |
| 58 |
"info": {"title": "FastAPI", "version": "0.1.0"}, |
| 59 |
"paths": { |
| 60 |
"/items/{item_id}": { |
| 61 |
"get": { |
| 62 |
"summary": "Read Items", |
| 63 |
"operationId": "read_items_items__item_id__get", |
| 64 |
"parameters": [ |
| 65 |
{ |
| 66 |
"required": True, |
| 67 |
"schema": { |
| 68 |
"title": "The ID of the item to get", |
| 69 |
"type": "integer", |
| 70 |
}, |
| 71 |
"name": "item_id", |
| 72 |
"in": "path", |
| 73 |
}, |
| 74 |
{ |
| 75 |
"required": False, |
| 76 |
"schema": { |
| 77 |
"anyOf": [ |
| 78 |
{ |
| 79 |
"type": "string", |
| 80 |
}, |
| 81 |
{ |
| 82 |
"type": "null", |
| 83 |
}, |
| 84 |
], |
| 85 |
"title": "Item-Query", |
| 86 |
}, |
| 87 |
"name": "item-query", |
| 88 |
"in": "query", |
| 89 |
}, |
| 90 |
], |
| 91 |
"responses": { |
| 92 |
"200": { |
| 93 |
"description": "Successful Response", |
| 94 |
"content": { |
| 95 |
"application/json": { |
| 96 |
"schema": {}, |
| 97 |
} |
| 98 |
}, |
| 99 |
}, |
| 100 |
"422": { |
| 101 |
"content": { |
| 102 |
"application/json": { |
| 103 |
"schema": { |
| 104 |
"$ref": "#/components/schemas/HTTPValidationError", |
| 105 |
}, |
| 106 |
}, |
| 107 |
}, |
| 108 |
"description": "Validation Error", |
| 109 |
}, |
| 110 |
}, |
| 111 |
} |
| 112 |
} |
| 113 |
}, |
| 114 |
"components": { |
| 115 |
"schemas": { |
| 116 |
"HTTPValidationError": { |
| 117 |
"properties": { |
| 118 |
"detail": { |
| 119 |
"items": { |
| 120 |
"$ref": "#/components/schemas/ValidationError", |
| 121 |
}, |
| 122 |
"title": "Detail", |
| 123 |
"type": "array", |
| 124 |
}, |
| 125 |
}, |
| 126 |
"title": "HTTPValidationError", |
| 127 |
"type": "object", |
| 128 |
}, |
| 129 |
"ValidationError": { |
| 130 |
"properties": { |
| 131 |
"ctx": {"title": "Context", "type": "object"}, |
| 132 |
"input": {"title": "Input"}, |
| 133 |
"loc": { |
| 134 |
"items": { |
| 135 |
"anyOf": [ |
| 136 |
{ |
| 137 |
"type": "string", |
| 138 |
}, |
| 139 |
{ |
| 140 |
"type": "integer", |
| 141 |
}, |
| 142 |
], |
| 143 |
}, |
| 144 |
"title": "Location", |
| 145 |
"type": "array", |
| 146 |
}, |
| 147 |
"msg": { |
| 148 |
"title": "Message", |
| 149 |
"type": "string", |
| 150 |
}, |
| 151 |
"type": { |
| 152 |
"title": "Error Type", |
| 153 |
"type": "string", |
| 154 |
}, |
| 155 |
}, |
| 156 |
"required": [ |
| 157 |
"loc", |
| 158 |
"msg", |
| 159 |
"type", |
| 160 |
], |
| 161 |
"title": "ValidationError", |
| 162 |
"type": "object", |
| 163 |
}, |
| 164 |
}, |
| 165 |
}, |
| 166 |
} |
| 167 |
) |