Debian Codegraph @

tests/test_nested_annotated_in_sequence.py

raw
1 from typing import Annotated
2
3 from dirty_equals import IsList
4 from fastapi import FastAPI, Query
5 from fastapi.testclient import TestClient
6 from inline_snapshot import snapshot
7 from pydantic import Field
8
9 MaxSizedSet = Annotated[set[str], Field(max_length=3)]
10
11 app = FastAPI()
12
13
14 @app.get("/")
15 def read_root(foo: Annotated[MaxSizedSet | None, Query()] = None):
16 return {"foo": foo}
17
18
19 client = TestClient(app)
20
21
22 def test_endpoint_none():
23 response = client.get("/")
24 assert response.status_code == 200
25 assert response.json() == {"foo": None}
26
27
28 def test_endpoint_valid():
29 response = client.get("/", params={"foo": ["a", "b"]})
30 assert response.status_code == 200
31 assert response.json() == {"foo": IsList("a", "b", check_order=False)}
32
33
34 def test_endpoint_too_long():
35 response = client.get("/", params={"foo": ["a", "b", "c", "d"]})
36 assert response.status_code == 422
37 assert response.json() == snapshot(
38 {
39 "detail": [
40 {
41 "type": "too_long",
42 "loc": ["query", "foo"],
43 "msg": "Set should have at most 3 items after validation, not more",
44 "input": IsList("a", "b", "c", "d", check_order=False),
45 "ctx": {
46 "actual_length": None,
47 "field_type": "Set",
48 "max_length": 3,
49 },
50 }
51 ]
52 }
53 )
54
55
56 def test_openapi():
57 assert app.openapi() == snapshot(
58 {
59 "components": {
60 "schemas": {
61 "HTTPValidationError": {
62 "properties": {
63 "detail": {
64 "items": {
65 "$ref": "#/components/schemas/ValidationError"
66 },
67 "title": "Detail",
68 "type": "array",
69 },
70 },
71 "title": "HTTPValidationError",
72 "type": "object",
73 },
74 "ValidationError": {
75 "properties": {
76 "ctx": {"title": "Context", "type": "object"},
77 "input": {"title": "Input"},
78 "loc": {
79 "items": {
80 "anyOf": [{"type": "string"}, {"type": "integer"}],
81 },
82 "title": "Location",
83 "type": "array",
84 },
85 "msg": {"title": "Message", "type": "string"},
86 "type": {"title": "Error Type", "type": "string"},
87 },
88 "required": ["loc", "msg", "type"],
89 "title": "ValidationError",
90 "type": "object",
91 },
92 },
93 },
94 "info": {
95 "title": "FastAPI",
96 "version": "0.1.0",
97 },
98 "openapi": "3.1.0",
99 "paths": {
100 "/": {
101 "get": {
102 "operationId": "read_root__get",
103 "parameters": [
104 {
105 "in": "query",
106 "name": "foo",
107 "required": False,
108 "schema": {
109 "anyOf": [
110 {
111 "items": {"type": "string"},
112 "maxItems": 3,
113 "type": "array",
114 "uniqueItems": True,
115 },
116 {"type": "null"},
117 ],
118 "title": "Foo",
119 },
120 },
121 ],
122 "responses": {
123 "200": {
124 "content": {"application/json": {"schema": {}}},
125 "description": "Successful Response",
126 },
127 "422": {
128 "content": {
129 "application/json": {
130 "schema": {
131 "$ref": "#/components/schemas/HTTPValidationError",
132 },
133 },
134 },
135 "description": "Validation Error",
136 },
137 },
138 "summary": "Read Root",
139 },
140 },
141 },
142 }
143 )