Coverage for tests/test_query.py: 100%

119 statements  

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

1from dirty_equals import IsDict 1abcde

2from fastapi.testclient import TestClient 1abcde

3 

4from .main import app 1abcde

5 

6client = TestClient(app) 1abcde

7 

8 

9def test_query(): 1abcde

10 response = client.get("/query") 1abcde

11 assert response.status_code == 422 1abcde

12 assert response.json() == IsDict( 1abcde

13 { 

14 "detail": [ 

15 { 

16 "type": "missing", 

17 "loc": ["query", "query"], 

18 "msg": "Field required", 

19 "input": None, 

20 } 

21 ] 

22 } 

23 ) | IsDict( 

24 # TODO: remove when deprecating Pydantic v1 

25 { 

26 "detail": [ 

27 { 

28 "loc": ["query", "query"], 

29 "msg": "field required", 

30 "type": "value_error.missing", 

31 } 

32 ] 

33 } 

34 ) 

35 

36 

37def test_query_query_baz(): 1abcde

38 response = client.get("/query?query=baz") 1abcde

39 assert response.status_code == 200 1abcde

40 assert response.json() == "foo bar baz" 1abcde

41 

42 

43def test_query_not_declared_baz(): 1abcde

44 response = client.get("/query?not_declared=baz") 1abcde

45 assert response.status_code == 422 1abcde

46 assert response.json() == IsDict( 1abcde

47 { 

48 "detail": [ 

49 { 

50 "type": "missing", 

51 "loc": ["query", "query"], 

52 "msg": "Field required", 

53 "input": None, 

54 } 

55 ] 

56 } 

57 ) | IsDict( 

58 # TODO: remove when deprecating Pydantic v1 

59 { 

60 "detail": [ 

61 { 

62 "loc": ["query", "query"], 

63 "msg": "field required", 

64 "type": "value_error.missing", 

65 } 

66 ] 

67 } 

68 ) 

69 

70 

71def test_query_optional(): 1abcde

72 response = client.get("/query/optional") 1abcde

73 assert response.status_code == 200 1abcde

74 assert response.json() == "foo bar" 1abcde

75 

76 

77def test_query_optional_query_baz(): 1abcde

78 response = client.get("/query/optional?query=baz") 1abcde

79 assert response.status_code == 200 1abcde

80 assert response.json() == "foo bar baz" 1abcde

81 

82 

83def test_query_optional_not_declared_baz(): 1abcde

84 response = client.get("/query/optional?not_declared=baz") 1abcde

85 assert response.status_code == 200 1abcde

86 assert response.json() == "foo bar" 1abcde

87 

88 

89def test_query_int(): 1abcde

90 response = client.get("/query/int") 1abcde

91 assert response.status_code == 422 1abcde

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

93 { 

94 "detail": [ 

95 { 

96 "type": "missing", 

97 "loc": ["query", "query"], 

98 "msg": "Field required", 

99 "input": None, 

100 } 

101 ] 

102 } 

103 ) | IsDict( 

104 # TODO: remove when deprecating Pydantic v1 

105 { 

106 "detail": [ 

107 { 

108 "loc": ["query", "query"], 

109 "msg": "field required", 

110 "type": "value_error.missing", 

111 } 

112 ] 

113 } 

114 ) 

115 

116 

117def test_query_int_query_42(): 1abcde

118 response = client.get("/query/int?query=42") 1abcde

119 assert response.status_code == 200 1abcde

120 assert response.json() == "foo bar 42" 1abcde

121 

122 

123def test_query_int_query_42_5(): 1abcde

124 response = client.get("/query/int?query=42.5") 1abcde

125 assert response.status_code == 422 1abcde

126 assert response.json() == IsDict( 1abcde

127 { 

128 "detail": [ 

129 { 

130 "type": "int_parsing", 

131 "loc": ["query", "query"], 

132 "msg": "Input should be a valid integer, unable to parse string as an integer", 

133 "input": "42.5", 

134 } 

135 ] 

136 } 

137 ) | IsDict( 

138 # TODO: remove when deprecating Pydantic v1 

139 { 

140 "detail": [ 

141 { 

142 "loc": ["query", "query"], 

143 "msg": "value is not a valid integer", 

144 "type": "type_error.integer", 

145 } 

146 ] 

147 } 

148 ) 

