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