2023-08-08 18:26:03 +00:00
|
|
|
"""Integration test for PubMed API Wrapper."""
|
2024-02-10 00:13:30 +00:00
|
|
|
|
2023-08-08 18:26:03 +00:00
|
|
|
from typing import List
|
|
|
|
|
|
|
|
import pytest
|
2023-11-21 16:35:29 +00:00
|
|
|
from langchain_core.documents import Document
|
2023-08-08 18:26:03 +00:00
|
|
|
|
2023-12-11 21:53:30 +00:00
|
|
|
from langchain_community.document_loaders import PubMedLoader
|
2023-08-08 18:26:03 +00:00
|
|
|
|
|
|
|
xmltodict = pytest.importorskip("xmltodict")
|
|
|
|
|
|
|
|
|
|
|
|
def test_load_success() -> None:
|
|
|
|
"""Test that returns the correct answer"""
|
|
|
|
api_client = PubMedLoader(query="chatgpt")
|
|
|
|
docs = api_client.load()
|
2024-02-10 00:13:30 +00:00
|
|
|
print(docs) # noqa: T201
|
2023-08-08 18:26:03 +00:00
|
|
|
assert len(docs) == api_client.load_max_docs == 3
|
|
|
|
assert_docs(docs)
|
|
|
|
|
|
|
|
|
|
|
|
def test_load_success_load_max_docs() -> None:
|
|
|
|
"""Test that returns the correct answer"""
|
|
|
|
api_client = PubMedLoader(query="chatgpt", load_max_docs=2)
|
|
|
|
docs = api_client.load()
|
2024-02-10 00:13:30 +00:00
|
|
|
print(docs) # noqa: T201
|
2023-08-08 18:26:03 +00:00
|
|
|
assert len(docs) == api_client.load_max_docs == 2
|
|
|
|
assert_docs(docs)
|
|
|
|
|
|
|
|
|
|
|
|
def test_load_returns_no_result() -> None:
|
|
|
|
"""Test that gives no result."""
|
|
|
|
api_client = PubMedLoader(query="1605.08386WWW")
|
|
|
|
docs = api_client.load()
|
|
|
|
assert len(docs) == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_load_no_content() -> None:
|
|
|
|
"""Returns a Document without content."""
|
|
|
|
api_client = PubMedLoader(query="37548971")
|
|
|
|
docs = api_client.load()
|
2024-02-10 00:13:30 +00:00
|
|
|
print(docs) # noqa: T201
|
2023-08-08 18:26:03 +00:00
|
|
|
assert len(docs) > 0
|
|
|
|
assert docs[0].page_content == ""
|
|
|
|
|
|
|
|
|
|
|
|
def assert_docs(docs: List[Document]) -> None:
|
|
|
|
for doc in docs:
|
|
|
|
assert doc.metadata
|
|
|
|
assert set(doc.metadata) == {
|
|
|
|
"Copyright Information",
|
|
|
|
"uid",
|
|
|
|
"Title",
|
|
|
|
"Published",
|
|
|
|
}
|