mirror of
https://github.com/tiennm99/DocsGPT.git
synced 2026-10-14 08:17:34 +00:00
feat: prune some deps
This commit is contained in:
1 parent
c5b372df1e
commit
a166796f6c
26 files changed
+1937
-1343
No files matched your search
@@ -1,16 +1,19 @@
|
||||
import pytest
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch, MagicMock
|
||||
|
||||
import sys
|
||||
import types
|
||||
|
||||
from application.parser.file.html_parser import HTMLParser
|
||||
|
||||
|
||||
HTML = (
|
||||
"<html><head><title>My Page</title></head>"
|
||||
"<body><h1>Heading</h1><p>Hello world.</p></body></html>"
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def html_parser():
|
||||
return HTMLParser()
|
||||
def html_file(tmp_path):
|
||||
path = tmp_path / "page.html"
|
||||
path.write_text(HTML)
|
||||
return path
|
||||
|
||||
|
||||
def test_html_init_parser():
|
||||
@@ -21,23 +24,33 @@ def test_html_init_parser():
|
||||
assert parser.parser_config_set
|
||||
|
||||
|
||||
def test_html_parser_parse_file():
|
||||
parser = HTMLParser()
|
||||
mock_doc = MagicMock()
|
||||
mock_doc.page_content = "Extracted HTML content"
|
||||
mock_doc.metadata = {"source": "test.html"}
|
||||
def test_html_parser_extracts_text(html_file):
|
||||
text = HTMLParser().parse_file(html_file)
|
||||
assert "Heading" in text
|
||||
assert "Hello world." in text
|
||||
|
||||
fake_lc = types.ModuleType("langchain_community")
|
||||
fake_dl = types.ModuleType("langchain_community.document_loaders")
|
||||
|
||||
bshtml_mock = MagicMock(return_value=MagicMock(load=MagicMock(return_value=[mock_doc])))
|
||||
fake_dl.BSHTMLLoader = bshtml_mock
|
||||
fake_lc.document_loaders = fake_dl
|
||||
def test_html_parser_returns_plain_text(html_file):
|
||||
"""bulk.py stringifies parser output, so it must not be a repr.
|
||||
|
||||
with patch.dict(sys.modules, {
|
||||
"langchain_community": fake_lc,
|
||||
"langchain_community.document_loaders": fake_dl,
|
||||
}):
|
||||
result = parser.parse_file(Path("test.html"))
|
||||
assert result == [mock_doc]
|
||||
bshtml_mock.assert_called_once_with(Path("test.html"))
|
||||
The previous langchain BSHTMLLoader returned a Document whose ``str()`` is
|
||||
``page_content='...' metadata={...}``, which leaked into indexed content.
|
||||
"""
|
||||
text = HTMLParser().parse_file(html_file)
|
||||
assert isinstance(text, str)
|
||||
assert "page_content" not in str(text)
|
||||
assert "metadata=" not in str(text)
|
||||
|
||||
|
||||
def test_html_parser_reports_title_metadata(html_file):
|
||||
assert HTMLParser().get_file_metadata(html_file) == {"title": "My Page"}
|
||||
|
||||
|
||||
def test_html_parser_metadata_without_title(tmp_path):
|
||||
path = tmp_path / "no_title.html"
|
||||
path.write_text("<html><body><p>Just text.</p></body></html>")
|
||||
assert HTMLParser().get_file_metadata(path) == {}
|
||||
|
||||
|
||||
def test_html_parser_metadata_unreadable_file(tmp_path):
|
||||
assert HTMLParser().get_file_metadata(tmp_path / "missing.html") == {}
|
||||
Reference in new issue
Block a user