Coverage for app/tests/specification/test_from_specifications.py: 100%

153 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-11-27 09:26 +0000

1import pytest 

2from fastapi.testclient import TestClient 

3from http import HTTPStatus 

4from app.main import app 

5from app.delivery_fee.models import OrderInfo 

6import json 

7 

8 

9@pytest.fixture 

10def client(): 

11 return TestClient(app) 

12 

13 

14def test_cart_value_for_lt_10(client: TestClient): 

15 order_info = OrderInfo( 

16 cart_value=8.9e2, 

17 delivery_distance=0, 

18 number_of_items=0, 

19 time="2024-01-15T13:00:00Z" 

20 ) 

21 json_request_data = json.loads(order_info.model_dump_json()) 

22 res = client.post("/api/delivery/calculate_delivery_fee/", 

23 json=json_request_data) 

24 

25 assert res.status_code == HTTPStatus.OK 

26 assert res.json() == {"delivery_fee": 1.10e2} 

27 

28 

29def test_cart_value_for_gt_10(client: TestClient): 

30 order_info = OrderInfo( 

31 cart_value=10.1e2, 

32 delivery_distance=0, 

33 number_of_items=0, 

34 time="2024-01-15T13:00:00Z" 

35 ) 

36 json_request_data = json.loads(order_info.model_dump_json()) 

37 res = client.post("/api/delivery/calculate_delivery_fee/", 

38 json=json_request_data) 

39 

40 assert res.status_code == HTTPStatus.OK 

41 assert res.json() == {"delivery_fee": 0} 

42 

43 

44def test_delivery_distance_for_lt_1km(client: TestClient): 

45 order_info = OrderInfo( 

46 cart_value=0, 

47 delivery_distance=9.9e2, 

48 number_of_items=0, 

49 time="2024-01-15T13:00:00Z" 

50 ) 

51 json_request_data = json.loads(order_info.model_dump_json()) 

52 res = client.post("/api/delivery/calculate_delivery_fee/", 

53 json=json_request_data) 

54 

55 assert res.status_code == HTTPStatus.OK 

56 assert res.json() == {"delivery_fee": 2e2 + 10e2} 

57 

58 

59def test_delivery_distance_for_gt_1km(client: TestClient): 

60 order_info = OrderInfo( 

61 cart_value=0, 

62 delivery_distance=10.1e2, 

63 number_of_items=0, 

64 time="2024-01-15T13:00:00Z" 

65 ) 

66 json_request_data = json.loads(order_info.model_dump_json()) 

67 res = client.post("/api/delivery/calculate_delivery_fee/", 

68 json=json_request_data) 

69 

70 assert res.status_code == HTTPStatus.OK 

71 assert res.json() == {"delivery_fee": 2e2 + 10e2 + 1e2} 

72 

73 

74def test_delivery_distance_for_1499m(client: TestClient): 

75 order_info = OrderInfo( 

76 cart_value=0, 

77 delivery_distance=1.499e3, 

78 number_of_items=0, 

79 time="2024-01-15T13:00:00Z" 

80 ) 

81 json_request_data = json.loads(order_info.model_dump_json()) 

82 res = client.post("/api/delivery/calculate_delivery_fee/", 

83 json=json_request_data) 

84 

85 assert res.status_code == HTTPStatus.OK 

86 assert res.json() == {"delivery_fee": 10e2 + (2e2 + 1e2)} 

87 

88 

89def test_delivery_distance_for_1500m(client: TestClient): 

90 order_info = OrderInfo( 

91 cart_value=0, 

92 delivery_distance=1.5e3, 

93 number_of_items=0, 

94 time="2024-01-15T13:00:00Z" 

95 ) 

96 json_request_data = json.loads(order_info.model_dump_json()) 

97 res = client.post("/api/delivery/calculate_delivery_fee/", 

98 json=json_request_data) 

99 

100 assert res.status_code == HTTPStatus.OK 

101 assert res.json() == {"delivery_fee": 10e2 + (2e2 + 1e2)} 

102 

103 

104def test_delivery_distance_for_1501m(client: TestClient): 

105 order_info = OrderInfo( 

106 cart_value=0, 

107 delivery_distance=1.501e3, 

108 number_of_items=0, 

109 time="2024-01-15T13:00:00Z" 

110 ) 

111 json_request_data = json.loads(order_info.model_dump_json()) 

112 res = client.post("/api/delivery/calculate_delivery_fee/", 

113 json=json_request_data) 

114 

115 assert res.status_code == HTTPStatus.OK 

116 assert res.json() == {"delivery_fee": 10e2 + (2e2 + 1e2 + 1e2)} 

117 

118 

119def test_delivery_distance_for_2000m(client: TestClient): 

120 order_info = OrderInfo( 

121 cart_value=0, 

122 delivery_distance=2e3, 

123 number_of_items=0, 

124 time="2024-01-15T13:00:00Z" 

125 ) 

126 json_request_data = json.loads(order_info.model_dump_json()) 

