feat: 增加采集任务重新执行入口 (#63)
This commit is contained in:
+47
-3
@@ -1,4 +1,4 @@
|
||||
"""SQLite 初始化和 v1 数据库结构测试。"""
|
||||
"""SQLite 初始化和数据库迁移测试。"""
|
||||
|
||||
import sqlite3
|
||||
import tempfile
|
||||
@@ -6,6 +6,7 @@ import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from src.db import DatabaseVersionError, initialize_database, open_database
|
||||
from src.db_schema import MIGRATION_1
|
||||
|
||||
|
||||
EXPECTED_TABLES = {
|
||||
@@ -36,7 +37,7 @@ class DatabaseInitializationTests(unittest.TestCase):
|
||||
def tearDown(self) -> None:
|
||||
self._temporary_directory.cleanup()
|
||||
|
||||
def test_initialize_creates_v1_tables_and_indexes(self) -> None:
|
||||
def test_initialize_creates_latest_tables_and_indexes(self) -> None:
|
||||
result_path = initialize_database(self.db_path)
|
||||
|
||||
self.assertEqual(result_path, self.db_path)
|
||||
@@ -62,7 +63,50 @@ class DatabaseInitializationTests(unittest.TestCase):
|
||||
|
||||
self.assertTrue(EXPECTED_TABLES.issubset(tables))
|
||||
self.assertTrue(EXPECTED_INDEXES.issubset(indexes))
|
||||
self.assertEqual(version, 1)
|
||||
self.assertEqual(version, 2)
|
||||
|
||||
def test_v1_database_is_upgraded_without_losing_task_runs(self) -> None:
|
||||
connection = open_database(self.db_path)
|
||||
try:
|
||||
with connection:
|
||||
for statement in MIGRATION_1:
|
||||
connection.execute(statement)
|
||||
connection.execute("PRAGMA user_version = 1")
|
||||
connection.execute(
|
||||
"INSERT INTO pdd_tasks"
|
||||
" (remote_task_id, task_type, goods_url, status, received_at,"
|
||||
" created_at, updated_at) VALUES"
|
||||
" ('TASK-OLD', 'collect', 'https://example.test', 'claimed',"
|
||||
" '2026-08-06T00:00:00Z', '2026-08-06T00:00:00Z',"
|
||||
" '2026-08-06T00:00:00Z')"
|
||||
)
|
||||
connection.execute(
|
||||
"INSERT INTO task_runs"
|
||||
" (task_id, attempt_id, attempt_no, device_address, run_status,"
|
||||
" started_at, created_at, updated_at) VALUES"
|
||||
" (1, 'ATTEMPT-OLD', 1, 'USB-001', 'succeeded',"
|
||||
" '2026-08-06T00:00:00Z', '2026-08-06T00:00:00Z',"
|
||||
" '2026-08-06T00:00:00Z')"
|
||||
)
|
||||
finally:
|
||||
connection.close()
|
||||
|
||||
initialize_database(self.db_path)
|
||||
|
||||
connection = open_database(self.db_path)
|
||||
try:
|
||||
columns = {
|
||||
row[1] for row in connection.execute("PRAGMA table_info(task_runs)")
|
||||
}
|
||||
attempt_id = connection.execute(
|
||||
"SELECT attempt_id FROM task_runs"
|
||||
).fetchone()[0]
|
||||
version = connection.execute("PRAGMA user_version").fetchone()[0]
|
||||
finally:
|
||||
connection.close()
|
||||
self.assertIn("result_data", columns)
|
||||
self.assertEqual(attempt_id, "ATTEMPT-OLD")
|
||||
self.assertEqual(version, 2)
|
||||
|
||||
def test_initialize_can_run_twice_without_losing_data(self) -> None:
|
||||
initialize_database(self.db_path)
|
||||
|
||||
Reference in New Issue
Block a user