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