|
10 | 10 | ) |
11 | 11 | from typesense import exceptions |
12 | 12 | from typesense.api_call import ApiCall |
| 13 | +from typesense.async_api_call import AsyncApiCall |
| 14 | +from typesense.async_multi_search import AsyncMultiSearch |
13 | 15 | from typesense.multi_search import MultiSearch |
14 | 16 | from typesense.types.multi_search import MultiSearchRequestSchema |
15 | 17 |
|
16 | 18 |
|
17 | 19 | def test_init(fake_api_call: ApiCall) -> None: |
18 | | - """Test that the Document object is initialized correctly.""" |
19 | | - documents = MultiSearch(fake_api_call) |
| 20 | + """Test that the MultiSearch object is initialized correctly.""" |
| 21 | + multi_search = MultiSearch(fake_api_call) |
20 | 22 |
|
21 | | - assert_match_object(documents.api_call, fake_api_call) |
| 23 | + assert_match_object(multi_search.api_call, fake_api_call) |
22 | 24 | assert_object_lists_match( |
23 | | - documents.api_call.node_manager.nodes, |
| 25 | + multi_search.api_call.node_manager.nodes, |
24 | 26 | fake_api_call.node_manager.nodes, |
25 | 27 | ) |
26 | 28 | assert_match_object( |
27 | | - documents.api_call.config.nearest_node, |
| 29 | + multi_search.api_call.config.nearest_node, |
28 | 30 | fake_api_call.config.nearest_node, |
29 | 31 | ) |
30 | 32 |
|
31 | 33 |
|
| 34 | +def test_init_async(fake_async_api_call: AsyncApiCall) -> None: |
| 35 | + """Test that the AsyncMultiSearch object is initialized correctly.""" |
| 36 | + multi_search = AsyncMultiSearch(fake_async_api_call) |
| 37 | + |
| 38 | + assert_match_object(multi_search.api_call, fake_async_api_call) |
| 39 | + assert_object_lists_match( |
| 40 | + multi_search.api_call.node_manager.nodes, |
| 41 | + fake_async_api_call.node_manager.nodes, |
| 42 | + ) |
| 43 | + assert_match_object( |
| 44 | + multi_search.api_call.config.nearest_node, |
| 45 | + fake_async_api_call.config.nearest_node, |
| 46 | + ) |
| 47 | + |
| 48 | + |
32 | 49 | def test_multi_search_single_search( |
33 | 50 | actual_multi_search: MultiSearch, |
34 | 51 | actual_api_call: ApiCall, |
@@ -220,3 +237,157 @@ def test_search_invalid_parameters( |
220 | 237 | ], |
221 | 238 | }, |
222 | 239 | ) |
| 240 | + |
| 241 | + |
| 242 | +async def test_multi_search_single_search_async( |
| 243 | + actual_async_multi_search: AsyncMultiSearch, |
| 244 | + delete_all: None, |
| 245 | + create_collection: None, |
| 246 | + create_document: None, |
| 247 | +) -> None: |
| 248 | + """Test that the AsyncMultiSearch object can perform a single search.""" |
| 249 | + request_params: MultiSearchRequestSchema = { |
| 250 | + "searches": [ |
| 251 | + {"q": "com", "query_by": "company_name", "collection": "companies"}, |
| 252 | + ], |
| 253 | + } |
| 254 | + response = await actual_async_multi_search.perform( |
| 255 | + search_queries=request_params, |
| 256 | + ) |
| 257 | + |
| 258 | + assert len(response.get("results")) == 1 |
| 259 | + assert_to_contain_keys( |
| 260 | + response.get("results")[0], |
| 261 | + [ |
| 262 | + "facet_counts", |
| 263 | + "found", |
| 264 | + "hits", |
| 265 | + "page", |
| 266 | + "out_of", |
| 267 | + "request_params", |
| 268 | + "search_time_ms", |
| 269 | + "search_cutoff", |
| 270 | + ], |
| 271 | + ) |
| 272 | + |
| 273 | + assert_to_contain_keys( |
| 274 | + response.get("results")[0].get("hits")[0], |
| 275 | + ["document", "highlights", "highlight", "text_match", "text_match_info"], |
| 276 | + ) |
| 277 | + |
| 278 | + |
| 279 | +async def test_multi_search_multiple_searches_async( |
| 280 | + actual_async_multi_search: AsyncMultiSearch, |
| 281 | + delete_all: None, |
| 282 | + create_collection: None, |
| 283 | + create_document: None, |
| 284 | +) -> None: |
| 285 | + """Test that the AsyncMultiSearch object can perform multiple searches.""" |
| 286 | + request_params: MultiSearchRequestSchema = { |
| 287 | + "searches": [ |
| 288 | + {"q": "com", "query_by": "company_name", "collection": "companies"}, |
| 289 | + {"q": "company", "query_by": "company_name", "collection": "companies"}, |
| 290 | + ], |
| 291 | + } |
| 292 | + |
| 293 | + response = await actual_async_multi_search.perform(search_queries=request_params) |
| 294 | + |
| 295 | + assert len(response.get("results")) == len(request_params.get("searches")) |
| 296 | + for search_results in response.get("results"): |
| 297 | + assert_to_contain_keys( |
| 298 | + search_results, |
| 299 | + [ |
| 300 | + "facet_counts", |
| 301 | + "found", |
| 302 | + "hits", |
| 303 | + "page", |
| 304 | + "out_of", |
| 305 | + "request_params", |
| 306 | + "search_time_ms", |
| 307 | + "search_cutoff", |
| 308 | + ], |
| 309 | + ) |
| 310 | + |
| 311 | + assert_to_contain_keys( |
| 312 | + search_results.get("hits")[0], |
| 313 | + ["document", "highlights", "highlight", "text_match", "text_match_info"], |
| 314 | + ) |
| 315 | + |
| 316 | + |
| 317 | +async def test_multi_search_union_async( |
| 318 | + actual_async_multi_search: AsyncMultiSearch, |
| 319 | + delete_all: None, |
| 320 | + create_collection: None, |
| 321 | + create_document: None, |
| 322 | +) -> None: |
| 323 | + """Test that the AsyncMultiSearch object can perform multiple searches with union.""" |
| 324 | + request_params: MultiSearchRequestSchema = { |
| 325 | + "union": True, |
| 326 | + "searches": [ |
| 327 | + {"q": "com", "query_by": "company_name", "collection": "companies"}, |
| 328 | + {"q": "company", "query_by": "company_name", "collection": "companies"}, |
| 329 | + ], |
| 330 | + } |
| 331 | + |
| 332 | + response = await actual_async_multi_search.perform(search_queries=request_params) |
| 333 | + |
| 334 | + assert_to_contain_keys( |
| 335 | + response, |
| 336 | + [ |
| 337 | + "found", |
| 338 | + "hits", |
| 339 | + "page", |
| 340 | + "out_of", |
| 341 | + "union_request_params", |
| 342 | + "search_time_ms", |
| 343 | + "search_cutoff", |
| 344 | + ], |
| 345 | + ) |
| 346 | + |
| 347 | + assert_to_contain_keys( |
| 348 | + response.get("hits")[0], |
| 349 | + [ |
| 350 | + "collection", |
| 351 | + "document", |
| 352 | + "highlights", |
| 353 | + "highlight", |
| 354 | + "text_match", |
| 355 | + "text_match_info", |
| 356 | + "search_index", |
| 357 | + ], |
| 358 | + ) |
| 359 | + |
| 360 | + |
| 361 | +async def test_multi_search_array_async( |
| 362 | + actual_async_multi_search: AsyncMultiSearch, |
| 363 | + delete_all: None, |
| 364 | + create_collection: None, |
| 365 | + create_document: None, |
| 366 | +) -> None: |
| 367 | + """Test that the AsyncMultiSearch object can perform a search with an array query_by.""" |
| 368 | + request_params: MultiSearchRequestSchema = { |
| 369 | + "searches": [ |
| 370 | + {"q": "com", "query_by": ["company_name"], "collection": "companies"}, |
| 371 | + ], |
| 372 | + } |
| 373 | + response = await actual_async_multi_search.perform(search_queries=request_params) |
| 374 | + |
| 375 | + assert len(response.get("results")) == 1 |
| 376 | + assert_to_contain_keys( |
| 377 | + response.get("results")[0], |
| 378 | + [ |
| 379 | + "facet_counts", |
| 380 | + "found", |
| 381 | + "hits", |
| 382 | + "page", |
| 383 | + "out_of", |
| 384 | + "request_params", |
| 385 | + "search_time_ms", |
| 386 | + "search_cutoff", |
| 387 | + ], |
| 388 | + ) |
| 389 | + |
| 390 | + assert_to_contain_keys( |
| 391 | + response.get("results")[0].get("hits")[0], |
| 392 | + ["document", "highlights", "highlight", "text_match", "text_match_info"], |
| 393 | + ) |
0 commit comments