Coverage for tests/test_additional_properties_bool.py: 100%
29 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
1from typing import Union 1abcde
3from dirty_equals import IsDict 1abcde
4from fastapi import FastAPI 1abcde
5from fastapi._compat import PYDANTIC_V2 1abcde
6from fastapi.testclient import TestClient 1abcde
7from pydantic import BaseModel, ConfigDict 1abcde
10class FooBaseModel(BaseModel): 1abcde
11 if PYDANTIC_V2: 1abcde
12 model_config = ConfigDict(extra="forbid") 1abcde
13 else:
15 class Config: 1abcde
16 extra = "forbid" 1abcde
19class Foo(FooBaseModel): 1abcde
20 pass 1abcde
23app = FastAPI() 1abcde
26@app.post("/") 1abcde
27async def post( 1abcde
28 foo: Union[Foo, None] = None,
29):
30 return foo 1abcde
33client = TestClient(app) 1abcde
36def test_call_invalid(): 1abcde
37 response = client.post("/", json={"foo": {"bar": "baz"}}) 1abcde
38 assert response.status_code == 422 1abcde
41def test_call_valid(): 1abcde
42 response = client.post("/", json={}) 1abcde
43 assert response.status_code == 200 1abcde
44 assert response.json() == {} 1abcde
47def test_openapi_schema(): 1abcde
48 response = client.get("/openapi.json") 1abcde
49 assert response.status_code == 200, response.text 1abcde
50 assert response.json() == { 1abcde
51 "openapi": "3.1.0",
52 "info": {"title": "FastAPI", "version": "0.1.0"},
53 "paths": {
54 "/": {
55 "post": {
56 "summary": "Post",
57 "operationId": "post__post",
58 "requestBody": {
59 "content": {
60 "application/json": {
61 "schema": IsDict(
62 {
63 "anyOf": [
64 {"$ref": "#/components/schemas/Foo"},
65 {"type": "null"},
66 ],
67 "title": "Foo",
68 }
69 )
70 | IsDict(
71 # TODO: remove when deprecating Pydantic v1
72 {"$ref": "#/components/schemas/Foo"}
73 )
74 }
75 }
76 },
77 "responses": {
78 "200": {
79 "description": "Successful Response",
80 "content": {"application/json": {"schema": {}}},
81 },
82 "422": {
83 "description": "Validation Error",
84 "content": {
85 "application/json": {
86 "schema": {
87 "$ref": "#/components/schemas/HTTPValidationError"
88 }
89 }
90 },
91 },
92 },
93 }
94 }
95 },
96 "components": {
97 "schemas": {
98 "Foo": {
99 "properties": {},
100 "additionalProperties": False,
101 "type": "object",
102 "title": "Foo",
103 },
104 "HTTPValidationError": {
105 "properties": {
106 "detail": {
107 "items": {"$ref": "#/components/schemas/ValidationError"},
108 "type": "array",
109 "title": "Detail",
110 }
111 },
112 "type": "object",
113 "title": "HTTPValidationError",
114 },
115 "ValidationError": {
116 "properties": {
117 "loc": {
118 "items": {
119 "anyOf": [{"type": "string"}, {"type": "integer"}]
120 },
121 "type": "array",
122 "title": "Location",
123 },
124 "msg": {"type": "string", "title": "Message"},
125 "type": {"type": "string", "title": "Error Type"},
126 },
127 "type": "object",
128 "required": ["loc", "msg", "type"],
129 "title": "ValidationError",
130 },
131 }
132 },
133 }