feat: 实现受控真实下单安全边界 (#99)
This commit is contained in:
+65
-3
@@ -6,7 +6,7 @@ import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from src.db import DatabaseVersionError, initialize_database, open_database
|
||||
from src.db_schema import MIGRATION_1
|
||||
from src.db_schema import MIGRATION_1, MIGRATION_2
|
||||
|
||||
|
||||
EXPECTED_TABLES = {
|
||||
@@ -63,7 +63,7 @@ class DatabaseInitializationTests(unittest.TestCase):
|
||||
|
||||
self.assertTrue(EXPECTED_TABLES.issubset(tables))
|
||||
self.assertTrue(EXPECTED_INDEXES.issubset(indexes))
|
||||
self.assertEqual(version, 2)
|
||||
self.assertEqual(version, 3)
|
||||
|
||||
def test_v1_database_is_upgraded_without_losing_task_runs(self) -> None:
|
||||
connection = open_database(self.db_path)
|
||||
@@ -106,7 +106,7 @@ class DatabaseInitializationTests(unittest.TestCase):
|
||||
connection.close()
|
||||
self.assertIn("result_data", columns)
|
||||
self.assertEqual(attempt_id, "ATTEMPT-OLD")
|
||||
self.assertEqual(version, 2)
|
||||
self.assertEqual(version, 3)
|
||||
|
||||
def test_initialize_can_run_twice_without_losing_data(self) -> None:
|
||||
initialize_database(self.db_path)
|
||||
@@ -133,6 +133,44 @@ class DatabaseInitializationTests(unittest.TestCase):
|
||||
|
||||
self.assertEqual(value, "true")
|
||||
|
||||
def test_v2_database_adds_safe_execution_mode_default(self) -> None:
|
||||
connection = open_database(self.db_path)
|
||||
try:
|
||||
with connection:
|
||||
for statement in MIGRATION_1 + MIGRATION_2:
|
||||
connection.execute(statement)
|
||||
connection.execute("PRAGMA user_version = 2")
|
||||
connection.execute(
|
||||
"INSERT INTO pdd_tasks"
|
||||
" (remote_task_id, task_type, goods_url, status, received_at,"
|
||||
" created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
||||
(
|
||||
"PUR-OLD",
|
||||
"purchase",
|
||||
"https://example.test",
|
||||
"claimed",
|
||||
"2026-08-10T00:00:00Z",
|
||||
"2026-08-10T00:00:00Z",
|
||||
"2026-08-10T00:00:00Z",
|
||||
),
|
||||
)
|
||||
finally:
|
||||
connection.close()
|
||||
|
||||
initialize_database(self.db_path)
|
||||
|
||||
connection = open_database(self.db_path)
|
||||
try:
|
||||
row = connection.execute(
|
||||
"SELECT execution_mode FROM pdd_tasks"
|
||||
" WHERE remote_task_id = 'PUR-OLD'"
|
||||
).fetchone()
|
||||
version = connection.execute("PRAGMA user_version").fetchone()[0]
|
||||
finally:
|
||||
connection.close()
|
||||
self.assertEqual(row[0], "dry_run")
|
||||
self.assertEqual(version, 3)
|
||||
|
||||
def test_new_connection_uses_required_pragmas(self) -> None:
|
||||
initialize_database(self.db_path)
|
||||
connection = open_database(self.db_path)
|
||||
@@ -181,6 +219,30 @@ class DatabaseInitializationTests(unittest.TestCase):
|
||||
finally:
|
||||
connection.close()
|
||||
|
||||
def test_execution_mode_constraint_is_enforced(self) -> None:
|
||||
initialize_database(self.db_path)
|
||||
connection = open_database(self.db_path)
|
||||
try:
|
||||
with self.assertRaises(sqlite3.IntegrityError):
|
||||
connection.execute(
|
||||
"INSERT INTO pdd_tasks"
|
||||
" (remote_task_id, task_type, execution_mode, goods_url,"
|
||||
" status, received_at, created_at, updated_at)"
|
||||
" VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
(
|
||||
"PUR-INVALID-MODE",
|
||||
"purchase",
|
||||
"unknown",
|
||||
"https://example.test/goods",
|
||||
"claimed",
|
||||
"2026-08-10T00:00:00Z",
|
||||
"2026-08-10T00:00:00Z",
|
||||
"2026-08-10T00:00:00Z",
|
||||
),
|
||||
)
|
||||
finally:
|
||||
connection.close()
|
||||
|
||||
def test_task_run_and_outbox_foreign_keys_are_enforced(self) -> None:
|
||||
initialize_database(self.db_path)
|
||||
connection = open_database(self.db_path)
|
||||
|
||||
Reference in New Issue
Block a user