127 res = client.post("/api/delivery/calculate_delivery_fee/", 

128 json=json_request_data) 

129 

130 assert res.status_code == HTTPStatus.OK 

131 assert res.json() == {"delivery_fee": 10e2 + (2e2 + 1e2 + 1e2)} 

132 

133 

134def test_delivery_distance_for_2001m(client: TestClient): 

135 order_info = OrderInfo( 

136 cart_value=0, 

137 delivery_distance=2.001e3, 

138 number_of_items=0, 

139 time="2024-01-15T13:00:00Z" 

140 ) 

141 json_request_data = json.loads(order_info.model_dump_json()) 

142 res = client.post("/api/delivery/calculate_delivery_fee/", 

143 json=json_request_data) 

144 

145 assert res.status_code == HTTPStatus.OK 

146 assert res.json() == {"delivery_fee": 10e2 + (2e2 + 1e2 + 1e2 + 1e2)} 

147 

148 

149def test_number_of_items_lt_5(client: TestClient): 

150 order_info = OrderInfo( 

151 cart_value=0, 

152 delivery_distance=0, 

153 number_of_items=4, 

154 time="2024-01-15T13:00:00Z" 

155 ) 

156 json_request_data = json.loads(order_info.model_dump_json()) 

157 

158 res = client.post("/api/delivery/calculate_delivery_fee/", 

159 json=json_request_data) 

160 

161 assert res.status_code == HTTPStatus.OK 

162 assert res.json() == {"delivery_fee": 10e2} 

163 

164 

165def test_number_of_items_eq_5(client: TestClient): 

166 order_info = OrderInfo( 

167 cart_value=0, 

168 delivery_distance=0, 

169 number_of_items=5, 

170 time="2024-01-15T13:00:00Z" 

171 ) 

172 json_request_data = json.loads(order_info.model_dump_json()) 

173 

174 res = client.post("/api/delivery/calculate_delivery_fee/", 

175 json=json_request_data) 

176 

177 assert res.status_code == HTTPStatus.OK 

178 assert res.json() == {"delivery_fee": 10e2 + 0.5e2} 

179 

180 

181def test_number_of_items_for_10(client: TestClient): 

182 order_info = OrderInfo( 

183 cart_value=0, 

184 delivery_distance=0, 

185 number_of_items=10, 

186 time="2024-01-15T13:00:00Z" 

187 ) 

188 json_request_data = json.loads(order_info.model_dump_json()) 

189 

190 res = client.post("/api/delivery/calculate_delivery_fee/", 

191 json=json_request_data) 

192 

193 assert res.status_code == HTTPStatus.OK 

194 assert res.json() == {"delivery_fee": 10e2 + (0.5e2*(10-4))} 

195 

196 

197def test_number_of_items_for_13(client: TestClient): 

198 order_info = OrderInfo( 

199 cart_value=0, 

200 delivery_distance=0, 

201 number_of_items=13, 

202 time="2024-01-15T13:00:00Z" 

203 ) 

204 json_request_data = json.loads(order_info.model_dump_json()) 

205 

206 res = client.post("/api/delivery/calculate_delivery_fee/", 

207 json=json_request_data) 

208 

209 assert res.status_code == HTTPStatus.OK 

210 assert res.json() == {"delivery_fee": min((10e2 + (0.5e2*(13-4)) + 1.2e2), 15e2)} 

211 

212 

213def test_number_of_items_for_14(client: TestClient): 

214 order_info = OrderInfo( 

215 cart_value=0, 

216 delivery_distance=0, 

217 number_of_items=14, 

218 time="2024-01-15T13:00:00Z" 

219 ) 

220 json_request_data = json.loads(order_info.model_dump_json()) 

221 

222 res = client.post("/api/delivery/calculate_delivery_fee/", 

223 json=json_request_data) 

224 

225 assert res.status_code == HTTPStatus.OK 

226 assert res.json() == {"delivery_fee": 15e2} 

227 

228 

229def test_delivery_fee_never_gt_15euro(client: TestClient): 

230 order_info = OrderInfo( 

231 cart_value=0, 

232 delivery_distance=0, 

233 number_of_items=100, 

234 time="2024-01-15T13:00:00Z" 

235 ) 

236 json_request_data = json.loads(order_info.model_dump_json()) 

237 

238 res = client.post("/api/delivery/calculate_delivery_fee/", 

239 json=json_request_data) 

240 

241 assert res.status_code == HTTPStatus.OK 

242 assert res.json() == {"delivery_fee": 15e2} 

243 

244 

245def test_delivery_fee_0_when_cart_value_gte_200euro(client: TestClient): 

246 order_info = OrderInfo( 

247 cart_value=200e2, 

248 delivery_distance=15000, 

249 number_of_items=3000, 

250 time="2024-01-15T13:00:00Z" 

251 ) 

252 json_request_data = json.loads(order_info.model_dump_json()) 

253 

