Coverage for tests/test_regex_deprecated_params.py: 100%
40 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 import FastAPI, Query 1deabc
4from fastapi.testclient import TestClient 1deabc
5from typing_extensions import Annotated 1deabc
7from .utils import needs_py310 1deabc
10def get_client(): 1deabc
11 app = FastAPI() 1abc
12 with pytest.warns(DeprecationWarning): 1abc
14 @app.get("/items/") 1abc
15 async def read_items( 1abc
16 q: Annotated[str | None, Query(regex="^fixedquery$")] = None,
17 ):
18 if q: 1abc
19 return f"Hello {q}" 1abc
20 else:
21 return "Hello World" 1abc
23 client = TestClient(app) 1abc
24 return client 1abc
27@needs_py310 1deabc
28def test_query_params_str_validations_no_query(): 1deabc
29 client = get_client() 1abc
30 response = client.get("/items/") 1abc
31 assert response.status_code == 200 1abc
32 assert response.json() == "Hello World" 1abc
35@needs_py310 1deabc
36def test_query_params_str_validations_q_fixedquery(): 1deabc
37 client = get_client() 1abc
38 response = client.get("/items/", params={"q": "fixedquery"}) 1abc
39 assert response.status_code == 200 1abc
40 assert response.json() == "Hello fixedquery" 1abc
43@needs_py310 1deabc
44def test_query_params_str_validations_item_query_nonregexquery(): 1deabc
45 client = get_client() 1abc
46 response = client.get("/items/", params={"q": "nonregexquery"}) 1abc
47 assert response.status_code == 422 1abc
48 assert response.json() == IsDict( 1abc
49 {
50 "detail": [
51 {
52 "type": "string_pattern_mismatch",
53 "loc": ["query", "q"],
54 "msg": "String should match pattern '^fixedquery$'",
55 "input": "nonregexquery",
56 "ctx": {"pattern": "^fixedquery$"},
57 }
58 ]
59 }
60 ) | IsDict(
61 # TODO: remove when deprecating Pydantic v1
62 {
63 "detail": [
64 {
65 "ctx": {"pattern": "^fixedquery$"},
66 "loc": ["query", "q"],
67 "msg": 'string does not match regex "^fixedquery$"',
68 "type": "value_error.str.regex",
69 }
70 ]
71 }
72 )
75@needs_py310 1deabc
76def test_openapi_schema(): 1deabc
77 client = get_client() 1abc
78 response = client.get("/openapi.json") 1abc
79 assert response.status_code == 200, response.text 1abc
80 # insert_assert(response.json())
81 assert response.json() == { 1abc
82 "openapi": "3.1.0",
83 "info": {"title": "FastAPI", "version": "0.1.0"},
84 "paths": {
85 "/items/": {
86 "get": {
87 "summary": "Read Items",
88 "operationId": "read_items_items__get",
89 "parameters": [
90 {
91 "name": "q",
92 "in": "query",
93 "required": False,
94 "schema": IsDict(
95 {
96 "anyOf": [
97 {"type": "string", "pattern": "^fixedquery$"},
98 {"type": "null"},
99 ],
100 "title": "Q",
101 }
102 )
103 | IsDict(
104 # TODO: remove when deprecating Pydantic v1
105 {
106 "type": "string",
107 "pattern": "^fixedquery$",
108 "title": "Q",
109 }
110 ),
111 }
112 ],
113 "responses": {
114 "200": {
115 "description": "Successful Response",
116 "content": {"application/json": {"schema": {}}},
117 },
118 "422": {
119 "description": "Validation Error",
120 "content": {
121 "application/json": {
122 "schema": {
123 "$ref": "#/components/schemas/HTTPValidationError"
124 }
125 }
126 },
127 },
128 },
129 }
130 }
131 },
132 "components": {
133 "schemas": {
134 "HTTPValidationError": {
135 "properties": {
136 "detail": {
137 "items": {"$ref": "#/components/schemas/ValidationError"},
138 "type": "array",
139 "title": "Detail",
140 }
141 },
142 "type": "object",
143 "title": "HTTPValidationError",
144 },
145 "ValidationError": {
146 "properties": {
147 "loc": {
148 "items": {
149 "anyOf": [{"type": "string"}, {"type": "integer"}]
150 },
151 "type": "array",
152 "title": "Location",
153 },
154 "msg": {"type": "string", "title": "Message"},
155 "type": {"type": "string", "title": "Error Type"},
156 },
157 "type": "object",
158 "required": ["loc", "msg", "type"],
159 "title": "ValidationError",
160 },
161 }
162 },
163 }