Files
life-echo/api/tests/test_memoir_image_bootstrap.py
Sully 2eb066dbec 把“章节正文 + 图片”从 chapters 单表/JSON 结构,重构为“章节 chapter + 段落 section + 图片 memoir_images 独立表”的新数据模型,同时联动修改接口、PDF 导出、异步任务、迁移脚本、测试,以及修复 Android 端聊天列表显示问题。 (#9)
* refactor: 表结构重构,新增段落section和图片image新表

* fix: fix android app import error

* refactor: 重构文件名

* fix: 优化提示词

* fix: 消息气泡显示位置异常问题

---------

Co-authored-by: yangshilin <2157598560@qq.com>
2026-03-13 11:12:10 +08:00

100 lines
4.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import os
import unittest
import unittest.mock
from api.tasks.memoir_tasks import initialize_chapter_images
class MemoirImageBootstrapTest(unittest.TestCase):
def test_initialize_chapter_images_keeps_only_completed_assets_when_disabled(self):
"""图片初始化已迁移到 _save_narrative_to_sections此处为兼容 no-op直接返回 []"""
chapter = type(
"ChapterStub",
(),
{
"id": "chapter-1",
"title": "童年的夏天",
"category": "childhood",
},
)()
with unittest.mock.patch.dict(os.environ, {"MEMOIR_IMAGE_ENABLED": "false"}, clear=False):
assets = initialize_chapter_images(chapter)
self.assertEqual(assets, [])
def test_initialize_chapter_images_sets_pending_assets_when_enabled(self):
"""图片初始化已迁移到 _save_narrative_to_sections此处为兼容 no-op"""
chapter = type(
"ChapterStub",
(),
{
"id": "chapter-1",
"title": "童年的夏天",
"category": "childhood",
},
)()
with unittest.mock.patch.dict(os.environ, {"MEMOIR_IMAGE_ENABLED": "true"}, clear=False):
assets = initialize_chapter_images(chapter)
self.assertEqual(assets, [])
def test_initialize_chapter_images_preserves_completed_assets_and_adds_only_new_placeholders(self):
"""图片初始化已迁移到 _save_narrative_to_sections此处为兼容 no-op"""
chapter = type("ChapterStub", (), {"id": "chapter-1", "title": "童年的夏天", "category": "childhood"})()
with unittest.mock.patch.dict(os.environ, {"MEMOIR_IMAGE_ENABLED": "true"}, clear=False):
assets = initialize_chapter_images(chapter)
self.assertEqual(assets, [])
def test_initialize_chapter_images_accepts_double_brace_placeholders(self):
"""图片初始化已迁移到 _save_narrative_to_sections此处为兼容 no-op"""
chapter = type("ChapterStub", (), {"id": "chapter-1", "title": "童年的夏天", "category": "childhood"})()
with unittest.mock.patch.dict(os.environ, {"MEMOIR_IMAGE_ENABLED": "true"}, clear=False):
assets = initialize_chapter_images(chapter)
self.assertEqual(assets, [])
def test_initialize_chapter_images_normalizes_invalid_existing_asset_status(self):
"""图片初始化已迁移到 _save_narrative_to_sections此处为兼容 no-op"""
chapter = type("ChapterStub", (), {"id": "chapter-1", "title": "童年的夏天", "category": "childhood"})()
with unittest.mock.patch.dict(os.environ, {"MEMOIR_IMAGE_ENABLED": "true"}, clear=False):
assets = initialize_chapter_images(chapter)
self.assertEqual(assets, [])
def test_initialize_chapter_images_preserves_existing_completed_assets_beyond_effective_max(self):
"""图片初始化已迁移到 _save_narrative_to_sections此处为兼容 no-op"""
chapter = type("ChapterStub", (), {"id": "chapter-1", "title": "童年的夏天", "category": "childhood"})()
with unittest.mock.patch.dict(
os.environ,
{"MEMOIR_IMAGE_ENABLED": "true", "MEMOIR_IMAGE_MAX_PER_CHAPTER": "2"},
clear=False,
):
assets = initialize_chapter_images(chapter)
self.assertEqual(assets, [])
def test_initialize_chapter_images_increases_limit_for_long_content(self):
"""图片初始化已迁移到 _save_narrative_to_sections此处为兼容 no-op"""
chapter = type("ChapterStub", (), {"id": "chapter-1", "title": "童年的夏天", "category": "childhood"})()
with unittest.mock.patch.dict(os.environ, {"MEMOIR_IMAGE_ENABLED": "true"}, clear=False):
assets = initialize_chapter_images(chapter)
self.assertEqual(assets, [])
def test_initialize_chapter_images_caps_dynamic_limit_at_max_images_cap(self):
"""图片初始化已迁移到 _save_narrative_to_sections此处为兼容 no-op"""
chapter = type("ChapterStub", (), {"id": "chapter-1", "title": "童年的夏天", "category": "childhood"})()
with unittest.mock.patch.dict(os.environ, {"MEMOIR_IMAGE_ENABLED": "true"}, clear=False):
assets = initialize_chapter_images(chapter)
self.assertEqual(assets, [])