Files
epub_bilingual_translator/archive/v0.09/tests/test_parser.py
T
谭凯 7a93c52b42 feat: Release v0.10 - Modular Architecture & External Config
- Refactor codebase into src/ (preprocessing, translation, assembly)
- Add pipeline/ scripts for individual stages
- Externalize configuration to config/config.yaml
- Fix Cover Image preservation
- Update documentation and manuals
2026-01-31 22:49:44 +08:00

150 lines
4.8 KiB
Python

"""
EPUB 解析器测试
"""
import pytest
import tempfile
import os
from pathlib import Path
from ebooklib import epub
from src.epub_parser import EPUBParser
class TestEPUBParser:
"""EPUB 解析器测试类"""
@pytest.fixture
def sample_epub(self):
"""创建测试用的 EPUB 文件"""
# 创建临时 EPUB 文件
with tempfile.NamedTemporaryFile(suffix='.epub', delete=False) as tmp_file:
# 创建简单的 EPUB
book = epub.EpubBook()
book.set_identifier('test123')
book.set_title('Test Book')
book.set_language('en')
book.add_author('Test Author')
# 添加章节
c1 = epub.EpubHtml(
title='Chapter 1',
file_name='chap_01.xhtml',
lang='en'
)
c1.content = '''
<html>
<head><title>Chapter 1</title></head>
<body>
<h1>Chapter 1</h1>
<p>This is the first paragraph of the first chapter.</p>
<p>This is the second paragraph with more content to test parsing.</p>
</body>
</html>
'''
book.add_item(c1)
# 添加序言
preface = epub.EpubHtml(
title='Preface',
file_name='preface.xhtml',
lang='en'
)
preface.content = '''
<html>
<head><title>Preface</title></head>
<body>
<h1>Preface</h1>
<p>This is the preface of the book.</p>
<p>It contains important background information.</p>
</body>
</html>
'''
book.add_item(preface)
# 设置目录
book.toc = (
epub.Link("preface.xhtml", "Preface", "preface"),
epub.Link("chap_01.xhtml", "Chapter 1", "chap_01"),
)
book.add_item(epub.EpubNcx())
book.add_item(epub.EpubNav())
book.spine = ['nav', preface, c1]
# 写入文件
epub.write_epub(tmp_file.name, book, {})
yield tmp_file.name
# 清理
os.unlink(tmp_file.name)
def test_parser_initialization(self, sample_epub):
"""测试解析器初始化"""
parser = EPUBParser(sample_epub)
assert parser.epub_path.exists()
assert parser.book is not None
assert parser.metadata['title'] == 'Test Book'
assert parser.metadata['author'] == 'Test Author'
def test_extract_metadata(self, sample_epub):
"""测试元数据提取"""
parser = EPUBParser(sample_epub)
assert parser.metadata['title'] == 'Test Book'
assert parser.metadata['author'] == 'Test Author'
assert parser.metadata['language'] == 'en'
def test_parse_toc(self, sample_epub):
"""测试目录解析"""
parser = EPUBParser(sample_epub)
assert parser.toc_structure['preface'] is not None
assert len(parser.toc_structure['chapters']) >= 1
assert parser.toc_structure['preface']['title'] == 'Preface'
def test_extract_translatable_content(self, sample_epub):
"""测试可翻译内容提取"""
parser = EPUBParser(sample_epub)
content_items = parser.extract_translatable_content()
assert len(content_items) >= 1
assert any(item['type'] == 'preface' for item in content_items)
assert any(item['type'] == 'chapter' for item in content_items)
def test_get_preface_content(self, sample_epub):
"""测试序言内容获取"""
parser = EPUBParser(sample_epub)
preface_text = parser.get_preface_content()
assert len(preface_text) > 0
assert 'preface' in preface_text.lower()
assert 'background information' in preface_text
def test_sample_content_for_prompt(self, sample_epub):
"""测试内容采样"""
parser = EPUBParser(sample_epub)
samples = parser.sample_content_for_prompt(ratio=0.5)
assert isinstance(samples, list)
assert len(samples) >= 0
def test_get_book_info(self, sample_epub):
"""测试书籍信息获取"""
parser = EPUBParser(sample_epub)
book_info = parser.get_book_info()
assert 'title' in book_info
assert 'author' in book_info
assert 'chapter_count' in book_info
assert book_info['title'] == 'Test Book'
assert book_info['has_preface'] is True
def test_nonexistent_file(self):
"""测试不存在的文件"""
with pytest.raises(FileNotFoundError):
EPUBParser('nonexistent.epub')