fix(product-suite): allow completed source image removal
This commit is contained in:
@@ -574,6 +574,162 @@ class ImageStudioTests(TempDirMixin, unittest.TestCase):
|
||||
|
||||
self.assert_removed(temp_dir)
|
||||
|
||||
def test_remove_original_assets_allows_completed_history_source_and_reference(self):
|
||||
with self.make_temp_dir() as temp_dir:
|
||||
db_path = os.path.join(temp_dir, "cmshopee.db")
|
||||
db.init_db(db_path)
|
||||
project = image_studio.create_or_get_project(
|
||||
account_alias="alias",
|
||||
account_slug="alias_slug",
|
||||
item_id="51100639510",
|
||||
path=db_path,
|
||||
)
|
||||
source = image_studio.add_asset(
|
||||
project.id,
|
||||
image_studio.ASSET_KIND_ORIGINAL,
|
||||
source_order=1,
|
||||
path=db_path,
|
||||
)
|
||||
reference = image_studio.add_asset(
|
||||
project.id,
|
||||
image_studio.ASSET_KIND_ORIGINAL,
|
||||
source_order=2,
|
||||
path=db_path,
|
||||
)
|
||||
output = image_studio.add_asset(
|
||||
project.id,
|
||||
"generated_main",
|
||||
parent_asset_id=source.id,
|
||||
path=db_path,
|
||||
)
|
||||
job = image_studio.create_job(
|
||||
project.id,
|
||||
source_asset_id=source.id,
|
||||
reference_asset_ids=[reference.id],
|
||||
path=db_path,
|
||||
)
|
||||
job = image_studio.update_job_status(
|
||||
job.id,
|
||||
"succeeded",
|
||||
output_asset_id=output.id,
|
||||
path=db_path,
|
||||
)
|
||||
self.assertEqual(image_studio.JOB_RECOVERY_NONE, job.recovery_action)
|
||||
|
||||
removed = image_studio.remove_original_assets_if_unused(
|
||||
project.id,
|
||||
[source.id, reference.id],
|
||||
path=db_path,
|
||||
)
|
||||
|
||||
self.assertEqual([source.id, reference.id], [asset.id for asset in removed])
|
||||
self.assertIsNone(image_studio.get_asset(source.id, path=db_path))
|
||||
self.assertIsNone(image_studio.get_asset(reference.id, path=db_path))
|
||||
saved_job = image_studio.get_job(job.id, path=db_path)
|
||||
saved_output = image_studio.get_asset(output.id, path=db_path)
|
||||
self.assertIsNone(saved_job.source_asset_id)
|
||||
self.assertEqual("[%d]" % reference.id, saved_job.reference_asset_ids)
|
||||
self.assertIsNone(saved_output.parent_asset_id)
|
||||
|
||||
self.assert_removed(temp_dir)
|
||||
|
||||
def test_remove_original_assets_blocks_active_and_retryable_source_or_reference(self):
|
||||
with self.make_temp_dir() as temp_dir:
|
||||
db_path = os.path.join(temp_dir, "cmshopee.db")
|
||||
db.init_db(db_path)
|
||||
project = image_studio.create_or_get_project(
|
||||
account_alias="alias",
|
||||
account_slug="alias_slug",
|
||||
item_id="51100639510",
|
||||
path=db_path,
|
||||
)
|
||||
statuses = ("pending", "submitted", "running", "failed", "expired", "cancelled")
|
||||
for index, status in enumerate(statuses, 1):
|
||||
source = image_studio.add_asset(
|
||||
project.id,
|
||||
image_studio.ASSET_KIND_ORIGINAL,
|
||||
source_order=index * 2 - 1,
|
||||
path=db_path,
|
||||
)
|
||||
reference = image_studio.add_asset(
|
||||
project.id,
|
||||
image_studio.ASSET_KIND_ORIGINAL,
|
||||
source_order=index * 2,
|
||||
path=db_path,
|
||||
)
|
||||
job = image_studio.create_job(
|
||||
project.id,
|
||||
source_asset_id=source.id,
|
||||
reference_asset_ids=[reference.id],
|
||||
path=db_path,
|
||||
)
|
||||
if status in {"submitted", "running"}:
|
||||
job = image_studio.set_job_submitted(
|
||||
job.id,
|
||||
"task-%d" % index,
|
||||
path=db_path,
|
||||
)
|
||||
if status == "running":
|
||||
job = image_studio.update_job_status(job.id, "running", path=db_path)
|
||||
elif status in {"failed", "expired", "cancelled"}:
|
||||
job = image_studio.update_job_status(
|
||||
job.id,
|
||||
status,
|
||||
recovery_action=image_studio.JOB_RECOVERY_REGENERATE,
|
||||
path=db_path,
|
||||
)
|
||||
self.assertEqual(status, job.status)
|
||||
with self.assertRaisesRegex(db.DbError, "可继续处理的生成任务引用"):
|
||||
image_studio.remove_original_assets_if_unused(
|
||||
project.id,
|
||||
[reference.id],
|
||||
path=db_path,
|
||||
)
|
||||
self.assertIsNotNone(image_studio.get_asset(reference.id, path=db_path))
|
||||
|
||||
self.assert_removed(temp_dir)
|
||||
|
||||
def test_remove_original_assets_blocks_invalid_reference_snapshot(self):
|
||||
with self.make_temp_dir() as temp_dir:
|
||||
db_path = os.path.join(temp_dir, "cmshopee.db")
|
||||
db.init_db(db_path)
|
||||
project = image_studio.create_or_get_project(
|
||||
account_alias="alias",
|
||||
account_slug="alias_slug",
|
||||
item_id="51100639510",
|
||||
path=db_path,
|
||||
)
|
||||
source = image_studio.add_asset(
|
||||
project.id,
|
||||
image_studio.ASSET_KIND_ORIGINAL,
|
||||
path=db_path,
|
||||
)
|
||||
job = image_studio.create_job(
|
||||
project.id,
|
||||
source_asset_id=source.id,
|
||||
path=db_path,
|
||||
)
|
||||
job = image_studio.update_job_status(job.id, "succeeded", path=db_path)
|
||||
conn = db.connect(db_path)
|
||||
try:
|
||||
with conn:
|
||||
conn.execute(
|
||||
"UPDATE image_studio_jobs SET reference_asset_ids = ? WHERE id = ?",
|
||||
("{坏快照", job.id),
|
||||
)
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
with self.assertRaisesRegex(db.DbError, "参考图快照无效"):
|
||||
image_studio.remove_original_assets_if_unused(
|
||||
project.id,
|
||||
[source.id],
|
||||
path=db_path,
|
||||
)
|
||||
self.assertIsNotNone(image_studio.get_asset(source.id, path=db_path))
|
||||
|
||||
self.assert_removed(temp_dir)
|
||||
|
||||
def test_sync_original_asset_urls_is_idempotent_and_marks_missing(self):
|
||||
with self.make_temp_dir() as temp_dir:
|
||||
db_path = os.path.join(temp_dir, "cmshopee.db")
|
||||
|
||||
Reference in New Issue
Block a user