|
|
|
@ -4,11 +4,12 @@ from typing import Any
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
from langchain.document_loaders import SitemapLoader
|
|
|
|
|
from langchain.document_loaders.sitemap import _extract_scheme_and_domain
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_sitemap() -> None:
|
|
|
|
|
"""Test sitemap loader."""
|
|
|
|
|
loader = SitemapLoader("https://langchain.readthedocs.io/sitemap.xml")
|
|
|
|
|
loader = SitemapLoader("https://api.python.langchain.com/sitemap.xml")
|
|
|
|
|
documents = loader.load()
|
|
|
|
|
assert len(documents) > 1
|
|
|
|
|
assert "LangChain Python API" in documents[0].page_content
|
|
|
|
@ -17,7 +18,7 @@ def test_sitemap() -> None:
|
|
|
|
|
def test_sitemap_block() -> None:
|
|
|
|
|
"""Test sitemap loader."""
|
|
|
|
|
loader = SitemapLoader(
|
|
|
|
|
"https://langchain.readthedocs.io/sitemap.xml", blocksize=1, blocknum=1
|
|
|
|
|
"https://api.python.langchain.com/sitemap.xml", blocksize=1, blocknum=1
|
|
|
|
|
)
|
|
|
|
|
documents = loader.load()
|
|
|
|
|
assert len(documents) == 1
|
|
|
|
@ -27,7 +28,7 @@ def test_sitemap_block() -> None:
|
|
|
|
|
def test_sitemap_block_only_one() -> None:
|
|
|
|
|
"""Test sitemap loader."""
|
|
|
|
|
loader = SitemapLoader(
|
|
|
|
|
"https://langchain.readthedocs.io/sitemap.xml", blocksize=1000000, blocknum=0
|
|
|
|
|
"https://api.python.langchain.com/sitemap.xml", blocksize=1000000, blocknum=0
|
|
|
|
|
)
|
|
|
|
|
documents = loader.load()
|
|
|
|
|
assert len(documents) > 1
|
|
|
|
@ -37,7 +38,7 @@ def test_sitemap_block_only_one() -> None:
|
|
|
|
|
def test_sitemap_block_blocknum_default() -> None:
|
|
|
|
|
"""Test sitemap loader."""
|
|
|
|
|
loader = SitemapLoader(
|
|
|
|
|
"https://langchain.readthedocs.io/sitemap.xml", blocksize=1000000
|
|
|
|
|
"https://api.python.langchain.com/sitemap.xml", blocksize=1000000
|
|
|
|
|
)
|
|
|
|
|
documents = loader.load()
|
|
|
|
|
assert len(documents) > 1
|
|
|
|
@ -47,14 +48,14 @@ def test_sitemap_block_blocknum_default() -> None:
|
|
|
|
|
def test_sitemap_block_size_to_small() -> None:
|
|
|
|
|
"""Test sitemap loader."""
|
|
|
|
|
with pytest.raises(ValueError, match="Sitemap blocksize should be at least 1"):
|
|
|
|
|
SitemapLoader("https://langchain.readthedocs.io/sitemap.xml", blocksize=0)
|
|
|
|
|
SitemapLoader("https://api.python.langchain.com/sitemap.xml", blocksize=0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_sitemap_block_num_to_small() -> None:
|
|
|
|
|
"""Test sitemap loader."""
|
|
|
|
|
with pytest.raises(ValueError, match="Sitemap blocknum can not be lower then 0"):
|
|
|
|
|
SitemapLoader(
|
|
|
|
|
"https://langchain.readthedocs.io/sitemap.xml",
|
|
|
|
|
"https://api.python.langchain.com/sitemap.xml",
|
|
|
|
|
blocksize=1000000,
|
|
|
|
|
blocknum=-1,
|
|
|
|
|
)
|
|
|
|
@ -63,7 +64,7 @@ def test_sitemap_block_num_to_small() -> None:
|
|
|
|
|
def test_sitemap_block_does_not_exists() -> None:
|
|
|
|
|
"""Test sitemap loader."""
|
|
|
|
|
loader = SitemapLoader(
|
|
|
|
|
"https://langchain.readthedocs.io/sitemap.xml", blocksize=1000000, blocknum=15
|
|
|
|
|
"https://api.python.langchain.com/sitemap.xml", blocksize=1000000, blocknum=15
|
|
|
|
|
)
|
|
|
|
|
with pytest.raises(
|
|
|
|
|
ValueError,
|
|
|
|
@ -75,7 +76,7 @@ def test_sitemap_block_does_not_exists() -> None:
|
|
|
|
|
def test_filter_sitemap() -> None:
|
|
|
|
|
"""Test sitemap loader."""
|
|
|
|
|
loader = SitemapLoader(
|
|
|
|
|
"https://langchain.readthedocs.io/sitemap.xml",
|
|
|
|
|
"https://api.python.langchain.com/sitemap.xml",
|
|
|
|
|
filter_urls=["https://api.python.langchain.com/en/stable/"],
|
|
|
|
|
)
|
|
|
|
|
documents = loader.load()
|
|
|
|
@ -89,7 +90,7 @@ def test_sitemap_metadata() -> None:
|
|
|
|
|
|
|
|
|
|
"""Test sitemap loader."""
|
|
|
|
|
loader = SitemapLoader(
|
|
|
|
|
"https://langchain.readthedocs.io/sitemap.xml",
|
|
|
|
|
"https://api.python.langchain.com/sitemap.xml",
|
|
|
|
|
meta_function=sitemap_metadata_one,
|
|
|
|
|
)
|
|
|
|
|
documents = loader.load()
|
|
|
|
@ -107,7 +108,7 @@ def test_sitemap_metadata_extraction() -> None:
|
|
|
|
|
|
|
|
|
|
"""Test sitemap loader."""
|
|
|
|
|
loader = SitemapLoader(
|
|
|
|
|
"https://langchain.readthedocs.io/sitemap.xml",
|
|
|
|
|
"https://api.python.langchain.com/sitemap.xml",
|
|
|
|
|
meta_function=sitemap_metadata_two,
|
|
|
|
|
)
|
|
|
|
|
documents = loader.load()
|
|
|
|
@ -118,7 +119,7 @@ def test_sitemap_metadata_extraction() -> None:
|
|
|
|
|
|
|
|
|
|
def test_sitemap_metadata_default() -> None:
|
|
|
|
|
"""Test sitemap loader."""
|
|
|
|
|
loader = SitemapLoader("https://langchain.readthedocs.io/sitemap.xml")
|
|
|
|
|
loader = SitemapLoader("https://api.python.langchain.com/sitemap.xml")
|
|
|
|
|
documents = loader.load()
|
|
|
|
|
assert len(documents) > 1
|
|
|
|
|
assert "source" in documents[0].metadata
|
|
|
|
@ -132,3 +133,23 @@ def test_local_sitemap() -> None:
|
|
|
|
|
documents = loader.load()
|
|
|
|
|
assert len(documents) > 1
|
|
|
|
|
assert "🦜️🔗" in documents[0].page_content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_extract_domain() -> None:
|
|
|
|
|
"""Test domain extraction."""
|
|
|
|
|
assert _extract_scheme_and_domain("https://js.langchain.com/sitemap.xml") == (
|
|
|
|
|
"https",
|
|
|
|
|
"js.langchain.com",
|
|
|
|
|
)
|
|
|
|
|
assert _extract_scheme_and_domain("http://example.com/path/to/page") == (
|
|
|
|
|
"http",
|
|
|
|
|
"example.com",
|
|
|
|
|
)
|
|
|
|
|
assert _extract_scheme_and_domain("ftp://files.example.com") == (
|
|
|
|
|
"ftp",
|
|
|
|
|
"files.example.com",
|
|
|
|
|
)
|
|
|
|
|
assert _extract_scheme_and_domain("https://deep.subdomain.example.com") == (
|
|
|
|
|
"https",
|
|
|
|
|
"deep.subdomain.example.com",
|
|
|
|
|
)
|
|
|
|
|