Add trip history and route replay
Co-authored-by: Copilot <[email protected]>
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
import os
|
||||
import tempfile
|
||||
import unittest
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from modules import app_sql
|
||||
|
||||
|
||||
class TripSqlTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
handle, self.path = tempfile.mkstemp(suffix=".db")
|
||||
os.close(handle)
|
||||
self.original_path = app_sql.sql_path
|
||||
app_sql._conn = None
|
||||
app_sql._last_trip_refresh = 0
|
||||
app_sql.sql_path = self.path
|
||||
self.conn = app_sql.get_conn()
|
||||
|
||||
def tearDown(self):
|
||||
if app_sql._conn is not None:
|
||||
app_sql._conn.close()
|
||||
app_sql._conn = None
|
||||
app_sql._last_trip_refresh = 0
|
||||
app_sql.sql_path = self.original_path
|
||||
os.unlink(self.path)
|
||||
|
||||
def insert_motion(self, start_id, count, started, latitude):
|
||||
rows = [
|
||||
(
|
||||
start_id + index,
|
||||
"Yaris",
|
||||
"test",
|
||||
started + timedelta(seconds=index * 5),
|
||||
latitude + index * 0.0001,
|
||||
-73.5,
|
||||
20,
|
||||
None,
|
||||
0,
|
||||
)
|
||||
for index in range(count)
|
||||
]
|
||||
self.conn.executemany(
|
||||
"""INSERT INTO tracking (
|
||||
id, VN, NAME, DATETIME, GPSX, GPSY, SPEED, ODOMETER, UPLOADED
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)""",
|
||||
rows,
|
||||
)
|
||||
self.conn.commit()
|
||||
|
||||
def test_incremental_rebuild_preserves_trip_before_overlap_boundary(self):
|
||||
started = datetime(2026, 1, 1, 8)
|
||||
self.insert_motion(1, 160, started, 45.5)
|
||||
self.insert_motion(161, 10, started + timedelta(minutes=30), 45.6)
|
||||
app_sql.refresh_trips(force=True)
|
||||
original = self.conn.execute(
|
||||
"""SELECT START_TRACKING_ID, END_TRACKING_ID, DISTANCE_KM
|
||||
FROM trips ORDER BY START_TRACKING_ID"""
|
||||
).fetchall()
|
||||
self.assertEqual(len(original), 2)
|
||||
|
||||
self.insert_motion(171, 1, started + timedelta(minutes=31), 45.601)
|
||||
app_sql._last_trip_refresh = 0
|
||||
app_sql.refresh_trips()
|
||||
rebuilt = self.conn.execute(
|
||||
"""SELECT START_TRACKING_ID, END_TRACKING_ID, DISTANCE_KM
|
||||
FROM trips ORDER BY START_TRACKING_ID"""
|
||||
).fetchall()
|
||||
|
||||
self.assertEqual(rebuilt[0], original[0])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user