Coverage for tests / test_compat.py: 100%
71 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-21 17:29 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-21 17:29 +0000
1from fastapi import FastAPI, UploadFile 1defg
2from fastapi._compat import ( 1defg
3 Undefined,
4 is_uploadfile_sequence_annotation,
5)
6from fastapi._compat.shared import is_bytes_sequence_annotation 1defg
7from fastapi.testclient import TestClient 1defg
8from pydantic import BaseModel, ConfigDict 1defg
9from pydantic.fields import FieldInfo 1defg
12def test_model_field_default_required(): 1defg
13 from fastapi._compat import v2 1tuv
15 # For coverage
16 field_info = FieldInfo(annotation=str) 1tuv
17 field = v2.ModelField(name="foo", field_info=field_info) 1tuv
18 assert field.default is Undefined 1tuv
21def test_complex(): 1defg
22 app = FastAPI() 1hij
24 @app.post("/") 1hij
25 def foo(foo: str | list[int]): 1hij
26 return foo 1hij
28 client = TestClient(app) 1hij
30 response = client.post("/", json="bar") 1hij
31 assert response.status_code == 200, response.text 1hij
32 assert response.json() == "bar" 1hij
34 response2 = client.post("/", json=[1, 2]) 1hij
35 assert response2.status_code == 200, response2.text 1hij
36 assert response2.json() == [1, 2] 1hij
39def test_propagates_pydantic2_model_config(): 1defg
40 app = FastAPI() 1abc
42 class Missing: 1abc
43 def __bool__(self): 1abc
44 return False 1abc
46 class EmbeddedModel(BaseModel): 1abc
47 model_config = ConfigDict(arbitrary_types_allowed=True) 1abc
48 value: str | Missing = Missing() 1abc
50 class Model(BaseModel): 1abc
51 model_config = ConfigDict( 1abc
52 arbitrary_types_allowed=True,
53 )
54 value: str | Missing = Missing() 1abc
55 embedded_model: EmbeddedModel = EmbeddedModel() 1abc
57 @app.post("/") 1abc
58 def foo(req: Model) -> dict[str, str | None]: 1abc
59 return { 1abc
60 "value": req.value or None,
61 "embedded_value": req.embedded_model.value or None,
62 }
64 client = TestClient(app) 1abc
66 response = client.post("/", json={}) 1abc
67 assert response.status_code == 200, response.text 1abc
68 assert response.json() == { 1abc
69 "value": None,
70 "embedded_value": None,
71 }
73 response2 = client.post( 1abc
74 "/", json={"value": "foo", "embedded_model": {"value": "bar"}}
75 )
76 assert response2.status_code == 200, response2.text 1abc
77 assert response2.json() == { 1abc
78 "value": "foo",
79 "embedded_value": "bar",
80 }
83def test_is_bytes_sequence_annotation_union(): 1defg
84 # For coverage
85 # TODO: in theory this would allow declaring types that could be lists of bytes
86 # to be read from files and other types, but I'm not even sure it's a good idea
87 # to support it as a first class "feature"
88 assert is_bytes_sequence_annotation(list[str] | list[bytes]) 1wxy
91def test_is_uploadfile_sequence_annotation(): 1defg
92 # For coverage
93 # TODO: in theory this would allow declaring types that could be lists of UploadFile
94 # and other types, but I'm not even sure it's a good idea to support it as a first
95 # class "feature"
96 assert is_uploadfile_sequence_annotation(list[str] | list[UploadFile]) 1zAB
99def test_serialize_sequence_value_with_optional_list(): 1defg
100 """Test that serialize_sequence_value handles optional lists correctly."""
101 from fastapi._compat import v2 1nop
103 field_info = FieldInfo(annotation=list[str] | None) 1nop
104 field = v2.ModelField(name="items", field_info=field_info) 1nop
105 result = v2.serialize_sequence_value(field=field, value=["a", "b", "c"]) 1nop
106 assert result == ["a", "b", "c"] 1nop
107 assert isinstance(result, list) 1nop
110def test_serialize_sequence_value_with_optional_list_pipe_union(): 1defg
111 """Test that serialize_sequence_value handles optional lists correctly (with new syntax)."""
112 from fastapi._compat import v2 1qrs
114 field_info = FieldInfo(annotation=list[str] | None) 1qrs
115 field = v2.ModelField(name="items", field_info=field_info) 1qrs
116 result = v2.serialize_sequence_value(field=field, value=["a", "b", "c"]) 1qrs
117 assert result == ["a", "b", "c"] 1qrs
118 assert isinstance(result, list) 1qrs
121def test_serialize_sequence_value_with_none_first_in_union(): 1defg
122 """Test that serialize_sequence_value handles Union[None, List[...]] correctly."""
123 from typing import Union 1klm
125 from fastapi._compat import v2 1klm
127 # Use Union[None, list[str]] to ensure None comes first in the union args
128 field_info = FieldInfo(annotation=Union[None, list[str]]) # noqa: UP007 1klm
129 field = v2.ModelField(name="items", field_info=field_info) 1klm
130 result = v2.serialize_sequence_value(field=field, value=["x", "y"]) 1klm
131 assert result == ["x", "y"] 1klm
132 assert isinstance(result, list) 1klm