80 lines
3.0 KiB
Python
80 lines
3.0 KiB
Python
import os
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from types import SimpleNamespace
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
|
|
|
|
from app import image_paths
|
|
from app.config import make_slug
|
|
|
|
|
|
class ImagePathTests(unittest.TestCase):
|
|
def test_task_image_path_uses_batch_slug_task_and_item(self):
|
|
root = os.path.abspath(os.path.join("tmp", "images"))
|
|
task = SimpleNamespace(id=42, batch_id=7, item_id="51100639510", alias="alias-a")
|
|
account = SimpleNamespace(slug="main_shop")
|
|
|
|
path = image_paths.task_image_path(root, task, account, "new")
|
|
|
|
self.assertEqual(
|
|
os.path.join(root, "7", "main_shop", "42_51100639510_new.jpg"),
|
|
path,
|
|
)
|
|
|
|
def test_task_image_path_falls_back_to_account_alias_slug(self):
|
|
root = os.path.abspath(os.path.join("tmp", "images"))
|
|
task = {"id": 5, "batch_id": 3, "item_id": "ITEM/1", "alias": "alias-a"}
|
|
|
|
path = image_paths.task_image_path(root, task, None, "old")
|
|
|
|
self.assertEqual(
|
|
os.path.join(root, "3", make_slug("alias-a"), "5_ITEM_1_old.jpg"),
|
|
path,
|
|
)
|
|
|
|
def test_list_task_cover_candidates_filters_and_sorts_generated_covers(self):
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
task = SimpleNamespace(id=42, batch_id="batch-a", item_id="51100639510", alias="alias-a")
|
|
canonical = image_paths.task_image_path(temp_dir, task, suffix="new")
|
|
directory = os.path.dirname(canonical)
|
|
prefix = os.path.splitext(os.path.basename(canonical))[0]
|
|
os.makedirs(directory, exist_ok=True)
|
|
filenames = [
|
|
f"{prefix}_20260709094800.jpg",
|
|
f"{prefix}_20260709094800_2.jpg",
|
|
f"{prefix}_20260709094900.jpg",
|
|
f"{prefix}.jpg",
|
|
f"{prefix}_bad.jpg",
|
|
f"{prefix.replace('_new', '_old')}.jpg",
|
|
f"{prefix}.png",
|
|
"99_51100639510_new.jpg",
|
|
]
|
|
for filename in filenames:
|
|
with open(os.path.join(directory, filename), "wb") as fh:
|
|
fh.write(b"jpeg")
|
|
|
|
paths = image_paths.list_task_cover_candidates(temp_dir, task)
|
|
|
|
self.assertEqual(
|
|
[
|
|
canonical,
|
|
os.path.join(directory, f"{prefix}_20260709094900.jpg"),
|
|
os.path.join(directory, f"{prefix}_20260709094800_2.jpg"),
|
|
os.path.join(directory, f"{prefix}_20260709094800.jpg"),
|
|
os.path.join(directory, f"{prefix}_bad.jpg"),
|
|
],
|
|
paths,
|
|
)
|
|
|
|
def test_list_task_cover_candidates_returns_empty_for_missing_directory(self):
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
task = SimpleNamespace(id=42, batch_id="batch-a", item_id="51100639510", alias="alias-a")
|
|
|
|
self.assertEqual([], image_paths.list_task_cover_candidates(temp_dir, task))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|