From b8afc9043d614cccd441c02c194bd930049cd5da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E8=88=AA=E5=AE=87?= <3364451258@qq.com> Date: Fri, 10 Jul 2026 14:25:40 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E6=B7=BB=E5=8A=A0=20PDFSplitter=20?= =?UTF-8?q?=E5=92=8C=20HTMLSplitter=20=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_splitters_html.py | 42 +++++++++++++++++++++++++++++++ tests/test_splitters_pdf.py | 49 ++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 tests/test_splitters_html.py create mode 100644 tests/test_splitters_pdf.py diff --git a/tests/test_splitters_html.py b/tests/test_splitters_html.py new file mode 100644 index 0000000..977e18b --- /dev/null +++ b/tests/test_splitters_html.py @@ -0,0 +1,42 @@ +"""HTMLSplitter 测试.""" +import pytest + +bs4 = pytest.importorskip("bs4", reason="beautifulsoup4 未安装") + + +class TestHTMLSplitter: + """HTMLSplitter 测试(需 beautifulsoup4).""" + + def test_split_simple_html(self): + from src.core.splitters.html import HTMLSplitter + html = "

标题

这是段落内容。

第二段。

" + s = HTMLSplitter(max_size=500) + chunks = s.split(html, source_file="test.html") + assert len(chunks) >= 1 + all_text = "".join(c["content"] for c in chunks) + assert "标题" in all_text + assert "段落内容" in all_text + assert "第二段" in all_text + + def test_strips_script_and_style(self): + from src.core.splitters.html import HTMLSplitter + html = """ +

可见内容。

""" + s = HTMLSplitter() + chunks = s.split(html, source_file="test.html") + all_text = "".join(c["content"] for c in chunks) + assert "可见内容" in all_text + assert "alert" not in all_text + assert ".a{color:red}" not in all_text + + def test_empty_html(self): + from src.core.splitters.html import HTMLSplitter + s = HTMLSplitter() + assert s.split("") == [] + assert s.split("") == [] + + def test_html_has_split_method(self): + """HTMLSplitter 遵循 Splitter Protocol.""" + from src.core.splitters.html import HTMLSplitter + s = HTMLSplitter() + assert hasattr(s, "split") diff --git a/tests/test_splitters_pdf.py b/tests/test_splitters_pdf.py new file mode 100644 index 0000000..ea0af2e --- /dev/null +++ b/tests/test_splitters_pdf.py @@ -0,0 +1,49 @@ +"""PDFSplitter 测试.""" +import pytest +from pathlib import Path + +pymupdf = pytest.importorskip("fitz", reason="pymupdf 未安装") + + +class TestPDFSplitter: + """PDFSplitter 测试(需 pymupdf).""" + + def test_split_simple_pdf(self, tmp_path): + """用 pymupdf 创建一个简单 PDF 并测试分块.""" + from src.core.splitters.pdf import PDFSplitter + import fitz + + pdf_path = tmp_path / "test.pdf" + doc = fitz.open() + # 插入纯 ASCII 文本避免 CJK 字体编码问题 + doc.new_page().insert_text((72, 72), "This is PDF document content.\n\nSecond paragraph text.") + doc.save(str(pdf_path)) + doc.close() + + s = PDFSplitter(max_size=500) + chunks = s.split(str(pdf_path), source_file="test.pdf") + assert len(chunks) >= 1 + all_text = "".join(c["content"] for c in chunks) + assert "PDF document" in all_text + assert "Second paragraph" in all_text + + def test_empty_pdf(self, tmp_path): + """空 PDF(有页但无文字)返回空列表.""" + from src.core.splitters.pdf import PDFSplitter + import fitz + + pdf_path = tmp_path / "empty.pdf" + doc = fitz.open() + doc.new_page() # pymupdf 必须有至少一页才能保存 + doc.save(str(pdf_path)) + doc.close() + + s = PDFSplitter() + chunks = s.split(str(pdf_path), source_file="empty.pdf") + assert chunks == [] + + def test_pdf_has_split_method(self): + """PDFSplitter 遵循 Splitter Protocol.""" + from src.core.splitters.pdf import PDFSplitter + s = PDFSplitter() + assert hasattr(s, "split")