Skip to content

Add test coverage for optional BigQuery dependency #1027

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions libs/community/langchain_google_community/bigquery.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
from __future__ import annotations

from typing import TYPE_CHECKING, List, Optional
from typing import TYPE_CHECKING, List, Optional, Any

from langchain_core.document_loaders import BaseLoader
from langchain_core.documents import Document
from langchain_core.utils import guard_import

from langchain_google_community._utils import get_client_info

if TYPE_CHECKING:
from google.auth.credentials import Credentials # type: ignore[import]


def import_bigquery() -> Any:
"""Import the google-cloud-bigquery library and return the client."""
return guard_import(
"google.cloud.bigquery", pip_name="langchain-google-community[bigquery]"
)


class BigQueryLoader(BaseLoader):
"""Load from the Google Cloud Platform `BigQuery`.

Expand Down Expand Up @@ -44,6 +52,7 @@ def __init__(
(`google.auth.compute_engine.Credentials`) or Service Account
(`google.oauth2.service_account.Credentials`) credentials directly.
"""
import_bigquery()
self.query = query
self.project = project
self.page_content_columns = page_content_columns
Expand Down Expand Up @@ -92,4 +101,4 @@ def load(self) -> List[Document]:
doc = Document(page_content=page_content, metadata=metadata)
docs.append(doc)

return docs
return docs
24 changes: 24 additions & 0 deletions libs/community/tests/unit_tests/test_bigquery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Unit tests for the BigQueryLoader."""
import pytest
from unittest.mock import patch

from langchain_google_community.bigquery import BigQueryLoader


@patch("langchain_google_community.bigquery.import_bigquery")
def test_bigquery_loader_import_error(mock_import_bigquery):
"""Test that BigQueryLoader raises an ImportError if google-cloud-bigquery is not installed."""
# Simulate the ImportError that occurs when the dependency is missing
mock_import_bigquery.side_effect = ImportError(
"Could not import google.cloud.bigquery python package. "
"Please, install it with `pip install langchain-google-community[bigquery]`"
)

with pytest.raises(ImportError) as excinfo:
BigQueryLoader(query="SELECT 1")

assert "Could not import google.cloud.bigquery python package." in str(excinfo.value)
assert (
"Please, install it with `pip install langchain-google-community[bigquery]`"
in str(excinfo.value)
)