| 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("tutorial003_py310", marks=needs_py310), |
| 14 |
], |
| 15 |
) |
| 16 |
def get_client(request: pytest.FixtureRequest): |
| 17 |
mod = importlib.import_module(f"docs_src.query_params.{request.param}") |
| 18 |
|
| 19 |
client = TestClient(mod.app) |
| 20 |
return client |
| 21 |
|
| 22 |
|
| 23 |
@pytest.mark.parametrize( |
| 24 |
("path", "expected_json"), |
| 25 |
[ |
| 26 |
( |
| 27 |
"/items/foo", |
| 28 |
{ |
| 29 |
"item_id": "foo", |
| 30 |
"description": "This is an amazing item that has a long description", |
| 31 |
}, |
| 32 |
), |
| 33 |
( |
| 34 |
"/items/bar?q=somequery", |
| 35 |
{ |
| 36 |
"item_id": "bar", |
| 37 |
"q": "somequery", |
| 38 |
"description": "This is an amazing item that has a long description", |
| 39 |
}, |
| 40 |
), |
| 41 |
( |
| 42 |
"/items/baz?short=true", |
| 43 |
{"item_id": "baz"}, |
| 44 |
), |
| 45 |
], |
| 46 |
) |
| 47 |
def test_read_user_item(client: TestClient, path, expected_json): |
| 48 |
response = client.get(path) |
| 49 |
assert response.status_code == 200 |
| 50 |
assert response.json() == expected_json |
| 51 |
|
| 52 |
|
| 53 |
def test_openapi_schema(client: TestClient): |
| 54 |
response = client.get("/openapi.json") |
| 55 |
assert response.status_code == 200 |
| 56 |
assert response.json() == snapshot( |
| 57 |
{ |
| 58 |
"openapi": "3.1.0", |
| 59 |
"info": {"title": "FastAPI", "version": "0.1.0"}, |
| 60 |
"paths": { |
| 61 |
"/items/{item_id}": { |
| 62 |
"get": { |
| 63 |
"summary": "Read Item", |
| 64 |
"operationId": "read_item_items__item_id__get", |
| 65 |
"parameters": [ |
| 66 |
{ |
| 67 |
"required": True, |
| 68 |
"schema": {"title": "Item Id", "type": "string"}, |
| 69 |
"name": "item_id", |
| 70 |
"in": "path", |
| 71 |
}, |
| 72 |
{ |
| 73 |
"required": False, |
| 74 |
"schema": { |
| 75 |
"title": "Q", |
| 76 |
"anyOf": [ |
| 77 |
{ |
| 78 |
"type": "string", |
| 79 |
}, |
| 80 |
{ |
| 81 |
"type": "null", |
| 82 |
}, |
| 83 |
], |
| 84 |
}, |
| 85 |
"name": "q", |
| 86 |
"in": "query", |
| 87 |
}, |
| 88 |
{ |
| 89 |
"required": False, |
| 90 |
"schema": { |
| 91 |
"title": "Short", |
| 92 |
"type": "boolean", |
| 93 |
"default": False, |
| 94 |
}, |
| 95 |
"name": "short", |
| 96 |
"in": "query", |
| 97 |
}, |
| 98 |
], |
| 99 |
"responses": { |
| 100 |
"200": { |
| 101 |
"description": "Successful Response", |
| 102 |
"content": {"application/json": {"schema": {}}}, |
| 103 |
}, |
| 104 |
"422": { |
| 105 |
"content": { |
| 106 |
"application/json": { |
| 107 |
"schema": { |
| 108 |
"$ref": "#/components/schemas/HTTPValidationError", |
| 109 |
}, |
| 110 |
}, |
| 111 |
}, |
| 112 |
"description": "Validation Error", |
| 113 |
}, |
| 114 |
}, |
| 115 |
} |
| 116 |
} |
| 117 |
}, |
| 118 |
"components": { |
| 119 |
"schemas": { |
| 120 |
"ValidationError": { |
| 121 |
"title": "ValidationError", |
| 122 |
"required": ["loc", "msg", "type"], |
| 123 |
"type": "object", |
| 124 |
"properties": { |
| 125 |
"loc": { |
| 126 |
"title": "Location", |
| 127 |
"type": "array", |
| 128 |
"items": { |
| 129 |
"anyOf": [{"type": "string"}, {"type": "integer"}] |
| 130 |
}, |
| 131 |
}, |
| 132 |
"msg": {"title": "Message", "type": "string"}, |
| 133 |
"type": {"title": "Error Type", "type": "string"}, |
| 134 |
"input": {"title": "Input"}, |
| 135 |
"ctx": {"title": "Context", "type": "object"}, |
| 136 |
}, |
| 137 |
}, |
| 138 |
"HTTPValidationError": { |
| 139 |
"title": "HTTPValidationError", |
| 140 |
"type": "object", |
| 141 |
"properties": { |
| 142 |
"detail": { |
| 143 |
"title": "Detail", |
| 144 |
"type": "array", |
| 145 |
"items": { |
| 146 |
"$ref": "#/components/schemas/ValidationError" |
| 147 |
}, |
| 148 |
} |
| 149 |
}, |
| 150 |
}, |
| 151 |
} |
| 152 |
}, |
| 153 |
} |
| 154 |
) |