Coverage for tests/test_tutorial/test_query_params_str_validations/test_tutorial010_an_py310.py: 100%
34 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
1import pytest 1deabc
2from dirty_equals import IsDict 1deabc
3from fastapi.testclient import TestClient 1deabc
5from ...utils import needs_py310 1deabc
8@pytest.fixture(name="client") 1deabc
9def get_client(): 1deabc
10 from docs_src.query_params_str_validations.tutorial010_an_py310 import app 1abc
12 client = TestClient(app) 1abc
13 return client 1abc
16@needs_py310 1deabc
17def test_query_params_str_validations_no_query(client: TestClient): 1deabc
18 response = client.get("/items/") 1abc
19 assert response.status_code == 200 1abc
20 assert response.json() == {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} 1abc
23@needs_py310 1deabc
24def test_query_params_str_validations_item_query_fixedquery(client: TestClient): 1deabc
25 response = client.get("/items/", params={"item-query": "fixedquery"}) 1abc
26 assert response.status_code == 200 1abc
27 assert response.json() == { 1abc
28 "items": [{"item_id": "Foo"}, {"item_id": "Bar"}],
29 "q": "fixedquery",
30 }
33@needs_py310 1deabc
34def test_query_params_str_validations_q_fixedquery(client: TestClient): 1deabc
35 response = client.get("/items/", params={"q": "fixedquery"}) 1abc
36 assert response.status_code == 200 1abc
37 assert response.json() == {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} 1abc
40@needs_py310 1deabc
41def test_query_params_str_validations_item_query_nonregexquery(client: TestClient): 1deabc
42 response = client.get("/items/", params={"item-query": "nonregexquery"}) 1abc
43 assert response.status_code == 422 1abc
44 assert response.json() == IsDict( 1abc
45 {
46 "detail": [
47 {
48 "type": "string_pattern_mismatch",
49 "loc": ["query", "item-query"],
50 "msg": "String should match pattern '^fixedquery$'",
51 "input": "nonregexquery",
52 "ctx": {"pattern": "^fixedquery$"},
53 }
54 ]
55 }
56 ) | IsDict(
57 # TODO: remove when deprecating Pydantic v1
58 {
59 "detail": [
60 {
61 "ctx": {"pattern": "^fixedquery$"},
62 "loc": ["query", "item-query"],
63 "msg": 'string does not match regex "^fixedquery$"',
64 "type": "value_error.str.regex",
65 }
66 ]
67 }
68 )
71@needs_py310 1deabc
72def test_openapi_schema(client: TestClient): 1deabc
73 response = client.get("/openapi.json") 1abc
74 assert response.status_code == 200, response.text 1abc
75 assert response.json() == { 1abc
76 "openapi": "3.1.0",
77 "info": {"title": "FastAPI", "version": "0.1.0"},
78 "paths": {
79 "/items/": {
80 "get": {
81 "responses": {
82 "200": {
83 "description": "Successful Response",
84 "content": {"application/json": {"schema": {}}},
85 },
86 "422": {
87 "description": "Validation Error",
88 "content": {
89 "application/json": {
90 "schema": {
91 "$ref": "#/components/schemas/HTTPValidationError"
92 }
93 }
94 },
95 },
96 },
97 "summary": "Read Items",
98 "operationId": "read_items_items__get",
99 "parameters": [
100 {
101 "description": "Query string for the items to search in the database that have a good match",
102 "required": False,
103 "deprecated": True,
104 "schema": IsDict(
105 {
106 "anyOf": [
107 {
108 "type": "string",
109 "minLength": 3,
110 "maxLength": 50,
111 "pattern": "^fixedquery$",
112 },
113 {"type": "null"},
114 ],
115 "title": "Query string",
116 "description": "Query string for the items to search in the database that have a good match",
117 }
118 )
119 | IsDict(
120 # TODO: remove when deprecating Pydantic v1
121 {
122 "title": "Query string",
123 "maxLength": 50,
124 "minLength": 3,
125 "pattern": "^fixedquery$",
126 "type": "string",
127 "description": "Query string for the items to search in the database that have a good match",
128 }
129 ),
130 "name": "item-query",
131 "in": "query",
132 }
133 ],
134 }
135 }
136 },
137 "components": {
138 "schemas": {
139 "ValidationError": {
140 "title": "ValidationError",
141 "required": ["loc", "msg", "type"],
142 "type": "object",
143 "properties": {
144 "loc": {
145 "title": "Location",
146 "type": "array",
147 "items": {
148 "anyOf": [{"type": "string"}, {"type": "integer"}]
149 },
150 },
151 "msg": {"title": "Message", "type": "string"},
152 "type": {"title": "Error Type", "type": "string"},
153 },
154 },
155 "HTTPValidationError": {
156 "title": "HTTPValidationError",
157 "type": "object",
158 "properties": {
159 "detail": {
160 "title": "Detail",
161 "type": "array",
162 "items": {"$ref": "#/components/schemas/ValidationError"},
163 }
164 },
165 },
166 }
167 },
168 }