|
| 1 | +import asyncio |
| 2 | +from unittest.mock import AsyncMock, Mock |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from vortexdb.async_client import AsyncVortexDB |
| 7 | +from vortexdb.async_connection import AsyncGRPCConnection |
| 8 | +from vortexdb.models import ContentType, DenseVector, Payload, Point, Similarity |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def mock_connection(monkeypatch): |
| 13 | + """ |
| 14 | + Replace AsyncGRPCConnection with a mock instance. |
| 15 | + """ |
| 16 | + conn = Mock(spec=AsyncGRPCConnection) |
| 17 | + conn.stub = Mock() |
| 18 | + conn.call = AsyncMock() |
| 19 | + conn.close = AsyncMock() |
| 20 | + monkeypatch.setattr("vortexdb.async_client.AsyncGRPCConnection", lambda _: conn) |
| 21 | + return conn |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture |
| 25 | +def client(mock_connection): |
| 26 | + return AsyncVortexDB( |
| 27 | + grpc_url="localhost:50051", |
| 28 | + api_key="secret", |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | +def test_async_insert_success(client, mock_connection): |
| 33 | + async def run(): |
| 34 | + response = Mock() |
| 35 | + response.id = Mock() |
| 36 | + response.id.value = "point-123" |
| 37 | + |
| 38 | + mock_connection.call.return_value = response |
| 39 | + |
| 40 | + point_id = await client.insert( |
| 41 | + vector=DenseVector([1, 2, 3]), |
| 42 | + payload=Payload.text("hello"), |
| 43 | + ) |
| 44 | + |
| 45 | + assert point_id == "point-123" |
| 46 | + |
| 47 | + asyncio.run(run()) |
| 48 | + |
| 49 | + |
| 50 | +def test_async_insert_rejects_invalid_vector(client): |
| 51 | + async def run(): |
| 52 | + with pytest.raises(TypeError): |
| 53 | + await client.insert( |
| 54 | + vector=[1, 2, 3], |
| 55 | + payload=Payload.text("hello"), |
| 56 | + ) |
| 57 | + |
| 58 | + asyncio.run(run()) |
| 59 | + |
| 60 | + |
| 61 | +def test_async_get_point_success(client, mock_connection): |
| 62 | + async def run(): |
| 63 | + proto_point = Mock() |
| 64 | + proto_point.id.id.value = "point-123" |
| 65 | + proto_point.vector.values = [1, 2, 3] |
| 66 | + proto_point.payload.content_type = ContentType.TEXT.to_proto() |
| 67 | + proto_point.payload.content = "hello" |
| 68 | + |
| 69 | + mock_connection.call.return_value = proto_point |
| 70 | + |
| 71 | + point = await client.get(point_id="point-123") |
| 72 | + |
| 73 | + assert isinstance(point, Point) |
| 74 | + assert point.id == "point-123" |
| 75 | + assert point.payload.content == "hello" |
| 76 | + |
| 77 | + asyncio.run(run()) |
| 78 | + |
| 79 | + |
| 80 | +def test_async_get_point_not_found(client, mock_connection): |
| 81 | + async def run(): |
| 82 | + mock_connection.call.return_value = None |
| 83 | + |
| 84 | + result = await client.get(point_id="missing") |
| 85 | + |
| 86 | + assert result is None |
| 87 | + |
| 88 | + asyncio.run(run()) |
| 89 | + |
| 90 | + |
| 91 | +def test_async_delete_success(client, mock_connection): |
| 92 | + async def run(): |
| 93 | + mock_connection.call.return_value = None |
| 94 | + |
| 95 | + await client.delete(point_id="point-123") |
| 96 | + |
| 97 | + mock_connection.call.assert_awaited_once() |
| 98 | + |
| 99 | + asyncio.run(run()) |
| 100 | + |
| 101 | + |
| 102 | +def test_async_search_success(client, mock_connection): |
| 103 | + async def run(): |
| 104 | + mock_connection.call.return_value = Mock( |
| 105 | + result_point_ids=[ |
| 106 | + Mock(id=Mock(value="p1")), |
| 107 | + Mock(id=Mock(value="p2")), |
| 108 | + ] |
| 109 | + ) |
| 110 | + |
| 111 | + results = await client.search( |
| 112 | + vector=DenseVector([1, 2, 3]), |
| 113 | + similarity=Similarity.COSINE, |
| 114 | + limit=2, |
| 115 | + ) |
| 116 | + |
| 117 | + assert results == ["p1", "p2"] |
| 118 | + |
| 119 | + asyncio.run(run()) |
| 120 | + |
| 121 | + |
| 122 | +def test_async_search_invalid_vector(client): |
| 123 | + async def run(): |
| 124 | + with pytest.raises(TypeError): |
| 125 | + await client.search( |
| 126 | + vector=[1, 2, 3], |
| 127 | + similarity=Similarity.COSINE, |
| 128 | + limit=2, |
| 129 | + ) |
| 130 | + |
| 131 | + asyncio.run(run()) |
| 132 | + |
| 133 | + |
| 134 | +def test_async_close_closes_connection(client, mock_connection): |
| 135 | + async def run(): |
| 136 | + await client.close() |
| 137 | + mock_connection.close.assert_awaited_once() |
| 138 | + |
| 139 | + asyncio.run(run()) |
| 140 | + |
| 141 | + |
| 142 | +def test_async_context_manager_closes_connection(monkeypatch): |
| 143 | + async def run(): |
| 144 | + conn = Mock(spec=AsyncGRPCConnection) |
| 145 | + conn.stub = Mock() |
| 146 | + conn.call = AsyncMock() |
| 147 | + conn.close = AsyncMock() |
| 148 | + monkeypatch.setattr("vortexdb.async_client.AsyncGRPCConnection", lambda _: conn) |
| 149 | + |
| 150 | + async with AsyncVortexDB( |
| 151 | + grpc_url="localhost:50051", |
| 152 | + api_key="secret", |
| 153 | + ) as db: |
| 154 | + assert db is not None |
| 155 | + |
| 156 | + conn.close.assert_awaited_once() |
| 157 | + |
| 158 | + asyncio.run(run()) |
0 commit comments