|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import os |
4 | | -from typing import Generator |
| 4 | +from typing import TYPE_CHECKING, Generator |
5 | 5 |
|
6 | 6 | import pytest |
7 | 7 | from langchain_core.documents import Document |
8 | 8 | from langchain_core.language_models.chat_models import BaseChatModel |
9 | | -from langchain_core.messages import AIMessage |
10 | 9 | from langchain_openai import AzureChatOpenAI, ChatOpenAI |
11 | 10 | from pymongo import MongoClient |
12 | 11 | from pymongo.collection import Collection |
|
17 | 16 |
|
18 | 17 | from ..utils import CONNECTION_STRING, DB_NAME |
19 | 18 |
|
| 19 | +if TYPE_CHECKING: |
| 20 | + pass |
| 21 | + |
| 22 | + |
20 | 23 | COLLECTION_NAME = "langchain_test_graphrag" |
21 | 24 |
|
22 | 25 |
|
@@ -303,3 +306,39 @@ def test_allowed_relationship_types(documents, entity_extraction_model): |
303 | 306 | relationships.update(set(ent.get("relationships", {}).get("types", []))) |
304 | 307 | assert relationships == {"partner"} |
305 | 308 | store.close() |
| 309 | + |
| 310 | + |
| 311 | +@pytest.mark.viz |
| 312 | +def test_networkx(graph_store): |
| 313 | + """Basic test that MongoDBGraphStore can be exported to NetworkX Graph. |
| 314 | +
|
| 315 | + See examples for self-contained Jupyter notebook example. |
| 316 | + To run: `pytest -m viz` |
| 317 | + """ |
| 318 | + try: |
| 319 | + import networkx as nx |
| 320 | + except ImportError: |
| 321 | + pytest.skip("This test requires optional-dependency `viz`") |
| 322 | + |
| 323 | + nx_graph = graph_store.to_networkx() |
| 324 | + # assert nx_graph.number_of_nodes() == len(nx_graph.nodes) |
| 325 | + assert isinstance(nx_graph, nx.DiGraph) |
| 326 | + assert nx_graph.number_of_nodes() == graph_store.collection.count_documents({}) |
| 327 | + assert nx_graph.number_of_edges() > 0 |
| 328 | + |
| 329 | + |
| 330 | +@pytest.mark.viz |
| 331 | +def test_view(graph_store): |
| 332 | + """Basic test that MongoDBGraphStore can be exported to NetworkX Graph. |
| 333 | +
|
| 334 | + See examples for self-contained Jupyter notebook example. |
| 335 | + To run: `pytest -m viz` |
| 336 | + """ |
| 337 | + try: |
| 338 | + import holoviews as hv |
| 339 | + except ImportError: |
| 340 | + pytest.skip("This test requires optional-dependency `viz`") |
| 341 | + |
| 342 | + view = graph_store.view() |
| 343 | + assert isinstance(view, hv.Overlay) |
| 344 | + assert len(view) == 2 |
0 commit comments