Skip to content

Commit 57fa671

Browse files
authored
test: Opensearch - add unit tests (#3182)
1 parent fb99d1f commit 57fa671

File tree

11 files changed

+169
-192
lines changed

11 files changed

+169
-192
lines changed

integrations/opensearch/src/haystack_integrations/components/retrievers/opensearch/bm25_retriever.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
from haystack_integrations.document_stores.opensearch import OpenSearchDocumentStore
1515

16+
from .utils import _resolve_document_store
17+
1618
logger = logging.getLogger(__name__)
1719

1820

@@ -268,13 +270,7 @@ def run(
268270
custom_query=custom_query,
269271
)
270272

271-
if document_store is not None:
272-
if not isinstance(document_store, OpenSearchDocumentStore):
273-
msg = "document_store must be an instance of OpenSearchDocumentStore"
274-
raise ValueError(msg)
275-
doc_store = document_store
276-
else:
277-
doc_store = self._document_store
273+
doc_store = _resolve_document_store(document_store, self._document_store)
278274

279275
try:
280276
docs = doc_store._bm25_retrieval(**bm25_args) # example for BM25Retriever
@@ -335,13 +331,7 @@ async def run_async( # pylint: disable=too-many-positional-arguments
335331
custom_query=custom_query,
336332
)
337333

338-
if document_store is not None:
339-
if not isinstance(document_store, OpenSearchDocumentStore):
340-
msg = "document_store must be an instance of OpenSearchDocumentStore"
341-
raise ValueError(msg)
342-
doc_store = document_store
343-
else:
344-
doc_store = self._document_store
334+
doc_store = _resolve_document_store(document_store, self._document_store)
345335

346336
try:
347337
docs = await doc_store._bm25_retrieval_async(**bm25_args)

integrations/opensearch/src/haystack_integrations/components/retrievers/opensearch/embedding_retriever.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
from haystack_integrations.document_stores.opensearch import OpenSearchDocumentStore
1515

16+
from .utils import _resolve_document_store
17+
1618
logger = logging.getLogger(__name__)
1719

1820

@@ -257,13 +259,7 @@ def run(
257259

258260
docs: list[Document] = []
259261

260-
if document_store is not None:
261-
if not isinstance(document_store, OpenSearchDocumentStore):
262-
msg = "document_store must be an instance of OpenSearchDocumentStore"
263-
raise ValueError(msg)
264-
doc_store = document_store
265-
else:
266-
doc_store = self._document_store
262+
doc_store = _resolve_document_store(document_store, self._document_store)
267263

268264
try:
269265
docs = doc_store._embedding_retrieval(
@@ -383,13 +379,7 @@ async def run_async(
383379

384380
docs: list[Document] = []
385381

386-
if document_store is not None:
387-
if not isinstance(document_store, OpenSearchDocumentStore):
388-
msg = "document_store must be an instance of OpenSearchDocumentStore"
389-
raise ValueError(msg)
390-
doc_store = document_store
391-
else:
392-
doc_store = self._document_store
382+
doc_store = _resolve_document_store(document_store, self._document_store)
393383

394384
try:
395385
docs = await doc_store._embedding_retrieval_async(

integrations/opensearch/src/haystack_integrations/components/retrievers/opensearch/metadata_retriever.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
from haystack_integrations.document_stores.opensearch import OpenSearchDocumentStore
1111

12+
from .utils import _resolve_document_store
13+
1214
logger = logging.getLogger(__name__)
1315

1416

@@ -252,10 +254,7 @@ def run(
252254
# Returns: {"metadata": [{"category": "Python", "status": "active", "priority": 1}]}
253255
```
254256
"""
255-
doc_store = document_store or self._document_store
256-
if not isinstance(doc_store, OpenSearchDocumentStore):
257-
msg = "document_store must be an instance of OpenSearchDocumentStore"
258-
raise ValueError(msg)
257+
doc_store = _resolve_document_store(document_store, self._document_store)
259258

260259
fields_to_use = metadata_fields if metadata_fields is not None else self._metadata_fields
261260
top_k_to_use = top_k if top_k is not None else self._top_k
@@ -366,10 +365,7 @@ async def run_async(
366365
# Returns: {"metadata": [{"category": "Python", "status": "active", "priority": 1}]}
367366
```
368367
"""
369-
doc_store = document_store or self._document_store
370-
if not isinstance(doc_store, OpenSearchDocumentStore):
371-
msg = "document_store must be an instance of OpenSearchDocumentStore"
372-
raise ValueError(msg)
368+
doc_store = _resolve_document_store(document_store, self._document_store)
373369

374370
fields_to_use = metadata_fields if metadata_fields is not None else self._metadata_fields
375371
top_k_to_use = top_k if top_k is not None else self._top_k

integrations/opensearch/src/haystack_integrations/components/retrievers/opensearch/sql_retriever.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
from haystack_integrations.document_stores.opensearch import OpenSearchDocumentStore
1010

11+
from .utils import _resolve_document_store
12+
1113
logger = logging.getLogger(__name__)
1214

1315

@@ -108,13 +110,7 @@ def run(
108110
# For aggregate queries: result["result"]["aggregations"] contains aggregations
109111
```
110112
"""
111-
if document_store is not None:
112-
if not isinstance(document_store, OpenSearchDocumentStore):
113-
msg = "document_store must be an instance of OpenSearchDocumentStore"
114-
raise ValueError(msg)
115-
doc_store = document_store
116-
else:
117-
doc_store = self._document_store
113+
doc_store = _resolve_document_store(document_store, self._document_store)
118114

119115
fetch_size = fetch_size if fetch_size is not None else self._fetch_size
120116

@@ -163,13 +159,7 @@ async def run_async(
163159
# For aggregate queries: result["result"]["aggregations"] contains aggregations
164160
```
165161
"""
166-
if document_store is not None:
167-
if not isinstance(document_store, OpenSearchDocumentStore):
168-
msg = "document_store must be an instance of OpenSearchDocumentStore"
169-
raise ValueError(msg)
170-
doc_store = document_store
171-
else:
172-
doc_store = self._document_store
162+
doc_store = _resolve_document_store(document_store, self._document_store)
173163

174164
fetch_size = fetch_size if fetch_size is not None else self._fetch_size
175165

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# SPDX-FileCopyrightText: 2023-present deepset GmbH <info@deepset.ai>
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
6+
from haystack_integrations.document_stores.opensearch import OpenSearchDocumentStore
7+
8+
9+
def _resolve_document_store(
10+
runtime_document_store: OpenSearchDocumentStore | None,
11+
default_document_store: OpenSearchDocumentStore,
12+
) -> OpenSearchDocumentStore:
13+
"""
14+
Return the runtime document store if provided and valid, otherwise the default one.
15+
16+
:raises ValueError: If `runtime_document_store` is not None and not an OpenSearchDocumentStore.
17+
"""
18+
if runtime_document_store is None:
19+
return default_document_store
20+
if not isinstance(runtime_document_store, OpenSearchDocumentStore):
21+
msg = "document_store must be an instance of OpenSearchDocumentStore"
22+
raise ValueError(msg)
23+
return runtime_document_store

0 commit comments

Comments
 (0)