fix: sync 单不再显示「起点/终点未知」
原因 - sync 单是巡检从上游发现到的订单:/record/detail 只返回分钟数和车牌,没有坐标, 创建/升级时从未写 start_latitude/start_label - 微信端自行还车走 _finish_active_wechat_rentals,只调 _mark_returned(id) 不传坐标, 所以 end_latitude/end_label 也是空 - 后台 routeCell 的逻辑是「标签优先 → 坐标 → 否则未知」,两列都空就显示未知 修法 - 新增 _bike_pos_from_history(code, when?):从 bike_history 取这辆车的位置 (给了时间就取 ±15 分钟内最近一条,否则取最新一条),拿不到返回 None,不编造 - sync 单创建/升级后调 _fill_missing_start:只在坐标为空时回填起点 + 楼栋标签 - _finish_active_wechat_rentals:用最新采集点补终点(SELECT 增加 bike_code) - 新增 scripts/backfill_rental_locations.py:历史单一次性回填, 默认 dry-run,--apply 才写库;排除 reminder(纯本地计时,没有车在动) 测试:169 pytest(新增 sync 起点回填、微信端还车补终点)+ 32 node 全绿 本地库 dry-run:339/336 行缺坐标、0 行可补(订单都是 7 月的,超出 30 天采集保留期)
This commit is contained in:
@@ -47,7 +47,11 @@ class OrderSyncTests(unittest.TestCase):
|
||||
notified_2 INTEGER DEFAULT 0,
|
||||
notified_1 INTEGER DEFAULT 0,
|
||||
notified_0 INTEGER DEFAULT 0,
|
||||
renewal_round INTEGER DEFAULT 1
|
||||
renewal_round INTEGER DEFAULT 1,
|
||||
start_latitude REAL, start_longitude REAL, start_label TEXT,
|
||||
end_latitude REAL, end_longitude REAL, end_label TEXT,
|
||||
returned_at TEXT, raw_latitude REAL, raw_longitude REAL,
|
||||
rounds_override INTEGER
|
||||
);
|
||||
INSERT INTO users (id, email) VALUES (7, 'rider@example.test');
|
||||
INSERT INTO users (id, email) VALUES (8, 'shared-rider@example.test');
|
||||
@@ -56,7 +60,15 @@ class OrderSyncTests(unittest.TestCase):
|
||||
VALUES
|
||||
(101, '9001', 7, 'sync', 0, '2026-01-01 08:00:00', '2099-01-01 00:00:00'),
|
||||
(102, NULL, 7, 'reminder', 0, '2026-01-01 08:00:00', '2099-01-01 00:00:00');
|
||||
""")
|
||||
CREATE TABLE bike_history (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
bike_code TEXT NOT NULL,
|
||||
latitude REAL, longitude REAL,
|
||||
status INTEGER DEFAULT 1, state INTEGER DEFAULT 1,
|
||||
electric REAL DEFAULT 100,
|
||||
recorded_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
""")
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
@@ -538,6 +550,68 @@ class OrderSyncTests(unittest.TestCase):
|
||||
auto_return.assert_called_once_with(101, 7)
|
||||
notify.assert_not_called()
|
||||
|
||||
def test_sync_rental_backfills_start_from_bike_history(self):
|
||||
"""sync 单没有坐标:用开始时间附近的采集点回填起点,别显示「起点未知」。"""
|
||||
active = {"rtn_code": 0, "data": {"nonDealOrder": {
|
||||
"order_id": "9101", "status": 2, "code": "60001"
|
||||
}}}
|
||||
detail = {"rtn_code": 0, "data": {"minutes": 5, "code": "60001"}}
|
||||
settings = {"reminder_enabled": False, "reminder_minutes": [],
|
||||
"rental_duration_minutes": 19, "auto_return_enabled": False}
|
||||
|
||||
def fake_auth(user_id, method, path, **kwargs):
|
||||
if path == "/user/info":
|
||||
return active
|
||||
if path == "/record/detail":
|
||||
return detail
|
||||
return {"rtn_code": 0, "data": {}}
|
||||
|
||||
with patch.object(timer, "get_db", self._get_db), \
|
||||
patch.object(timer, "get_user_auth", return_value=("token", "access")), \
|
||||
patch.object(timer, "get_user_rental_settings", return_value=settings), \
|
||||
patch.object(timer, "request_with_user_auth", side_effect=fake_auth), \
|
||||
patch.object(timer, "start_rental_timer"), \
|
||||
patch.object(timer, "_bike_pos_from_history", return_value=(36.5318, 116.7802)), \
|
||||
patch("campus.resolve_location_label", return_value="博学楼附近", create=True):
|
||||
timer._check_user_order(7)
|
||||
|
||||
conn = self._get_db()
|
||||
row = conn.execute(
|
||||
"SELECT start_latitude, start_longitude, start_label FROM rentals "
|
||||
"WHERE order_id = '9101'").fetchone()
|
||||
conn.close()
|
||||
self.assertIsNotNone(row)
|
||||
self.assertAlmostEqual(36.5318, row["start_latitude"], places=4)
|
||||
self.assertAlmostEqual(116.7802, row["start_longitude"], places=4)
|
||||
self.assertEqual("博学楼附近", row["start_label"])
|
||||
|
||||
def test_wechat_side_return_fills_end_position_from_history(self):
|
||||
"""微信端自行还车:上游不给坐标,用最新采集点补终点。"""
|
||||
conn = self._get_db()
|
||||
conn.execute("""
|
||||
INSERT INTO rentals
|
||||
(id, order_id, user_id, source, expired, start_time, end_time, bike_code, email)
|
||||
VALUES (103, '9103', 7, 'sync', 0, '2026-01-01 09:00:00',
|
||||
'2099-01-01 00:00:00', '60003', 'rider@example.test')
|
||||
""")
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
with patch.object(timer, "get_db", self._get_db), \
|
||||
patch.object(timer, "_bike_pos_from_history", return_value=(36.5321, 116.7805)), \
|
||||
patch("campus.resolve_location_label", return_value="图书馆附近", create=True):
|
||||
timer._finish_active_wechat_rentals(7, "测试:上游已无订单")
|
||||
|
||||
conn = self._get_db()
|
||||
row = conn.execute(
|
||||
"SELECT expired, end_latitude, end_longitude, end_label FROM rentals WHERE id = 103"
|
||||
).fetchone()
|
||||
conn.close()
|
||||
self.assertEqual(1, row["expired"])
|
||||
self.assertAlmostEqual(36.5321, row["end_latitude"], places=4)
|
||||
self.assertEqual("图书馆附近", row["end_label"])
|
||||
|
||||
|
||||
def test_auto_return_failure_keeps_notified_zero_and_schedules_retry(self):
|
||||
"""上游还车失败时不能把 notified_0 置位,否则这一单再也不会重试。"""
|
||||
class ImmediateTimer:
|
||||
|
||||
Reference in New Issue
Block a user