forked from innovacion/searchbox
Add 97% test coverage
This commit is contained in:
@@ -129,13 +129,138 @@ class TestQdrantEngine:
|
||||
assert all(isinstance(cond, models.FieldCondition) for cond in result.must)
|
||||
|
||||
def test_transform_response_empty(self, qdrant_engine):
|
||||
"""Test transform_response with empty response"""
|
||||
"""Test transform_response with empty results"""
|
||||
response = []
|
||||
|
||||
result = qdrant_engine.transform_response(response)
|
||||
assert result == []
|
||||
|
||||
assert isinstance(result, list)
|
||||
assert len(result) == 0
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_index(self, qdrant_engine, mock_client):
|
||||
"""Test create_index method"""
|
||||
mock_client.create_collection.return_value = True
|
||||
|
||||
result = await qdrant_engine.create_index("test_collection", 384)
|
||||
|
||||
assert result is True
|
||||
mock_client.create_collection.assert_called_once_with(
|
||||
collection_name="test_collection",
|
||||
vectors_config=models.VectorParams(
|
||||
size=384, distance=models.Distance.COSINE
|
||||
),
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_index_failure(self, qdrant_engine, mock_client):
|
||||
"""Test create_index method when it fails"""
|
||||
mock_client.create_collection.side_effect = Exception("Collection creation failed")
|
||||
|
||||
with pytest.raises(Exception, match="Collection creation failed"):
|
||||
await qdrant_engine.create_index("failing_collection", 512)
|
||||
|
||||
def test_transform_chunk(self, qdrant_engine):
|
||||
"""Test transform_chunk method"""
|
||||
from vector_search_mcp.models import Chunk, ChunkData
|
||||
|
||||
chunk = Chunk(
|
||||
id="test-chunk-1",
|
||||
vector=[0.1, 0.2, 0.3, 0.4, 0.5],
|
||||
payload=ChunkData(
|
||||
page_content="This is test content",
|
||||
filename="test_doc.pdf",
|
||||
page=42
|
||||
)
|
||||
)
|
||||
|
||||
result = qdrant_engine.transform_chunk(chunk)
|
||||
|
||||
assert isinstance(result, models.PointStruct)
|
||||
assert result.id == "test-chunk-1"
|
||||
assert result.vector == [0.1, 0.2, 0.3, 0.4, 0.5]
|
||||
assert result.payload == {
|
||||
"page_content": "This is test content",
|
||||
"filename": "test_doc.pdf",
|
||||
"page": 42
|
||||
}
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_run_upload_chunk(self, qdrant_engine, mock_client):
|
||||
"""Test run_upload_chunk method"""
|
||||
# Setup mock response
|
||||
mock_response = MagicMock()
|
||||
mock_response.status = models.UpdateStatus.ACKNOWLEDGED
|
||||
mock_client.upsert.return_value = mock_response
|
||||
|
||||
# Create test point
|
||||
test_point = models.PointStruct(
|
||||
id="test-point-1",
|
||||
vector=[0.1, 0.2, 0.3],
|
||||
payload={"content": "test"}
|
||||
)
|
||||
|
||||
result = await qdrant_engine.run_upload_chunk("test_index", test_point)
|
||||
|
||||
assert result is True
|
||||
mock_client.upsert.assert_called_once_with(
|
||||
collection_name="test_index",
|
||||
points=[test_point]
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_run_upload_chunk_failure(self, qdrant_engine, mock_client):
|
||||
"""Test run_upload_chunk method when upload fails"""
|
||||
# Setup mock response with failure status
|
||||
mock_response = MagicMock()
|
||||
mock_response.status = models.UpdateStatus.COMPLETED # Not ACKNOWLEDGED
|
||||
mock_client.upsert.return_value = mock_response
|
||||
|
||||
test_point = models.PointStruct(
|
||||
id="test-point-1",
|
||||
vector=[0.1, 0.2, 0.3],
|
||||
payload={"content": "test"}
|
||||
)
|
||||
|
||||
result = await qdrant_engine.run_upload_chunk("test_index", test_point)
|
||||
|
||||
assert result is False
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_upload_chunk_integration(self, qdrant_engine, mock_client):
|
||||
"""Test the complete upload_chunk workflow"""
|
||||
from vector_search_mcp.models import Chunk, ChunkData
|
||||
|
||||
# Setup mock response
|
||||
mock_response = MagicMock()
|
||||
mock_response.status = models.UpdateStatus.ACKNOWLEDGED
|
||||
mock_client.upsert.return_value = mock_response
|
||||
|
||||
chunk = Chunk(
|
||||
id="integration-test-chunk",
|
||||
vector=[0.5, 0.4, 0.3, 0.2, 0.1],
|
||||
payload=ChunkData(
|
||||
page_content="Integration test content",
|
||||
filename="integration_test.pdf",
|
||||
page=1
|
||||
)
|
||||
)
|
||||
|
||||
result = await qdrant_engine.upload_chunk("integration_collection", chunk)
|
||||
|
||||
assert result is True
|
||||
# Verify the complete workflow: transform_chunk -> run_upload_chunk
|
||||
mock_client.upsert.assert_called_once()
|
||||
args, kwargs = mock_client.upsert.call_args
|
||||
|
||||
assert kwargs["collection_name"] == "integration_collection"
|
||||
assert len(kwargs["points"]) == 1
|
||||
|
||||
uploaded_point = kwargs["points"][0]
|
||||
assert uploaded_point.id == "integration-test-chunk"
|
||||
assert uploaded_point.vector == [0.5, 0.4, 0.3, 0.2, 0.1]
|
||||
assert uploaded_point.payload == {
|
||||
"page_content": "Integration test content",
|
||||
"filename": "integration_test.pdf",
|
||||
"page": 1
|
||||
}
|
||||
|
||||
def test_transform_response_with_scored_points(self, qdrant_engine):
|
||||
"""Test transform_response with valid ScoredPoint objects"""
|
||||
|
||||
Reference in New Issue
Block a user