Coverage for tests / test_tutorial / test_query_params_str_validations / test_tutorial015.py: 100%
31 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
1import importlib 1abdc
3import pytest 1abdc
4from dirty_equals import IsStr 1abdc
5from fastapi.testclient import TestClient 1abdc
6from inline_snapshot import snapshot 1abdc
8from ...utils import needs_py310 1abdc
11@pytest.fixture( 1abdc
12 name="client",
13 params=[
14 pytest.param("tutorial015_an_py310", marks=[needs_py310]),
15 ],
16)
17def get_client(request: pytest.FixtureRequest): 1abdc
18 mod = importlib.import_module( 1abc
19 f"docs_src.query_params_str_validations.{request.param}"
20 )
22 client = TestClient(mod.app) 1abc
23 return client 1abc
26def test_get_random_item(client: TestClient): 1abdc
27 response = client.get("/items") 1efg
28 assert response.status_code == 200, response.text 1efg
29 assert response.json() == {"id": IsStr(), "name": IsStr()} 1efg
32def test_get_item(client: TestClient): 1abdc
33 response = client.get("/items?id=isbn-9781529046137") 1hij
34 assert response.status_code == 200, response.text 1hij
35 assert response.json() == { 1hij
36 "id": "isbn-9781529046137",
37 "name": "The Hitchhiker's Guide to the Galaxy",
38 }
41def test_get_item_does_not_exist(client: TestClient): 1abdc
42 response = client.get("/items?id=isbn-nope") 1klm
43 assert response.status_code == 200, response.text 1klm
44 assert response.json() == {"id": "isbn-nope", "name": None} 1klm
47def test_get_invalid_item(client: TestClient): 1abdc
48 response = client.get("/items?id=wtf-yes") 1nop
49 assert response.status_code == 422, response.text 1nop
50 assert response.json() == snapshot( 1nop
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 )
65def test_openapi_schema(client: TestClient): 1abdc
66 response = client.get("/openapi.json") 1qrs
67 assert response.status_code == 200, response.text 1qrs
68 assert response.json() == snapshot( 1qrs
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 )