55
66from vortexdb .async_client import AsyncVortexDB
77from vortexdb .async_connection import AsyncGRPCConnection
8- from vortexdb .models import ContentType , DenseVector , Payload , Point , Similarity
8+ from vortexdb .models import ContentType , DenseVector , Payload , Point , Similarity , SearchQuery
99
1010
1111@pytest .fixture
@@ -58,6 +58,59 @@ async def run():
5858 asyncio .run (run ())
5959
6060
61+ # Batch Insert
62+
63+ def test_async_batch_insert_success (client , mock_connection ):
64+ async def run ():
65+ response = Mock ()
66+ response .ids = [
67+ Mock (id = Mock (value = "p1" )),
68+ Mock (id = Mock (value = "p2" )),
69+ ]
70+ mock_connection .call .return_value = response
71+
72+ items = [
73+ (DenseVector ([1 , 2 , 3 ]), Payload .text ("a" )),
74+ (DenseVector ([4 , 5 , 6 ]), Payload .text ("b" )),
75+ ]
76+ result = await client .batch_insert (items = items )
77+ assert result == ["p1" , "p2" ]
78+
79+ asyncio .run (run ())
80+
81+
82+ def test_async_batch_insert_invalid_items_type (client ):
83+ async def run ():
84+ with pytest .raises (TypeError ):
85+ await client .batch_insert (items = "not-a-list" )
86+
87+ asyncio .run (run ())
88+
89+
90+ def test_async_batch_insert_invalid_tuple_structure (client ):
91+ async def run ():
92+ items = [
93+ (DenseVector ([1 , 2 , 3 ]),), # only one element
94+ ]
95+ with pytest .raises (TypeError ):
96+ await client .batch_insert (items = items )
97+
98+ asyncio .run (run ())
99+
100+
101+ def test_async_batch_insert_invalid_vector (client ):
102+ async def run ():
103+ items = [
104+ ([1 , 2 , 3 ], Payload .text ("a" )), # not DenseVector
105+ ]
106+ with pytest .raises (TypeError ):
107+ await client .batch_insert (items = items )
108+
109+ asyncio .run (run ())
110+
111+
112+ # Get
113+
61114def test_async_get_point_success (client , mock_connection ):
62115 async def run ():
63116 proto_point = Mock ()
@@ -88,6 +141,8 @@ async def run():
88141 asyncio .run (run ())
89142
90143
144+ # Delete
145+
91146def test_async_delete_success (client , mock_connection ):
92147 async def run ():
93148 mock_connection .call .return_value = None
@@ -99,6 +154,8 @@ async def run():
99154 asyncio .run (run ())
100155
101156
157+ # Search
158+
102159def test_async_search_success (client , mock_connection ):
103160 async def run ():
104161 mock_connection .call .return_value = Mock (
@@ -119,6 +176,39 @@ async def run():
119176 asyncio .run (run ())
120177
121178
179+ def test_async_search_with_query_object (client , mock_connection ):
180+ async def run ():
181+ mock_connection .call .return_value = Mock (
182+ result_point_ids = [
183+ Mock (id = Mock (value = "p1" )),
184+ ]
185+ )
186+
187+ q = SearchQuery (DenseVector ([1 , 2 , 3 ]), Similarity .COSINE , 2 )
188+ results = await client .search (query = q )
189+
190+ assert results == ["p1" ]
191+
192+ asyncio .run (run ())
193+
194+
195+ def test_async_search_accepts_ef (client , mock_connection ):
196+ async def run ():
197+ mock_connection .call .return_value = Mock (result_point_ids = [])
198+
199+ await client .search (
200+ vector = DenseVector ([1 , 2 , 3 ]),
201+ similarity = Similarity .COSINE ,
202+ limit = 2 ,
203+ ef = 128 ,
204+ )
205+
206+ request = mock_connection .call .call_args .args [1 ]
207+ assert request .ef == 128
208+
209+ asyncio .run (run ())
210+
211+
122212def test_async_search_invalid_vector (client ):
123213 async def run ():
124214 with pytest .raises (TypeError ):
@@ -131,6 +221,128 @@ async def run():
131221 asyncio .run (run ())
132222
133223
224+ # Batch Search
225+
226+ def test_async_batch_search_full_tuple (client , mock_connection ):
227+ async def run ():
228+ mock_connection .call .return_value = Mock (
229+ results = [
230+ Mock (result_point_ids = [Mock (id = Mock (value = "p1" ))]),
231+ Mock (result_point_ids = [Mock (id = Mock (value = "p2" ))]),
232+ ]
233+ )
234+ queries = [
235+ (DenseVector ([1 , 2 , 3 ]), Similarity .COSINE , 2 ),
236+ (DenseVector ([4 , 5 , 6 ]), Similarity .EUCLIDEAN , 1 ),
237+ ]
238+ result = await client .batch_search (queries = queries )
239+ assert result == [["p1" ], ["p2" ]]
240+
241+ asyncio .run (run ())
242+
243+
244+ def test_async_batch_search_searchquery_objects (client , mock_connection ):
245+ async def run ():
246+ mock_connection .call .return_value = Mock (
247+ results = [
248+ Mock (result_point_ids = [Mock (id = Mock (value = "p1" ))]),
249+ ]
250+ )
251+ queries = [
252+ SearchQuery (DenseVector ([1 , 2 , 3 ]), Similarity .COSINE , 2 ),
253+ ]
254+ result = await client .batch_search (queries = queries )
255+ assert result == [["p1" ]]
256+
257+ asyncio .run (run ())
258+
259+
260+ def test_async_batch_search_vectors_with_global_params (client , mock_connection ):
261+ async def run ():
262+ mock_connection .call .return_value = Mock (
263+ results = [
264+ Mock (result_point_ids = [Mock (id = Mock (value = "p1" ))]),
265+ ]
266+ )
267+ queries = [DenseVector ([1 , 2 , 3 ])]
268+ result = await client .batch_search (
269+ queries = queries ,
270+ similarity = Similarity .MANHATTAN ,
271+ limit = 2 ,
272+ )
273+ assert result == [["p1" ]]
274+
275+ asyncio .run (run ())
276+
277+
278+ def test_async_batch_search_vector_similarity_with_global_limit (client , mock_connection ):
279+ async def run ():
280+ mock_connection .call .return_value = Mock (
281+ results = [
282+ Mock (result_point_ids = [Mock (id = Mock (value = "p1" ))]),
283+ ]
284+ )
285+ queries = [
286+ (DenseVector ([1 , 2 , 3 ]), Similarity .COSINE ),
287+ ]
288+ result = await client .batch_search (
289+ queries = queries ,
290+ limit = 2 ,
291+ )
292+ assert result == [["p1" ]]
293+
294+ asyncio .run (run ())
295+
296+
297+ def test_async_batch_search_accepts_ef (client , mock_connection ):
298+ async def run ():
299+ mock_connection .call .return_value = Mock (results = [])
300+
301+ await client .batch_search (
302+ queries = [
303+ (DenseVector ([1 , 2 , 3 ]), Similarity .COSINE , 2 ),
304+ (DenseVector ([4 , 5 , 6 ]), Similarity .COSINE , 1 ),
305+ ],
306+ ef = 256 ,
307+ )
308+
309+ request = mock_connection .call .call_args .args [1 ]
310+ assert [query .ef for query in request .queries ] == [256 , 256 ]
311+
312+ asyncio .run (run ())
313+
314+
315+ def test_async_batch_search_missing_globals_for_vector (client ):
316+ async def run ():
317+ queries = [DenseVector ([1 , 2 , 3 ])]
318+ with pytest .raises (ValueError ):
319+ await client .batch_search (queries = queries )
320+
321+ asyncio .run (run ())
322+
323+
324+ def test_async_batch_search_missing_limit (client ):
325+ async def run ():
326+ queries = [
327+ (DenseVector ([1 , 2 , 3 ]), Similarity .COSINE ),
328+ ]
329+ with pytest .raises (ValueError ):
330+ await client .batch_search (queries = queries )
331+
332+ asyncio .run (run ())
333+
334+
335+ def test_async_batch_search_invalid_format (client ):
336+ async def run ():
337+ queries = ["invalid" ]
338+ with pytest .raises (TypeError ):
339+ await client .batch_search (queries = queries )
340+
341+ asyncio .run (run ())
342+
343+
344+ # Close / Context Manager
345+
134346def test_async_close_closes_connection (client , mock_connection ):
135347 async def run ():
136348 await client .close ()
@@ -155,4 +367,4 @@ async def run():
155367
156368 conn .close .assert_awaited_once ()
157369
158- asyncio .run (run ())
370+ asyncio .run (run ())
0 commit comments