Skip to content

Commit 158b844

Browse files
committed
Cleaned up closing of graphstore and clients in tests.
1 parent 65c4084 commit 158b844

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed

libs/langchain-mongodb/tests/integration_tests/test_graph_large.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
from typing import Generator
1414

1515
import pytest
16+
from langchain_core.documents import Document
1617
from langchain_core.language_models.chat_models import BaseChatModel
18+
from langchain_core.messages import AIMessage
1719
from langchain_openai import AzureChatOpenAI, ChatOpenAI
1820
from langchain_text_splitters import RecursiveCharacterTextSplitter
1921
from pymongo import MongoClient
@@ -85,10 +87,8 @@ def graph_store(
8587
collection: Collection,
8688
entity_extraction_model: BaseChatModel,
8789
text_chunks: list[str],
88-
) -> MongoDBGraphStore:
90+
) -> Generator[MongoDBGraphStore, None, None]:
8991
"""Create MongoDBGraphStore and populate it with entities from text chunks."""
90-
from langchain_core.documents import Document
91-
9292
# Create the graph store
9393
store = MongoDBGraphStore(
9494
collection=collection,
@@ -105,7 +105,8 @@ def graph_store(
105105
# Verify that we processed all chunks
106106
assert len(bulkwrite_results) == len(documents)
107107

108-
return store
108+
yield store
109+
store.close()
109110

110111

111112
def test_large_graph_entity_count(graph_store: MongoDBGraphStore):
@@ -257,8 +258,6 @@ def test_chunk_count(text_chunks: list[str]):
257258

258259
def test_graph_store_chat_response(graph_store: MongoDBGraphStore):
259260
"""Test the chat response functionality with the large graph."""
260-
from langchain_core.messages import AIMessage
261-
262261
query = (
263262
"Tell me about the AI Ethics Initiative and which companies are supporting it."
264263
)

libs/langchain-mongodb/tests/integration_tests/test_graphrag.py

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,27 @@ def test_additional_entity_examples(entity_extraction_model, entity_example, doc
201201
assert len(new_entities) >= 2
202202

203203

204+
def test_chat_response(graph_store, query_connection):
205+
"""Displays querying an existing Knowledge Graph Database"""
206+
answer = graph_store.chat_response(query_connection)
207+
assert isinstance(answer, AIMessage)
208+
assert "acme corporation" in answer.content.lower()
209+
210+
211+
def test_similarity_search(graph_store, query_connection):
212+
docs = graph_store.similarity_search(query_connection)
213+
assert len(docs) >= 4
214+
assert all({"_id", "type", "relationships"}.issubset(set(d.keys())) for d in docs)
215+
assert any("depth" in d.keys() for d in docs)
216+
assert any("attributes" in d.keys() for d in docs)
217+
218+
204219
def test_validator(documents, entity_extraction_model):
205220
# Case 1. No existing collection.
221+
client = MongoClient(CONNECTION_STRING)
206222
clxn_name = f"{COLLECTION_NAME}_validation"
207-
208-
with MongoClient(CONNECTION_STRING) as client:
209-
client[DB_NAME][clxn_name].drop()
210-
223+
client[DB_NAME][clxn_name].drop()
224+
client.close()
211225
# now we call with validation that can be added without db admin privileges
212226
store = MongoDBGraphStore(
213227
connection_string=CONNECTION_STRING,
@@ -229,15 +243,16 @@ def test_validator(documents, entity_extraction_model):
229243
collection = client[DB_NAME][clxn_name]
230244
collection.delete_many({})
231245

232-
store = MongoDBGraphStore(
233-
collection=collection,
234-
entity_extraction_model=entity_extraction_model,
235-
validate=True,
236-
validation_action="error",
237-
)
238-
bulkwrite_results = store.add_documents(documents)
239-
assert len(bulkwrite_results) == len(documents)
240-
collection.delete_many({})
246+
store = MongoDBGraphStore(
247+
collection=collection,
248+
entity_extraction_model=entity_extraction_model,
249+
validate=True,
250+
validation_action="error",
251+
)
252+
bulkwrite_results = store.add_documents(documents)
253+
assert len(bulkwrite_results) == len(documents)
254+
collection.drop()
255+
store.close()
241256

242257
# Case 3: Existing collection without a validator
243258
with MongoClient(CONNECTION_STRING) as client:

0 commit comments

Comments
 (0)