polish: 本地回写失败不再报「还车失败」+ 清理死分支
1) order_return 本地回写裸奔 - _record_rental_end / _record_raw_coords 各自包 try/except,只记日志; 仍然返回 rtn_code 0 + 「(本地记录稍后同步)」+ local_synced=False。 原来 SQLite 忙的时候直接 500 → 前端「还车失败」,用户再点一次撞 204010 2) 管理端兜底 UPDATE 也包住 - 按 rental_id 的兜底 UPDATE 单独 try/except;失败同样返回成功 + warning, 避免管理员重复点、第二次拿到「订单信息不存在」 3) _recover_overdue_rental 死分支 - 函数开头已有 _should_auto_return 的 return,后面的三元 else 永远走不到, 直接写 _run_recovery(...) 测试:156 pytest(新增用户端/管理端「本地写库失败仍报成功」2 条)+ 32 node 全绿
This commit is contained in:
@@ -110,6 +110,30 @@ class AdminReturnTests(unittest.TestCase):
|
||||
self.assertEqual(1, row["expired"])
|
||||
self.assertTrue(row["returned_at"]) # 兜底也写了实际还车时间
|
||||
|
||||
def test_admin_return_reports_success_when_local_write_fails(self):
|
||||
"""上游成功、本地写库失败:仍返回成功 + 提示稍后同步,别让管理员重复点。"""
|
||||
app = Flask(__name__)
|
||||
app.secret_key = "test"
|
||||
app.register_blueprint(admin_routes.bp)
|
||||
client = app.test_client()
|
||||
with client.session_transaction() as sess:
|
||||
sess["admin_logged_in"] = True
|
||||
|
||||
with patch.object(admin_routes, "get_db", self._get_db), \
|
||||
patch.object(admin_routes, "request_with_user_auth", return_value={
|
||||
"rtn_code": 0, "data": {"latitude": 36.53, "longitude": 116.78}}), \
|
||||
patch("routes.rental._try_return_candidates",
|
||||
return_value=({"rtn_code": 0}, (36.53, 116.78), ["你上报的位置"])), \
|
||||
patch("routes.rental._record_rental_end",
|
||||
side_effect=RuntimeError("database is locked")), \
|
||||
patch("timer._try_card_pay_auto", return_value={}):
|
||||
resp = client.post("/api/admin/return", json={"rental_id": 101, "user_id": 7})
|
||||
|
||||
body = resp.get_json()
|
||||
self.assertEqual(0, body["rtn_code"])
|
||||
self.assertFalse(body["local_synced"])
|
||||
self.assertIn("本地记录稍后同步", body["message"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@@ -673,6 +673,39 @@ class OrderSyncTests(unittest.TestCase):
|
||||
self.assertIn(107, recovered_ids) # subscribe 要补
|
||||
self.assertNotIn(106, recovered_ids) # reminder 不补
|
||||
|
||||
def test_return_succeeds_even_when_local_write_fails(self):
|
||||
"""上游还车成功、本地写库抛异常:不能报「还车失败」(否则用户会再点一次撞 204010)。"""
|
||||
app = Flask(__name__)
|
||||
app.secret_key = "test"
|
||||
app.register_blueprint(rental_routes.bp)
|
||||
client = app.test_client()
|
||||
|
||||
def fake_auth(user_id, method, path, **kwargs):
|
||||
if path == "/record/return":
|
||||
return {"rtn_code": 0, "message": "ok"}
|
||||
return {"rtn_code": 0, "data": {}}
|
||||
|
||||
with patch.object(rental_routes, "get_db", self._get_db), \
|
||||
patch.object(rental_routes, "get_current_user", return_value={
|
||||
"id": 7, "email": "rider@example.test"}), \
|
||||
patch.object(rental_routes, "get_user_display_name", return_value="骑行人"), \
|
||||
patch.object(rental_routes, "request_with_user_auth", side_effect=fake_auth), \
|
||||
patch.object(rental_routes, "_record_rental_end",
|
||||
side_effect=RuntimeError("database is locked")), \
|
||||
patch.object(rental_routes, "_record_raw_coords",
|
||||
side_effect=RuntimeError("database is locked")), \
|
||||
patch.object(rental_routes, "_try_card_pay_auto", return_value={}), \
|
||||
patch.object(rental_routes, "_cancel_rental_timers"):
|
||||
resp = client.post("/api/order/return", json={
|
||||
"order_id": "9001", "code": "50001",
|
||||
"latitude": 36.67, "longitude": 116.98,
|
||||
})
|
||||
|
||||
body = resp.get_json()
|
||||
self.assertEqual(0, body["rtn_code"])
|
||||
self.assertFalse(body["local_synced"])
|
||||
self.assertIn("本地记录稍后同步", body["message"])
|
||||
|
||||
def test_retry_success_sends_final_notice(self):
|
||||
"""重试后终于还上,要再发一封收尾,别让用户去点旧邮件里的还车按钮。"""
|
||||
class ImmediateTimer:
|
||||
|
||||
Reference in New Issue
Block a user