2023-04-01 19:48:27 +00:00
|
|
|
import sys
|
2023-03-17 04:47:17 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
2023-04-01 19:48:27 +00:00
|
|
|
import pytest
|
|
|
|
|
2023-03-17 04:47:17 +00:00
|
|
|
from langchain.document_loaders.html_bs import BSHTMLLoader
|
|
|
|
|
2023-05-18 02:39:11 +00:00
|
|
|
HERE = Path(__file__).parent
|
|
|
|
EXAMPLES = HERE.parent.parent / "integration_tests" / "examples"
|
2023-03-17 04:47:17 +00:00
|
|
|
|
2023-05-18 02:39:11 +00:00
|
|
|
|
|
|
|
@pytest.mark.requires("bs4", "lxml")
|
2023-03-17 04:47:17 +00:00
|
|
|
def test_bs_html_loader() -> None:
|
|
|
|
"""Test unstructured loader."""
|
2023-05-18 02:39:11 +00:00
|
|
|
file_path = EXAMPLES / "example.html"
|
2023-04-26 23:10:16 +00:00
|
|
|
loader = BSHTMLLoader(str(file_path), get_text_separator="|")
|
2023-03-17 04:47:17 +00:00
|
|
|
docs = loader.load()
|
|
|
|
|
|
|
|
assert len(docs) == 1
|
|
|
|
|
|
|
|
metadata = docs[0].metadata
|
2023-04-26 23:10:16 +00:00
|
|
|
content = docs[0].page_content
|
2023-03-17 04:47:17 +00:00
|
|
|
|
|
|
|
assert metadata["title"] == "Chew dad's slippers"
|
|
|
|
assert metadata["source"] == str(file_path)
|
2023-04-26 23:10:16 +00:00
|
|
|
assert content[:2] == "\n|"
|
2023-04-01 19:48:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
bool(sys.flags.utf8_mode) or not sys.platform.startswith("win"),
|
|
|
|
reason="default encoding is utf8",
|
|
|
|
)
|
2023-05-18 02:39:11 +00:00
|
|
|
@pytest.mark.requires("bs4", "lxml")
|
2023-04-01 19:48:27 +00:00
|
|
|
def test_bs_html_loader_non_utf8() -> None:
|
|
|
|
"""Test providing encoding to BSHTMLLoader."""
|
2023-05-18 02:39:11 +00:00
|
|
|
file_path = EXAMPLES / "example-utf8.html"
|
2023-04-01 19:48:27 +00:00
|
|
|
|
|
|
|
with pytest.raises(UnicodeDecodeError):
|
|
|
|
BSHTMLLoader(str(file_path)).load()
|
|
|
|
|
|
|
|
loader = BSHTMLLoader(str(file_path), open_encoding="utf8")
|
|
|
|
docs = loader.load()
|
|
|
|
|
|
|
|
assert len(docs) == 1
|
|
|
|
|
|
|
|
metadata = docs[0].metadata
|
|
|
|
|
|
|
|
assert metadata["title"] == "Chew dad's slippers"
|
|
|
|
assert metadata["source"] == str(file_path)
|