|
| 1 | +# SPDX-FileCopyrightText: 2023-present deepset GmbH <info@deepset.ai> |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +import os |
| 5 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 6 | +from uuid import uuid4 |
| 7 | + |
| 8 | +import pytest |
| 9 | +from haystack.utils.auth import Secret |
| 10 | +from pymongo import MongoClient |
| 11 | +from pymongo.driver_info import DriverInfo |
| 12 | + |
| 13 | +from haystack_integrations.document_stores.mongodb_atlas import MongoDBAtlasDocumentStore |
| 14 | + |
| 15 | +_STORE_KWARGS = { |
| 16 | + "mongo_connection_string": Secret.from_token("mongodb://localhost:27017"), |
| 17 | + "database_name": "test_db", |
| 18 | + "collection_name": "test_collection", |
| 19 | + "vector_search_index": "idx", |
| 20 | + "full_text_search_index": "idx", |
| 21 | +} |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture |
| 25 | +def local_store(): |
| 26 | + return MongoDBAtlasDocumentStore(**_STORE_KWARGS) |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture |
| 30 | +def mocked_store_collection(): |
| 31 | + with patch("haystack_integrations.document_stores.mongodb_atlas.document_store.MongoClient") as mock_cls: |
| 32 | + collection = MagicMock() |
| 33 | + collection.aggregate.return_value = [] |
| 34 | + client = mock_cls.return_value |
| 35 | + db = MagicMock() |
| 36 | + client.__getitem__.return_value = db |
| 37 | + db.__getitem__.return_value = collection |
| 38 | + db.list_collection_names.return_value = ["test_collection"] |
| 39 | + client.admin.command.return_value = {"ok": 1} |
| 40 | + yield MongoDBAtlasDocumentStore(**_STORE_KWARGS), collection |
| 41 | + |
| 42 | + |
| 43 | +@pytest.fixture |
| 44 | +def mocked_store_collection_async(): |
| 45 | + with patch("haystack_integrations.document_stores.mongodb_atlas.document_store.AsyncMongoClient") as mock_cls: |
| 46 | + collection = MagicMock() |
| 47 | + cursor = MagicMock() |
| 48 | + cursor.to_list = AsyncMock(return_value=[]) |
| 49 | + collection.aggregate = AsyncMock(return_value=cursor) |
| 50 | + client = mock_cls.return_value |
| 51 | + db = MagicMock() |
| 52 | + client.__getitem__.return_value = db |
| 53 | + db.__getitem__.return_value = collection |
| 54 | + db.list_collection_names = AsyncMock(return_value=["test_collection"]) |
| 55 | + client.admin.command = AsyncMock(return_value={"ok": 1}) |
| 56 | + yield MongoDBAtlasDocumentStore(**_STORE_KWARGS), collection |
| 57 | + |
| 58 | + |
| 59 | +@pytest.fixture |
| 60 | +def real_collection(): |
| 61 | + database_name = "haystack_integration_test" |
| 62 | + collection_name = "test_collection_" + str(uuid4()) |
| 63 | + client = MongoClient( |
| 64 | + os.environ["MONGO_CONNECTION_STRING"], driver=DriverInfo(name="MongoDBAtlasHaystackIntegration") |
| 65 | + ) |
| 66 | + database = client[database_name] |
| 67 | + if collection_name in database.list_collection_names(): |
| 68 | + database[collection_name].drop() |
| 69 | + database.create_collection(collection_name) |
| 70 | + database[collection_name].create_index("id", unique=True) |
| 71 | + try: |
| 72 | + yield database_name, collection_name, client |
| 73 | + finally: |
| 74 | + database[collection_name].drop() |
| 75 | + client.close() |
0 commit comments