|
| 1 | +import uuid |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from mcp_server_qdrant.embeddings.fastembed import FastEmbedProvider |
| 6 | +from mcp_server_qdrant.qdrant import Entry, QdrantConnector |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture |
| 10 | +async def embedding_provider(): |
| 11 | + """Fixture to provide a FastEmbed embedding provider.""" |
| 12 | + return FastEmbedProvider(model_name="sentence-transformers/all-MiniLM-L6-v2") |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture |
| 16 | +async def qdrant_connector(embedding_provider): |
| 17 | + """Fixture to provide a QdrantConnector with in-memory Qdrant client.""" |
| 18 | + # Use a random collection name to avoid conflicts between tests |
| 19 | + collection_name = f"test_collection_{uuid.uuid4().hex}" |
| 20 | + |
| 21 | + # Create connector with in-memory Qdrant |
| 22 | + connector = QdrantConnector( |
| 23 | + qdrant_url=":memory:", |
| 24 | + qdrant_api_key=None, |
| 25 | + collection_name=collection_name, |
| 26 | + embedding_provider=embedding_provider, |
| 27 | + ) |
| 28 | + |
| 29 | + yield connector |
| 30 | + |
| 31 | + |
| 32 | +@pytest.mark.asyncio |
| 33 | +async def test_store_and_search(qdrant_connector): |
| 34 | + """Test storing an entry and then searching for it.""" |
| 35 | + # Store a test entry |
| 36 | + test_entry = Entry( |
| 37 | + content="The quick brown fox jumps over the lazy dog", |
| 38 | + metadata={"source": "test", "importance": "high"}, |
| 39 | + ) |
| 40 | + await qdrant_connector.store(test_entry) |
| 41 | + |
| 42 | + # Search for the entry |
| 43 | + results = await qdrant_connector.search("fox jumps") |
| 44 | + |
| 45 | + # Verify results |
| 46 | + assert len(results) == 1 |
| 47 | + assert results[0].content == test_entry.content |
| 48 | + assert results[0].metadata == test_entry.metadata |
| 49 | + |
| 50 | + |
| 51 | +@pytest.mark.asyncio |
| 52 | +async def test_search_empty_collection(qdrant_connector): |
| 53 | + """Test searching in an empty collection.""" |
| 54 | + # Search in an empty collection |
| 55 | + results = await qdrant_connector.search("test query") |
| 56 | + |
| 57 | + # Verify results |
| 58 | + assert len(results) == 0 |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.asyncio |
| 62 | +async def test_multiple_entries(qdrant_connector): |
| 63 | + """Test storing and searching multiple entries.""" |
| 64 | + # Store multiple entries |
| 65 | + entries = [ |
| 66 | + Entry( |
| 67 | + content="Python is a programming language", |
| 68 | + metadata={"topic": "programming"}, |
| 69 | + ), |
| 70 | + Entry(content="The Eiffel Tower is in Paris", metadata={"topic": "landmarks"}), |
| 71 | + Entry(content="Machine learning is a subset of AI", metadata={"topic": "AI"}), |
| 72 | + ] |
| 73 | + |
| 74 | + for entry in entries: |
| 75 | + await qdrant_connector.store(entry) |
| 76 | + |
| 77 | + # Search for programming-related entries |
| 78 | + programming_results = await qdrant_connector.search("Python programming") |
| 79 | + assert len(programming_results) > 0 |
| 80 | + assert any("Python" in result.content for result in programming_results) |
| 81 | + |
| 82 | + # Search for landmark-related entries |
| 83 | + landmark_results = await qdrant_connector.search("Eiffel Tower Paris") |
| 84 | + assert len(landmark_results) > 0 |
| 85 | + assert any("Eiffel" in result.content for result in landmark_results) |
| 86 | + |
| 87 | + # Search for AI-related entries |
| 88 | + ai_results = await qdrant_connector.search( |
| 89 | + "artificial intelligence machine learning" |
| 90 | + ) |
| 91 | + assert len(ai_results) > 0 |
| 92 | + assert any("machine learning" in result.content.lower() for result in ai_results) |
| 93 | + |
| 94 | + |
| 95 | +@pytest.mark.asyncio |
| 96 | +async def test_ensure_collection_exists(qdrant_connector): |
| 97 | + """Test that the collection is created if it doesn't exist.""" |
| 98 | + # The collection shouldn't exist yet |
| 99 | + assert not await qdrant_connector._client.collection_exists( |
| 100 | + qdrant_connector._collection_name |
| 101 | + ) |
| 102 | + |
| 103 | + # Storing an entry should create the collection |
| 104 | + test_entry = Entry(content="Test content") |
| 105 | + await qdrant_connector.store(test_entry) |
| 106 | + |
| 107 | + # Now the collection should exist |
| 108 | + assert await qdrant_connector._client.collection_exists( |
| 109 | + qdrant_connector._collection_name |
| 110 | + ) |
| 111 | + |
| 112 | + |
| 113 | +@pytest.mark.asyncio |
| 114 | +async def test_metadata_handling(qdrant_connector): |
| 115 | + """Test that metadata is properly stored and retrieved.""" |
| 116 | + # Store entries with different metadata |
| 117 | + metadata1 = {"source": "book", "author": "Jane Doe", "year": 2023} |
| 118 | + metadata2 = {"source": "article", "tags": ["science", "research"]} |
| 119 | + |
| 120 | + await qdrant_connector.store( |
| 121 | + Entry(content="Content with structured metadata", metadata=metadata1) |
| 122 | + ) |
| 123 | + await qdrant_connector.store( |
| 124 | + Entry(content="Content with list in metadata", metadata=metadata2) |
| 125 | + ) |
| 126 | + |
| 127 | + # Search and verify metadata is preserved |
| 128 | + results = await qdrant_connector.search("metadata") |
| 129 | + |
| 130 | + assert len(results) == 2 |
| 131 | + |
| 132 | + # Check that both metadata objects are present in the results |
| 133 | + found_metadata1 = False |
| 134 | + found_metadata2 = False |
| 135 | + |
| 136 | + for result in results: |
| 137 | + if result.metadata.get("source") == "book": |
| 138 | + assert result.metadata.get("author") == "Jane Doe" |
| 139 | + assert result.metadata.get("year") == 2023 |
| 140 | + found_metadata1 = True |
| 141 | + elif result.metadata.get("source") == "article": |
| 142 | + assert "science" in result.metadata.get("tags", []) |
| 143 | + assert "research" in result.metadata.get("tags", []) |
| 144 | + found_metadata2 = True |
| 145 | + |
| 146 | + assert found_metadata1 |
| 147 | + assert found_metadata2 |
| 148 | + |
| 149 | + |
| 150 | +@pytest.mark.asyncio |
| 151 | +async def test_entry_without_metadata(qdrant_connector): |
| 152 | + """Test storing and retrieving entries without metadata.""" |
| 153 | + # Store an entry without metadata |
| 154 | + await qdrant_connector.store(Entry(content="Entry without metadata")) |
| 155 | + |
| 156 | + # Search and verify |
| 157 | + results = await qdrant_connector.search("without metadata") |
| 158 | + |
| 159 | + assert len(results) == 1 |
| 160 | + assert results[0].content == "Entry without metadata" |
| 161 | + assert results[0].metadata is None |
0 commit comments