feat: add soft delete for import batches

This commit is contained in:
chengma
2026-07-01 09:41:10 +08:00
parent 5b044f2349
commit cc277366ed
10 changed files with 362 additions and 42 deletions
+55
View File
@@ -167,6 +167,61 @@ class DbTests(TempDirMixin, unittest.TestCase):
self.assert_removed(temp_dir)
def test_delete_batch_soft_hides_batch_and_tasks(self):
with self.make_temp_dir() as temp_dir:
db_path = os.path.join(temp_dir, "cmshopee.db")
db.init_db(db_path)
batch_id = db.create_batch(["input.xlsx"], path=db_path)
db.insert_tasks(
batch_id,
[
{
"source_file_abs": os.path.join(temp_dir, "input.xlsx"),
"source_sheet": "Sheet1",
"source_row": 2,
"account_name": "shop",
"alias": "alias",
"item_id": "51100639510",
},
{
"source_file_abs": os.path.join(temp_dir, "input.xlsx"),
"source_sheet": "Sheet1",
"source_row": 3,
"account_name": "shop",
"alias": "alias",
"item_id": "51100639511",
},
],
path=db_path,
)
tasks = db.list_tasks(batch_id=batch_id, path=db_path)
old_cover = os.path.join(temp_dir, "old.jpg")
new_cover = os.path.join(temp_dir, "new.jpg")
db.set_collected(tasks[0].id, "旧标题", old_cover, path=db_path)
db.set_generated(tasks[0].id, "新标题", new_cover, path=db_path)
db.set_applied(tasks[0].id, True, path=db_path)
result = db.delete_batch(batch_id, reason="导错文件", path=db_path)
self.assertEqual(batch_id, result["batch_id"])
self.assertEqual(2, result["task_count"])
self.assertEqual(1, result["committed_count"])
self.assertEqual([old_cover, new_cover], result["image_paths"])
self.assertEqual([], db.list_batches(path=db_path))
self.assertEqual([], db.list_tasks(path=db_path))
self.assertIsNone(db.get_batch(batch_id, path=db_path))
self.assertIsNone(db.get_task(tasks[0].id, path=db_path))
deleted_task = db.get_task(tasks[0].id, path=db_path, include_deleted=True)
self.assertEqual(tasks[0].id, deleted_task.id)
deleted_batch = db.get_batch(batch_id, path=db_path, include_deleted=True)
self.assertIsNotNone(deleted_batch.deleted_at)
self.assertEqual("导错文件", deleted_batch.deleted_reason)
deleted_tasks = db.list_tasks(batch_id=batch_id, path=db_path, include_deleted=True)
self.assertEqual(2, len(deleted_tasks))
self.assert_removed(temp_dir)
def test_duplicate_task_and_invalid_update_raise_clear_errors(self):
with self.make_temp_dir() as temp_dir:
db_path = os.path.join(temp_dir, "cmshopee.db")