Files
md-vector-db/tests/test_splitters_html.py
T

43 lines
1.6 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.
"""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 = "<html><body><h1>标题</h1><p>这是段落内容。</p><p>第二段。</p></body></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 = """<html><head><style>.a{color:red}</style><script>alert('xss')</script></head>
<body><p>可见内容。</p></body></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("<html></html>") == []
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")