Coverage for tests/test_serialize_response_dataclass.py: 100%

62 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-08 03:53 +0000

1from dataclasses import dataclass 1abcde

2from datetime import datetime 1abcde

3from typing import List, Optional 1abcde

4 

5from fastapi import FastAPI 1abcde

6from fastapi.testclient import TestClient 1abcde

7 

8app = FastAPI() 1abcde

9 

10 

11@dataclass 1abcde

12class Item: 1abcde

13 name: str 1abcde

14 date: datetime 1abcde

15 price: Optional[float] = None 1abcde

16 owner_ids: Optional[List[int]] = None 1abcde

17 

18 

19@app.get("/items/valid", response_model=Item) 1abcde

20def get_valid(): 1abcde

21 return {"name": "valid", "date": datetime(2021, 7, 26), "price": 1.0} 1abcde

22 

23 

24@app.get("/items/object", response_model=Item) 1abcde

25def get_object(): 1abcde

26 return Item( 1abcde

27 name="object", date=datetime(2021, 7, 26), price=1.0, owner_ids=[1, 2, 3] 

28 ) 

29 

30 

31@app.get("/items/coerce", response_model=Item) 1abcde

32def get_coerce(): 1abcde

33 return {"name": "coerce", "date": datetime(2021, 7, 26).isoformat(), "price": "1.0"} 1abcde

34 

35 

36@app.get("/items/validlist", response_model=List[Item]) 1abcde

37def get_validlist(): 1abcde

38 return [ 1abcde

39 {"name": "foo", "date": datetime(2021, 7, 26)}, 

40 {"name": "bar", "date": datetime(2021, 7, 26), "price": 1.0}, 

41 { 

42 "name": "baz", 

43 "date": datetime(2021, 7, 26), 

44 "price": 2.0, 

45 "owner_ids": [1, 2, 3], 

46 }, 

47 ] 

48 

49 

50@app.get("/items/objectlist", response_model=List[Item]) 1abcde

51def get_objectlist(): 1abcde

52 return [ 1abcde

53 Item(name="foo", date=datetime(2021, 7, 26)), 

54 Item(name="bar", date=datetime(2021, 7, 26), price=1.0), 

55 Item(name="baz", date=datetime(2021, 7, 26), price=2.0, owner_ids=[1, 2, 3]), 

56 ] 

57 

58 

59@app.get("/items/no-response-model/object") 1abcde

60def get_no_response_model_object(): 1abcde

61 return Item( 1abcde

62 name="object", date=datetime(2021, 7, 26), price=1.0, owner_ids=[1, 2, 3] 

63 ) 

64 

65 

66@app.get("/items/no-response-model/objectlist") 1abcde

67def get_no_response_model_objectlist(): 1abcde

68 return [ 1abcde

69 Item(name="foo", date=datetime(2021, 7, 26)), 

70 Item(name="bar", date=datetime(2021, 7, 26), price=1.0), 

71 Item(name="baz", date=datetime(2021, 7, 26), price=2.0, owner_ids=[1, 2, 3]), 

72 ] 

73 

74 

75client = TestClient(app) 1abcde

76 

77 

78def test_valid(): 1abcde

79 response = client.get("/items/valid") 1abcde

80 response.raise_for_status() 1abcde

81 assert response.json() == { 1abcde

82 "name": "valid", 

83 "date": datetime(2021, 7, 26).isoformat(), 

84 "price": 1.0, 

85 "owner_ids": None, 

86 } 

87 

88 

89def test_object(): 1abcde

90 response = client.get("/items/object") 1abcde

91 response.raise_for_status() 1abcde

92 assert response.json() == { 1abcde

93 "name": "object", 

94 "date": datetime(2021, 7, 26).isoformat(), 

95 "price": 1.0, 

96 "owner_ids": [1, 2, 3], 

97 } 

98 

99 

100def test_coerce(): 1abcde

101 response = client.get("/items/coerce") 1abcde

102 response.raise_for_status() 1abcde

103 assert response.json() == { 1abcde

104 "name": "coerce", 

105 "date": datetime(2021, 7, 26).isoformat(), 

106 "price": 1.0, 

107 "owner_ids": None, 

108 } 

109 

110 

111def test_validlist(): 1abcde

112 response = client.get("/items/validlist") 1abcde

113 response.raise_for_status() 1abcde

114 assert response.json() == [ 1abcde

115 { 

116 "name": "foo", 

117 "date": datetime(2021, 7, 26).isoformat(), 

118 "price": None, 

119 "owner_ids": None, 

120 }, 

121 { 

122 "name": "bar", 

123 "date": datetime(2021, 7, 26).isoformat(), 

124 "price": 1.0, 

125 "owner_ids": None, 

126 }, 

127 { 

128 "name": "baz", 

129 "date": datetime(2021, 7, 26).isoformat(), 

130 "price": 2.0, 

131 "owner_ids": [1, 2, 3], 

132 }, 

133 ] 

134 

135 

136def test_objectlist(): 1abcde

137 response = client.get("/items/objectlist") 1abcde

138 response.raise_for_status() 1abcde

139 assert response.json() == [ 1abcde

140 { 

141 "name": "foo", 

142 "date": datetime(2021, 7, 26).isoformat(), 

143 "price": None, 

144 "owner_ids": None, 

145 }, 

146 { 

147 "name": "bar", 

148 "date": datetime(2021, 7, 26).isoformat(), 

149 "price": 1.0, 

150 "owner_ids": None, 

151 }, 

152 { 

153 "name": "baz", 

154 "date": datetime(2021, 7, 26).isoformat(), 

155 "price": 2.0, 

156 "owner_ids": [1, 2, 3], 

157 }, 

158 ] 

159 

160 

161def test_no_response_model_object(): 1abcde

162 response = client.get("/items/no-response-model/object") 1abcde

163 response.raise_for_status() 1abcde

164 assert response.json() == { 1abcde

165 "name": "object", 

166 "date": datetime(2021, 7, 26).isoformat(), 

167 "price": 1.0, 

168 "owner_ids": [1, 2, 3], 

169 } 

170 

171 

172def test_no_response_model_objectlist(): 1abcde

173 response = client.get("/items/no-response-model/objectlist") 1abcde

174 response.raise_for_status() 1abcde

175 assert response.json() == [ 1abcde

176 { 

177 "name": "foo", 

178 "date": datetime(2021, 7, 26).isoformat(), 

179 "price": None, 

180 "owner_ids": None, 

181 }, 

182 { 

183 "name": "bar", 

184 "date": datetime(2021, 7, 26).isoformat(), 

185 "price": 1.0, 

186 "owner_ids": None, 

187 }, 

188 { 

189 "name": "baz", 

190 "date": datetime(2021, 7, 26).isoformat(), 

191 "price": 2.0, 

192 "owner_ids": [1, 2, 3], 

193 }, 

194 ]