Skip to content

Commit

Permalink
mock get and post calls within pebblo unit tests. (langchain-ai#15)
Browse files Browse the repository at this point in the history
Signed-off-by: Rahul Tripathi <rahul@clouddefense.io>
Co-authored-by: Rahul Tripathi <rahul@clouddefense.io>
  • Loading branch information
rahul-trip and Rahul Tripathi authored Jan 29, 2024
1 parent ad5c324 commit 2da9d30
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions libs/community/tests/unit_tests/document_loaders/test_pebblo.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,40 @@
import os
from pathlib import Path
from typing import Dict

from langchain_core.documents import Document
from pytest_mock import MockerFixture

from langchain_community.document_loaders import CSVLoader, PyPDFLoader

EXAMPLE_DOCS_DIRECTORY = str(Path(__file__).parent.parent.parent / "examples/")


class MockResponse:
def __init__(self, json_data: Dict, status_code: int):
self.json_data = json_data
self.status_code = status_code

def json(self) -> Dict:
return self.json_data


def test_pebblo_import() -> None:
"""Test that the Pebblo safe loader can be imported."""
from langchain_community.document_loaders import PebbloSafeLoader # noqa: F401


def test_empty_filebased_loader() -> None:
def test_empty_filebased_loader(mocker: MockerFixture) -> None:
"""Test basic file based csv loader."""
# Setup
from langchain_community.document_loaders import PebbloSafeLoader

mocker.patch.multiple(
"requests",
get=MockResponse(json_data={"data": ""}, status_code=200),
post=MockResponse( json_data={"data": ""}, status_code=200)
)

file_path = os.path.join(EXAMPLE_DOCS_DIRECTORY, "test_empty.csv")
expected_docs: list = []

Expand All @@ -34,10 +51,15 @@ def test_empty_filebased_loader() -> None:
assert result == expected_docs


def test_csv_loader_load_valid_data() -> None:
def test_csv_loader_load_valid_data(mocker: MockerFixture) -> None:
# Setup
from langchain_community.document_loaders import PebbloSafeLoader

mocker.patch.multiple(
"requests",
get=MockResponse(json_data={"data": ""}, status_code=200),
post=MockResponse( json_data={"data": ""}, status_code=200)
)
file_path = os.path.join(EXAMPLE_DOCS_DIRECTORY, "test_nominal.csv")
expected_docs = [
Document(
Expand All @@ -63,10 +85,15 @@ def test_csv_loader_load_valid_data() -> None:
assert result == expected_docs


def test_pdf_lazy_load():
def test_pdf_lazy_load(mocker: MockerFixture):
# Setup
from langchain_community.document_loaders import PebbloSafeLoader

mocker.patch.multiple(
"requests",
get=MockResponse(json_data={"data": ""}, status_code=200),
post=MockResponse( json_data={"data": ""}, status_code=200)
)
file_path = os.path.join(
EXAMPLE_DOCS_DIRECTORY, "multi-page-forms-sample-2-page.pdf"
)
Expand All @@ -82,4 +109,4 @@ def test_pdf_lazy_load():
result = list(loader.lazy_load())

# Assert
assert len(result) == 2
assert len(result) == 2

0 comments on commit 2da9d30

Please sign in to comment.