149 

150 

151def test_query_int_query_baz(): 1abcde

152 response = client.get("/query/int?query=baz") 1abcde

153 assert response.status_code == 422 1abcde

154 assert response.json() == IsDict( 1abcde

155 { 

156 "detail": [ 

157 { 

158 "type": "int_parsing", 

159 "loc": ["query", "query"], 

160 "msg": "Input should be a valid integer, unable to parse string as an integer", 

161 "input": "baz", 

162 } 

163 ] 

164 } 

165 ) | IsDict( 

166 # TODO: remove when deprecating Pydantic v1 

167 { 

168 "detail": [ 

169 { 

170 "loc": ["query", "query"], 

171 "msg": "value is not a valid integer", 

172 "type": "type_error.integer", 

173 } 

174 ] 

175 } 

176 ) 

177 

178 

179def test_query_int_not_declared_baz(): 1abcde

180 response = client.get("/query/int?not_declared=baz") 1abcde

181 assert response.status_code == 422 1abcde

182 assert response.json() == IsDict( 1abcde

183 { 

184 "detail": [ 

185 { 

186 "type": "missing", 

187 "loc": ["query", "query"], 

188 "msg": "Field required", 

189 "input": None, 

190 } 

191 ] 

192 } 

193 ) | IsDict( 

194 # TODO: remove when deprecating Pydantic v1 

195 { 

196 "detail": [ 

197 { 

198 "loc": ["query", "query"], 

199 "msg": "field required", 

200 "type": "value_error.missing", 

201 } 

202 ] 

203 } 

204 ) 

205 

206 

207def test_query_int_optional(): 1abcde

208 response = client.get("/query/int/optional") 1abcde

209 assert response.status_code == 200 1abcde

210 assert response.json() == "foo bar" 1abcde

211 

212 

213def test_query_int_optional_query_50(): 1abcde

214 response = client.get("/query/int/optional?query=50") 1abcde

215 assert response.status_code == 200 1abcde

216 assert response.json() == "foo bar 50" 1abcde

217 

218 

219def test_query_int_optional_query_foo(): 1abcde

220 response = client.get("/query/int/optional?query=foo") 1abcde

221 assert response.status_code == 422 1abcde

222 assert response.json() == IsDict( 1abcde

223 { 

224 "detail": [ 

225 { 

226 "type": "int_parsing", 

227 "loc": ["query", "query"], 

228 "msg": "Input should be a valid integer, unable to parse string as an integer", 

229 "input": "foo", 

230 } 

231 ] 

232 } 

233 ) | IsDict( 

234 # TODO: remove when deprecating Pydantic v1 

235 { 

236 "detail": [ 

237 { 

238 "loc": ["query", "query"], 

239 "msg": "value is not a valid integer", 

240 "type": "type_error.integer", 

241 } 

242 ] 

243 } 

244 ) 

245 

246 

247def test_query_int_default(): 1abcde

248 response = client.get("/query/int/default") 1abcde

249 assert response.status_code == 200 1abcde

250 assert response.json() == "foo bar 10" 1abcde

251 

252 

253def test_query_int_default_query_50(): 1abcde

254 response = client.get("/query/int/default?query=50") 1abcde

255 assert response.status_code == 200 1abcde

256 assert response.json() == "foo bar 50" 1abcde

257 

258 

259def test_query_int_default_query_foo(): 1abcde

260 response = client.get("/query/int/default?query=foo") 1abcde

261 assert response.status_code == 422 1abcde

262 assert response.json() == IsDict( 1abcde

263 { 

264 "detail": [ 

265 { 

266 "type": "int_parsing", 

267 "loc": ["query", "query"], 

268 "msg": "Input should be a valid integer, unable to parse string as an integer", 

269 "input": "foo", 

270 } 

271 ] 

272 } 

273 ) | IsDict( 

274 # TODO: remove when deprecating Pydantic v1 

275 { 

276 "detail": [ 

277 { 

278 "loc": ["query", "query"], 

279 "msg": "value is not a valid integer", 

280 "type": "type_error.integer", 

281 } 

282 ] 

283 } 

284 ) 

285 

286 

287def test_query_param(): 1abcde