254 res = client.post("/api/delivery/calculate_delivery_fee/", 

255 json=json_request_data) 

256 

257 assert res.status_code == HTTPStatus.OK 

258 assert res.json() == {"delivery_fee": 0} 

259 

260 

261def test_friday_rush_hour_fee_for_12h(client: TestClient): 

262 order_info = OrderInfo( 

263 cart_value=5e2, 

264 delivery_distance=0, 

265 number_of_items=0, 

266 time="2024-02-02T13:00:00Z" 

267 ) 

268 json_request_data = json.loads(order_info.model_dump_json()) 

269 

270 res = client.post("/api/delivery/calculate_delivery_fee/", 

271 json=json_request_data) 

272 assert res.status_code == HTTPStatus.OK 

273 assert res.json() == {"delivery_fee": 5e2} 

274 

275 

276def test_friday_rush_hour_fee_for_15h(client: TestClient): 

277 order_info = OrderInfo( 

278 cart_value=5e2, 

279 delivery_distance=0, 

280 number_of_items=0, 

281 time="2024-02-02T15:00:00Z" 

282 ) 

283 json_request_data = json.loads(order_info.model_dump_json()) 

284 

285 res = client.post("/api/delivery/calculate_delivery_fee/", 

286 json=json_request_data) 

287 assert res.status_code == HTTPStatus.OK 

288 assert res.json() == {"delivery_fee": 5e2*1.2} 

289 

290 

291def test_friday_rush_hour_fee_for_end_of_19h(client: TestClient): 

292 order_info = OrderInfo( 

293 cart_value=5e2, 

294 delivery_distance=0, 

295 number_of_items=0, 

296 time="2024-02-02T19:59:59Z" 

297 ) 

298 json_request_data = json.loads(order_info.model_dump_json()) 

299 

300 res = client.post("/api/delivery/calculate_delivery_fee/", 

301 json=json_request_data) 

302 assert res.status_code == HTTPStatus.OK 

303 assert res.json() == {"delivery_fee": 5e2*1.2} 

304 

305 

306def test_friday_rush_hour_fee_after_19h(client: TestClient): 

307 order_info = OrderInfo( 

308 cart_value=5e2, 

309 delivery_distance=0, 

310 number_of_items=0, 

311 time="2024-02-02T20:00:00Z" 

312 ) 

313 json_request_data = json.loads(order_info.model_dump_json()) 

314 

315 res = client.post("/api/delivery/calculate_delivery_fee/", 

316 json=json_request_data) 

317 assert res.status_code == HTTPStatus.OK 

318 assert res.json() == {"delivery_fee": 5e2} 

319 

320 

321def test_not_friday_rush_hour_fee_for_12h(client: TestClient): 

322 order_info = OrderInfo( 

323 cart_value=5e2, 

324 delivery_distance=0, 

325 number_of_items=0, 

326 time="2024-02-01T13:00:00Z" 

327 ) 

328 json_request_data = json.loads(order_info.model_dump_json()) 

329 

330 res = client.post("/api/delivery/calculate_delivery_fee/", 

331 json=json_request_data) 

332 assert res.status_code == HTTPStatus.OK 

333 assert res.json() == {"delivery_fee": 5e2} 

334 

335 

336def test_not_friday_rush_hour_fee_for_15h(client: TestClient): 

337 order_info = OrderInfo( 

338 cart_value=5e2, 

339 delivery_distance=0, 

340 number_of_items=0, 

341 time="2024-02-01T15:00:00Z" 

342 ) 

343 json_request_data = json.loads(order_info.model_dump_json()) 

344 

345 res = client.post("/api/delivery/calculate_delivery_fee/", 

346 json=json_request_data) 

347 assert res.status_code == HTTPStatus.OK 

348 assert res.json() == {"delivery_fee": 5e2} 

349 

350 

351def test_not_friday_rush_hour_fee_for_end_of_19h(client: TestClient): 

352 order_info = OrderInfo( 

353 cart_value=5e2, 

354 delivery_distance=0, 

355 number_of_items=0, 

356 time="2024-02-01T19:59:59Z" 

357 ) 

358 json_request_data = json.loads(order_info.model_dump_json()) 

359 

360 res = client.post("/api/delivery/calculate_delivery_fee/", 

361 json=json_request_data) 

362 assert res.status_code == HTTPStatus.OK 

363 assert res.json() == {"delivery_fee": 5e2} 

364 

365 

366def test_not_friday_rush_hour_fee_after_19h(client: TestClient): 

367 order_info = OrderInfo( 

368 cart_value=5e2, 

369 delivery_distance=0, 

370 number_of_items=0, 

371 time="2024-02-01T20:00:00Z" 

372 ) 

373 json_request_data = json.loads(order_info.model_dump_json()) 

374 

375 res = client.post("/api/delivery/calculate_delivery_fee/", 

376 json=json_request_data) 

377 assert res.status_code == HTTPStatus.OK 

378 assert res.json() == {"delivery_fee": 5e2}