Coverage for tests/main.py: 100%

125 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-12-04 08:29 +0000

1import http 1abcdefg

2from typing import FrozenSet, List, Optional 1abcdefg

3 

4from fastapi import FastAPI, Path, Query 1abcdefg

5 

6external_docs = { 1abcdefg

7 "description": "External API documentation.", 

8 "url": "https://docs.example.com/api-general", 

9} 

10 

11app = FastAPI(openapi_external_docs=external_docs) 1abcdefg

12 

13 

14@app.api_route("/api_route") 1abcdefg

15def non_operation(): 1abcdefg

16 return {"message": "Hello World"} 1hijklmn

17 

18 

19def non_decorated_route(): 1abcdefg

20 return {"message": "Hello World"} 1hijklmn

21 

22 

23app.add_api_route("/non_decorated_route", non_decorated_route) 1abcdefg

24 

25 

26@app.get("/text") 1abcdefg

27def get_text(): 1abcdefg

28 return "Hello World" 1#$%'()*

29 

30 

31@app.get("/path/{item_id}") 1abcdefg

32def get_id(item_id): 1abcdefg

33 return item_id 1+,-./:;

34 

35 

36@app.get("/path/str/{item_id}") 1abcdefg

37def get_str_id(item_id: str): 1abcdefg

38 return item_id 2= ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbib

39 

40 

41@app.get("/path/int/{item_id}") 1abcdefg

42def get_int_id(item_id: int): 1abcdefg

43 return item_id 2jbkblbmbnbobpb

44 

45 

46@app.get("/path/float/{item_id}") 1abcdefg

47def get_float_id(item_id: float): 1abcdefg

48 return item_id 2qbrbsbtbubvbwbxbybzbAbBbCbDb

49 

50 

51@app.get("/path/bool/{item_id}") 1abcdefg

52def get_bool_id(item_id: bool): 1abcdefg

53 return item_id 2EbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+b,b

54 

55 

56@app.get("/path/param/{item_id}") 1abcdefg

57def get_path_param_id(item_id: Optional[str] = Path()): 1abcdefg

58 return item_id 2-b.b/b:b;b=b?b

59 

60 

61@app.get("/path/param-minlength/{item_id}") 1abcdefg

62def get_path_param_min_length(item_id: str = Path(min_length=3)): 1abcdefg

63 return item_id 2@b[b]b^b_b`b{b

64 

65 

66@app.get("/path/param-maxlength/{item_id}") 1abcdefg

67def get_path_param_max_length(item_id: str = Path(max_length=3)): 1abcdefg

68 return item_id 2|b}b~bacbcccdc

69 

70 

71@app.get("/path/param-min_maxlength/{item_id}") 1abcdefg

72def get_path_param_min_max_length(item_id: str = Path(max_length=3, min_length=2)): 1abcdefg

73 return item_id 2ecfcgchcicjckc

74 

75 

76@app.get("/path/param-gt/{item_id}") 1abcdefg

77def get_path_param_gt(item_id: float = Path(gt=3)): 1abcdefg

78 return item_id 2lcmcncocpcqcrc

79 

80 

81@app.get("/path/param-gt0/{item_id}") 1abcdefg

82def get_path_param_gt0(item_id: float = Path(gt=0)): 1abcdefg

83 return item_id 2sctcucvcwcxcyc

84 

85 

86@app.get("/path/param-ge/{item_id}") 1abcdefg

87def get_path_param_ge(item_id: float = Path(ge=3)): 1abcdefg

88 return item_id 2zcAcBcCcDcEcFcGcHcIcJcKcLcMc

89 

90 

91@app.get("/path/param-lt/{item_id}") 1abcdefg

92def get_path_param_lt(item_id: float = Path(lt=3)): 1abcdefg

93 return item_id 2NcOcPcQcRcScTc

94 

95 

96@app.get("/path/param-lt0/{item_id}") 1abcdefg

97def get_path_param_lt0(item_id: float = Path(lt=0)): 1abcdefg

98 return item_id 2UcVcWcXcYcZc0c

99 

100 

101@app.get("/path/param-le/{item_id}") 1abcdefg

102def get_path_param_le(item_id: float = Path(le=3)): 1abcdefg

103 return item_id 21c2c3c4c5c6c7c8c9c!c#c$c%c'c

104 

105 

106@app.get("/path/param-lt-gt/{item_id}") 1abcdefg

107def get_path_param_lt_gt(item_id: float = Path(lt=3, gt=1)): 1abcdefg

108 return item_id 2(c)c*c+c,c-c.c

109 

110 

111@app.get("/path/param-le-ge/{item_id}") 1abcdefg

112def get_path_param_le_ge(item_id: float = Path(le=3, ge=1)): 1abcdefg

113 return item_id 2/c:c;c=c?c@c[c]c^c_c`c{c|c}c~cadbdcdddedfd