288 response = client.get("/query/param") 1abcde

289 assert response.status_code == 200 1abcde

290 assert response.json() == "foo bar" 1abcde

291 

292 

293def test_query_param_query_50(): 1abcde

294 response = client.get("/query/param?query=50") 1abcde

295 assert response.status_code == 200 1abcde

296 assert response.json() == "foo bar 50" 1abcde

297 

298 

299def test_query_param_required(): 1abcde

300 response = client.get("/query/param-required") 1abcde

301 assert response.status_code == 422 1abcde

302 assert response.json() == IsDict( 1abcde

303 { 

304 "detail": [ 

305 { 

306 "type": "missing", 

307 "loc": ["query", "query"], 

308 "msg": "Field required", 

309 "input": None, 

310 } 

311 ] 

312 } 

313 ) | IsDict( 

314 # TODO: remove when deprecating Pydantic v1 

315 { 

316 "detail": [ 

317 { 

318 "loc": ["query", "query"], 

319 "msg": "field required", 

320 "type": "value_error.missing", 

321 } 

322 ] 

323 } 

324 ) 

325 

326 

327def test_query_param_required_query_50(): 1abcde

328 response = client.get("/query/param-required?query=50") 1abcde

329 assert response.status_code == 200 1abcde

330 assert response.json() == "foo bar 50" 1abcde

331 

332 

333def test_query_param_required_int(): 1abcde

334 response = client.get("/query/param-required/int") 1abcde

335 assert response.status_code == 422 1abcde

336 assert response.json() == IsDict( 1abcde

337 { 

338 "detail": [ 

339 { 

340 "type": "missing", 

341 "loc": ["query", "query"], 

342 "msg": "Field required", 

343 "input": None, 

344 } 

345 ] 

346 } 

347 ) | IsDict( 

348 # TODO: remove when deprecating Pydantic v1 

349 { 

350 "detail": [ 

351 { 

352 "loc": ["query", "query"], 

353 "msg": "field required", 

354 "type": "value_error.missing", 

355 } 

356 ] 

357 } 

358 ) 

359 

360 

361def test_query_param_required_int_query_50(): 1abcde

362 response = client.get("/query/param-required/int?query=50") 1abcde

363 assert response.status_code == 200 1abcde

364 assert response.json() == "foo bar 50" 1abcde

365 

366 

367def test_query_param_required_int_query_foo(): 1abcde

368 response = client.get("/query/param-required/int?query=foo") 1abcde

369 assert response.status_code == 422 1abcde

370 assert response.json() == IsDict( 1abcde

371 { 

372 "detail": [ 

373 { 

374 "type": "int_parsing", 

375 "loc": ["query", "query"], 

376 "msg": "Input should be a valid integer, unable to parse string as an integer", 

377 "input": "foo", 

378 } 

379 ] 

380 } 

381 ) | IsDict( 

382 # TODO: remove when deprecating Pydantic v1 

383 { 

384 "detail": [ 

385 { 

386 "loc": ["query", "query"], 

387 "msg": "value is not a valid integer", 

388 "type": "type_error.integer", 

389 } 

390 ] 

391 } 

392 ) 

393 

394 

395def test_query_frozenset_query_1_query_1_query_2(): 1abcde

396 response = client.get("/query/frozenset/?query=1&query=1&query=2") 1abcde

397 assert response.status_code == 200 1abcde

398 assert response.json() == "1,2" 1abcde

399 

400 

401def test_query_list(): 1abcde

402 response = client.get("/query/list/?device_ids=1&device_ids=2") 1abcde

403 assert response.status_code == 200 1abcde

404 assert response.json() == [1, 2] 1abcde

405 

406 

407def test_query_list_empty(): 1abcde

408 response = client.get("/query/list/") 1abcde

409 assert response.status_code == 422 1abcde

410 

411 

412def test_query_list_default(): 1abcde

413 response = client.get("/query/list-default/?device_ids=1&device_ids=2") 1abcde

414 assert response.status_code == 200 1abcde

415 assert response.json() == [1, 2] 1abcde

416 

417 

418def test_query_list_default_empty(): 1abcde

419 response = client.get("/query/list-default/") 1abcde

420 assert response.status_code == 200 1abcde

421 assert response.json() == [] 1abcde