Coverage for tests/test_tutorial/test_query_params_str_validations/test_tutorial015.py: 100%
31 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-05 00:03 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-05 00:03 +0000
1import importlib 1abcdef
3import pytest 1abcdef
4from dirty_equals import IsStr 1abcdef
5from fastapi.testclient import TestClient 1abcdef
6from inline_snapshot import snapshot 1abcdef
8from ...utils import needs_py39, needs_py310, needs_pydanticv2 1abcdef
11@pytest.fixture( 1abcdef
12 name="client",
13 params=[
14 pytest.param("tutorial015_an", marks=needs_pydanticv2),
15 pytest.param("tutorial015_an_py310", marks=(needs_py310, needs_pydanticv2)),
16 pytest.param("tutorial015_an_py39", marks=(needs_py39, needs_pydanticv2)),
17 ],
18)
19def get_client(request: pytest.FixtureRequest): 1abcdef
20 mod = importlib.import_module( 1abcdef
21 f"docs_src.query_params_str_validations.{request.param}"
22 )
24 client = TestClient(mod.app) 1abcdef
25 return client 1abcdef
28def test_get_random_item(client: TestClient): 1abcdef
29 response = client.get("/items") 1ghijkl
30 assert response.status_code == 200, response.text 1ghijkl
31 assert response.json() == {"id": IsStr(), "name": IsStr()} 1ghijkl
34def test_get_item(client: TestClient): 1abcdef
35 response = client.get("/items?id=isbn-9781529046137") 1mnopqr
36 assert response.status_code == 200, response.text 1mnopqr
37 assert response.json() == { 1mnopqr
38 "id": "isbn-9781529046137",
39 "name": "The Hitchhiker's Guide to the Galaxy",
40 }
43def test_get_item_does_not_exist(client: TestClient): 1abcdef
44 response = client.get("/items?id=isbn-nope") 1stuvwx
45 assert response.status_code == 200, response.text 1stuvwx
46 assert response.json() == {"id": "isbn-nope", "name": None} 1stuvwx
49def test_get_invalid_item(client: TestClient): 1abcdef
50 response = client.get("/items?id=wtf-yes") 1yzABCD
51 assert response.status_code == 422, response.text 1yzABCD
52 assert response.json() == snapshot( 1yzABCD
53 {
54 "detail": [
55 {
56 "type": "value_error",
57 "loc": ["query", "id"],
58 "msg": 'Value error, Invalid ID format, it must start with "isbn-" or "imdb-"',
59 "input": "wtf-yes",
60 "ctx": {"error": {}},
61 }
62 ]
63 }
64 )
67def test_openapi_schema(client: TestClient): 1abcdef
68 response = client.get("/openapi.json") 1EFGHIJ
69 assert response.status_code == 200, response.text 1EFGHIJ
70 assert response.json() == snapshot( 1EFGHIJ
71 {
72 "openapi": "3.1.0",
73 "info": {"title": "FastAPI", "version": "0.1.0"},
74 "paths": {
75 "/items/": {
76 "get": {
77 "summary": "Read Items",
78 "operationId": "read_items_items__get",
79 "parameters": [
80 {
81 "name": "id",
82 "in": "query",
83 "required": False,
84 "schema": {
85 "anyOf": [{"type": "string"}, {"type": "null"}],
86 "title": "Id",
87 },
88 }
89 ],
90 "responses": {
91 "200": {
92 "description": "Successful Response",
93 "content": {"application/json": {"schema": {}}},
94 },
95 "422": {
96 "description": "Validation Error",
97 "content": {
98 "application/json": {
99 "schema": {
100 "$ref": "#/components/schemas/HTTPValidationError"
101 }
102 }
103 },
104 },
105 },
106 }
107 }
108 },
109 "components": {
110 "schemas": {
111 "HTTPValidationError": {
112 "properties": {
113 "detail": {
114 "items": {
115 "$ref": "#/components/schemas/ValidationError"
116 },
117 "type": "array",
118 "title": "Detail",
119 }
120 },
121 "type": "object",
122 "title": "HTTPValidationError",
123 },
124 "ValidationError": {
125 "properties": {
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 )