114 

115 

116@app.get("/path/param-lt-int/{item_id}") 1abcdefg

117def get_path_param_lt_int(item_id: int = Path(lt=3)): 1abcdefg

118 return item_id 2gdhdidjdkdldmd

119 

120 

121@app.get("/path/param-gt-int/{item_id}") 1abcdefg

122def get_path_param_gt_int(item_id: int = Path(gt=3)): 1abcdefg

123 return item_id 2ndodpdqdrdsdtd

124 

125 

126@app.get("/path/param-le-int/{item_id}") 1abcdefg

127def get_path_param_le_int(item_id: int = Path(le=3)): 1abcdefg

128 return item_id 2udvdwdxdydzdAdBdCdDdEdFdGdHd

129 

130 

131@app.get("/path/param-ge-int/{item_id}") 1abcdefg

132def get_path_param_ge_int(item_id: int = Path(ge=3)): 1abcdefg

133 return item_id 2IdJdKdLdMdNdOdPdQdRdSdTdUdVd

134 

135 

136@app.get("/path/param-lt-gt-int/{item_id}") 1abcdefg

137def get_path_param_lt_gt_int(item_id: int = Path(lt=3, gt=1)): 1abcdefg

138 return item_id 2WdXdYdZd0d1d2d

139 

140 

141@app.get("/path/param-le-ge-int/{item_id}") 1abcdefg

142def get_path_param_le_ge_int(item_id: int = Path(le=3, ge=1)): 1abcdefg

143 return item_id 23d4d5d6d7d8d9d!d#d$d%d'd(d)d*d+d,d-d.d/d:d

144 

145 

146@app.get("/query") 1abcdefg

147def get_query(query): 1abcdefg

148 return f"foo bar {query}" 2;d=d?d@d[d]d^d

149 

150 

151@app.get("/query/optional") 1abcdefg

152def get_query_optional(query=None): 1abcdefg

153 if query is None: 1opqrstuvwxyzABCDEFGHI

154 return "foo bar" 1oprsuvxyABDEGH

155 return f"foo bar {query}" 1qtwzCFI

156 

157 

158@app.get("/query/int") 1abcdefg

159def get_query_type(query: int): 1abcdefg

160 return f"foo bar {query}" 2_d`d{d|d}d~dae

161 

162 

163@app.get("/query/int/optional") 1abcdefg

164def get_query_type_optional(query: Optional[int] = None): 1abcdefg

165 if query is None: 1JKLMNOPQRSTUVW

166 return "foo bar" 1JLNPRTV

167 return f"foo bar {query}" 1KMOQSUW

168 

169 

170@app.get("/query/int/default") 1abcdefg

171def get_query_type_int_default(query: int = 10): 1abcdefg

172 return f"foo bar {query}" 2becedeeefegeheiejekelemeneoe

173 

174 

175@app.get("/query/param") 1abcdefg

176def get_query_param(query=Query(default=None)): 1abcdefg

177 if query is None: 1XYZ0123456789!

178 return "foo bar" 1XZ13579

179 return f"foo bar {query}" 1Y02468!

180 

181 

182@app.get("/query/param-required") 1abcdefg

183def get_query_param_required(query=Query()): 1abcdefg

184 return f"foo bar {query}" 2peqereseteueve

185 

186 

187@app.get("/query/param-required/int") 1abcdefg

188def get_query_param_required_type(query: int = Query()): 1abcdefg

189 return f"foo bar {query}" 2wexeyezeAeBeCe

190 

191 

192@app.get("/enum-status-code", status_code=http.HTTPStatus.CREATED) 1abcdefg

193def get_enum_status_code(): 1abcdefg

194 return "foo bar" 2DeEeFeGeHeIeJe

195 

196 

197@app.get("/query/frozenset") 1abcdefg

198def get_query_type_frozenset(query: FrozenSet[int] = Query(...)): 1abcdefg

199 return ",".join(map(str, sorted(query))) 2KeLeMeNeOePeQe

200 

201 

202@app.get("/query/list") 1abcdefg

203def get_query_list(device_ids: List[int] = Query()) -> List[int]: 1abcdefg

204 return device_ids 2ReSeTeUeVeWeXe

205 

206 

207@app.get("/query/list-default") 1abcdefg

208def get_query_list_default(device_ids: List[int] = Query(default=[])) -> List[int]: 1abcdefg

209 return device_ids 2YeZe0e1e2e3e4e5e6e7e8e9e!e#e