| 1 |
import importlib |
| 2 |
|
| 3 |
import pytest |
| 4 |
from dirty_equals import IsStr |
| 5 |
from fastapi.testclient import TestClient |
| 6 |
from inline_snapshot import snapshot |
| 7 |
|
| 8 |
from ...utils import needs_py310 |
| 9 |
|
| 10 |
|
| 11 |
@pytest.fixture( |
| 12 |
name="client", |
| 13 |
params=[ |
| 14 |
pytest.param("tutorial015_an_py310", marks=[needs_py310]), |
| 15 |
], |
| 16 |
) |
| 17 |
def get_client(request: pytest.FixtureRequest): |
| 18 |
mod = importlib.import_module( |
| 19 |
f"docs_src.query_params_str_validations.{request.param}" |
| 20 |
) |
| 21 |
|
| 22 |
client = TestClient(mod.app) |
| 23 |
return client |
| 24 |
|
| 25 |
|
| 26 |
def test_get_random_item(client: TestClient): |
| 27 |
response = client.get("/items") |
| 28 |
assert response.status_code == 200, response.text |
| 29 |
assert response.json() == {"id": IsStr(), "name": IsStr()} |
| 30 |
|
| 31 |
|
| 32 |
def test_get_item(client: TestClient): |
| 33 |
response = client.get("/items?id=isbn-9781529046137") |
| 34 |
assert response.status_code == 200, response.text |
| 35 |
assert response.json() == { |
| 36 |
"id": "isbn-9781529046137", |
| 37 |
"name": "The Hitchhiker's Guide to the Galaxy", |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
def test_get_item_does_not_exist(client: TestClient): |
| 42 |
response = client.get("/items?id=isbn-nope") |
| 43 |
assert response.status_code == 200, response.text |
| 44 |
assert response.json() == {"id": "isbn-nope", "name": None} |
| 45 |
|
| 46 |
|
| 47 |
def test_get_invalid_item(client: TestClient): |
| 48 |
response = client.get("/items?id=wtf-yes") |
| 49 |
assert response.status_code == 422, response.text |
| 50 |
assert response.json() == snapshot( |
| 51 |
{ |
| 52 |
"detail": [ |
| 53 |
{ |
| 54 |
"type": "value_error", |
| 55 |
"loc": ["query", "id"], |
| 56 |
"msg": 'Value error, Invalid ID format, it must start with "isbn-" or "imdb-"', |
| 57 |
"input": "wtf-yes", |
| 58 |
"ctx": {"error": {}}, |
| 59 |
} |
| 60 |
] |
| 61 |
} |
| 62 |
) |
| 63 |
|
| 64 |
|
| 65 |
def test_openapi_schema(client: TestClient): |
| 66 |
response = client.get("/openapi.json") |
| 67 |
assert response.status_code == 200, response.text |
| 68 |
assert response.json() == snapshot( |
| 69 |
{ |
| 70 |
"openapi": "3.1.0", |
| 71 |
"info": {"title": "FastAPI", "version": "0.1.0"}, |
| 72 |
"paths": { |
| 73 |
"/items/": { |
| 74 |
"get": { |
| 75 |
"summary": "Read Items", |
| 76 |
"operationId": "read_items_items__get", |
| 77 |
"parameters": [ |
| 78 |
{ |
| 79 |
"name": "id", |
| 80 |
"in": "query", |
| 81 |
"required": False, |
| 82 |
"schema": { |
| 83 |
"anyOf": [{"type": "string"}, {"type": "null"}], |
| 84 |
"title": "Id", |
| 85 |
}, |
| 86 |
} |
| 87 |
], |
| 88 |
"responses": { |
| 89 |
"200": { |
| 90 |
"description": "Successful Response", |
| 91 |
"content": {"application/json": {"schema": {}}}, |
| 92 |
}, |
| 93 |
"422": { |
| 94 |
"description": "Validation Error", |
| 95 |
"content": { |
| 96 |
"application/json": { |
| 97 |
"schema": { |
| 98 |
"$ref": "#/components/schemas/HTTPValidationError" |
| 99 |
} |
| 100 |
} |
| 101 |
}, |
| 102 |
}, |
| 103 |
}, |
| 104 |
} |
| 105 |
} |
| 106 |
}, |
| 107 |
"components": { |
| 108 |
"schemas": { |
| 109 |
"HTTPValidationError": { |
| 110 |
"properties": { |
| 111 |
"detail": { |
| 112 |
"items": { |
| 113 |
"$ref": "#/components/schemas/ValidationError" |
| 114 |
}, |
| 115 |
"type": "array", |
| 116 |
"title": "Detail", |
| 117 |
} |
| 118 |
}, |
| 119 |
"type": "object", |
| 120 |
"title": "HTTPValidationError", |
| 121 |
}, |
| 122 |
"ValidationError": { |
| 123 |
"properties": { |
| 124 |
"ctx": {"title": "Context", "type": "object"}, |
| 125 |
"input": {"title": "Input"}, |
| 126 |
"loc": { |
| 127 |
"items": { |
| 128 |
"anyOf": [{"type": "string"}, {"type": "integer"}] |
| 129 |
}, |
| 130 |
"type": "array", |
| 131 |
"title": "Location", |
| 132 |
}, |
| 133 |
"msg": {"type": "string", "title": "Message"}, |
| 134 |
"type": {"type": "string", "title": "Error Type"}, |
| 135 |
}, |
| 136 |
"type": "object", |
| 137 |
"required": ["loc", "msg", "type"], |
| 138 |
"title": "ValidationError", |
| 139 |
}, |
| 140 |
} |
| 141 |
}, |
| 142 |
} |
| 143 |
) |