Files
life-echo/api/tests/test_memoir_image_schema.py
2026-03-20 15:15:35 +08:00

88 lines
2.9 KiB
Python

import unittest
from app.features.memoir.memoir_images.schema import (
IMAGE_STATUS_FAILED,
IMAGE_STATUS_PENDING,
normalize_image_asset,
)
class MemoirImageSchemaTest(unittest.TestCase):
def test_normalize_image_asset_coerces_invalid_status_to_failed(self):
asset = normalize_image_asset(
{
"index": 0,
"placeholder": "{{IMAGE:南方小镇的青石板路}}",
"description": "南方小镇的青石板路",
"status": "mystery",
"url": "https://cos.example.com/0.png",
}
)
self.assertEqual(asset["status"], IMAGE_STATUS_FAILED)
self.assertEqual(asset["error"], "invalid image status: mystery")
def test_normalize_image_asset_requires_placeholder_and_description(self):
asset = normalize_image_asset(
{
"index": 0,
"status": IMAGE_STATUS_PENDING,
}
)
self.assertIsNone(asset)
def test_normalize_image_asset_completed_without_inline_placeholder(self):
asset = normalize_image_asset(
{
"index": 0,
"placeholder": "",
"description": "章节封面",
"status": "completed",
"url": "https://cos.example.com/cover.png",
}
)
self.assertIsNotNone(asset)
self.assertEqual(asset["placeholder"], "")
self.assertEqual(asset["description"], "章节封面")
def test_normalize_image_asset_completed_defaults_description(self):
asset = normalize_image_asset(
{
"status": "completed",
"url": "https://cos.example.com/x.png",
}
)
self.assertIsNotNone(asset)
self.assertEqual(asset["placeholder"], "")
self.assertEqual(asset["description"], "插图")
def test_normalize_image_asset_preserves_retryable_for_failed_assets(self):
asset = normalize_image_asset(
{
"index": 0,
"placeholder": "{{IMAGE:南方小镇的青石板路}}",
"description": "南方小镇的青石板路",
"status": IMAGE_STATUS_FAILED,
"error": "upload denied",
"retryable": False,
}
)
self.assertEqual(asset["status"], IMAGE_STATUS_FAILED)
self.assertFalse(asset["retryable"])
def test_normalize_image_asset_clears_retryable_for_non_failed_assets(self):
asset = normalize_image_asset(
{
"index": 0,
"placeholder": "{{IMAGE:南方小镇的青石板路}}",
"description": "南方小镇的青石板路",
"status": IMAGE_STATUS_PENDING,
"retryable": True,
}
)
self.assertEqual(asset["status"], IMAGE_STATUS_PENDING)
self.assertIsNone(asset["retryable"])