Coverage for tests / test_serialize_response_model.py: 100%

66 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1from typing import Optional 1abcd

2 

3from fastapi import FastAPI 1abcd

4from pydantic import BaseModel, Field 1abcd

5from starlette.testclient import TestClient 1abcd

6 

7app = FastAPI() 1abcd

8 

9 

10class Item(BaseModel): 1abcd

11 name: str = Field(alias="aliased_name") 1abcd

12 price: Optional[float] = None 1abcd

13 owner_ids: Optional[list[int]] = None 1abcd

14 

15 

16@app.get("/items/valid", response_model=Item) 1abcd

17def get_valid(): 1abcd

18 return Item(aliased_name="valid", price=1.0) 1efg

19 

20 

21@app.get("/items/coerce", response_model=Item) 1abcd

22def get_coerce(): 1abcd

23 return Item(aliased_name="coerce", price="1.0") 1hij

24 

25 

26@app.get("/items/validlist", response_model=list[Item]) 1abcd

27def get_validlist(): 1abcd

28 return [ 1klm

29 Item(aliased_name="foo"), 

30 Item(aliased_name="bar", price=1.0), 

31 Item(aliased_name="baz", price=2.0, owner_ids=[1, 2, 3]), 

32 ] 

33 

34 

35@app.get("/items/validdict", response_model=dict[str, Item]) 1abcd

36def get_validdict(): 1abcd

37 return { 1nop

38 "k1": Item(aliased_name="foo"), 

39 "k2": Item(aliased_name="bar", price=1.0), 

40 "k3": Item(aliased_name="baz", price=2.0, owner_ids=[1, 2, 3]), 

41 } 

42 

43 

44@app.get( 1abcd

45 "/items/valid-exclude-unset", response_model=Item, response_model_exclude_unset=True 

46) 

47def get_valid_exclude_unset(): 1abcd

48 return Item(aliased_name="valid", price=1.0) 1qrs

49 

50 

51@app.get( 1abcd

52 "/items/coerce-exclude-unset", 

53 response_model=Item, 

54 response_model_exclude_unset=True, 

55) 

56def get_coerce_exclude_unset(): 1abcd

57 return Item(aliased_name="coerce", price="1.0") 1tuv

58 

59 

60@app.get( 1abcd

61 "/items/validlist-exclude-unset", 

62 response_model=list[Item], 

63 response_model_exclude_unset=True, 

64) 

65def get_validlist_exclude_unset(): 1abcd

66 return [ 1wxy

67 Item(aliased_name="foo"), 

68 Item(aliased_name="bar", price=1.0), 

69 Item(aliased_name="baz", price=2.0, owner_ids=[1, 2, 3]), 

70 ] 

71 

72 

73@app.get( 1abcd

74 "/items/validdict-exclude-unset", 

75 response_model=dict[str, Item], 

76 response_model_exclude_unset=True, 

77) 

78def get_validdict_exclude_unset(): 1abcd

79 return { 1zAB

80 "k1": Item(aliased_name="foo"), 

81 "k2": Item(aliased_name="bar", price=1.0), 

82 "k3": Item(aliased_name="baz", price=2.0, owner_ids=[1, 2, 3]), 

83 } 

84 

85 

86client = TestClient(app) 1abcd

87 

88 

89def test_valid(): 1abcd

90 response = client.get("/items/valid") 1efg

91 response.raise_for_status() 1efg

92 assert response.json() == {"aliased_name": "valid", "price": 1.0, "owner_ids": None} 1efg

93 

94 

95def test_coerce(): 1abcd

96 response = client.get("/items/coerce") 1hij

97 response.raise_for_status() 1hij

98 assert response.json() == { 1hij

99 "aliased_name": "coerce", 

100 "price": 1.0, 

101 "owner_ids": None, 

102 } 

103 

104 

105def test_validlist(): 1abcd

106 response = client.get("/items/validlist") 1klm

107 response.raise_for_status() 1klm

108 assert response.json() == [ 1klm

109 {"aliased_name": "foo", "price": None, "owner_ids": None}, 

110 {"aliased_name": "bar", "price": 1.0, "owner_ids": None}, 

111 {"aliased_name": "baz", "price": 2.0, "owner_ids": [1, 2, 3]}, 

112 ] 

113 

114 

115def test_validdict(): 1abcd

116 response = client.get("/items/validdict") 1nop

117 response.raise_for_status() 1nop

118 assert response.json() == { 1nop

119 "k1": {"aliased_name": "foo", "price": None, "owner_ids": None}, 

120 "k2": {"aliased_name": "bar", "price": 1.0, "owner_ids": None}, 

121 "k3": {"aliased_name": "baz", "price": 2.0, "owner_ids": [1, 2, 3]}, 

122 } 

123 

124 

125def test_valid_exclude_unset(): 1abcd

126 response = client.get("/items/valid-exclude-unset") 1qrs

127 response.raise_for_status() 1qrs

128 assert response.json() == {"aliased_name": "valid", "price": 1.0} 1qrs

129 

130 

131def test_coerce_exclude_unset(): 1abcd

132 response = client.get("/items/coerce-exclude-unset") 1tuv

133 response.raise_for_status() 1tuv

134 assert response.json() == {"aliased_name": "coerce", "price": 1.0} 1tuv

135 

136 

137def test_validlist_exclude_unset(): 1abcd

138 response = client.get("/items/validlist-exclude-unset") 1wxy

139 response.raise_for_status() 1wxy

140 assert response.json() == [ 1wxy

141 {"aliased_name": "foo"}, 

142 {"aliased_name": "bar", "price": 1.0}, 

143 {"aliased_name": "baz", "price": 2.0, "owner_ids": [1, 2, 3]}, 

144 ] 

145 

146 

147def test_validdict_exclude_unset(): 1abcd

148 response = client.get("/items/validdict-exclude-unset") 1zAB

149 response.raise_for_status() 1zAB

150 assert response.json() == { 1zAB

151 "k1": {"aliased_name": "foo"}, 

152 "k2": {"aliased_name": "bar", "price": 1.0}, 

153 "k3": {"aliased_name": "baz", "price": 2.0, "owner_ids": [1, 2, 3]}, 

154 }