11import asyncio
2+ import re
23
34import pytest
45from haystack import Document
56
67from haystack_integrations .document_stores .opensearch .document_store import OpenSearchDocumentStore
78
89
10+ def _get_unique_index_name (request ) -> str :
11+ """
12+ Generate a unique, valid OpenSearch index name from the test's nodeid.
13+
14+ Uses the full nodeid (e.g., 'test_document_store_async.py::TestClass::test_method')
15+ to avoid collisions when tests in different files/classes share the same method name.
16+ """
17+ # Use nodeid for uniqueness across files/classes, sanitize for valid index name
18+ # OpenSearch index names must be lowercase and cannot contain: \, /, *, ?, ", <, >, |, space, comma, #
19+ index_name = re .sub (r"[^a-zA-Z0-9_-]" , "_" , request .node .nodeid ).lower ()
20+ # Ensure it doesn't start with -, _, or + and is not longer than 255 chars
21+ index_name = index_name .lstrip ("-_+" )[:255 ]
22+ return index_name
23+
24+
925@pytest .fixture
1026def document_store (request ):
1127 """
@@ -14,7 +30,7 @@ def document_store(request):
1430 """
1531 hosts = ["https://localhost:9200" ]
1632 # Use a different index for each test so we can run them in parallel
17- index = f" { request . node . name } "
33+ index = _get_unique_index_name ( request )
1834
1935 store = OpenSearchDocumentStore (
2036 hosts = hosts ,
@@ -38,7 +54,7 @@ def document_store(request):
3854@pytest .fixture
3955def document_store_2 (request ):
4056 hosts = ["https://localhost:9200" ]
41- index = f"test_index_2_{ request . node . name } "
57+ index = f"test_index_2_{ _get_unique_index_name ( request ) } "
4258
4359 store = OpenSearchDocumentStore (
4460 hosts = hosts ,
@@ -67,7 +83,7 @@ def document_store_readonly(request):
6783 """
6884 hosts = ["https://localhost:9200" ]
6985 # Use a different index for each test so we can run them in parallel
70- index = f" { request . node . name } "
86+ index = _get_unique_index_name ( request )
7187
7288 store = OpenSearchDocumentStore (
7389 hosts = hosts ,
@@ -96,7 +112,7 @@ def document_store_embedding_dim_4_no_emb_returned(request):
96112 """
97113 hosts = ["https://localhost:9200" ]
98114 # Use a different index for each test so we can run them in parallel
99- index = f" { request . node . name } "
115+ index = _get_unique_index_name ( request )
100116
101117 store = OpenSearchDocumentStore (
102118 hosts = hosts ,
@@ -121,7 +137,7 @@ def document_store_embedding_dim_4_no_emb_returned_faiss(request):
121137 """
122138 hosts = ["https://localhost:9200" ]
123139 # Use a different index for each test so we can run them in parallel
124- index = f" { request . node . name } "
140+ index = _get_unique_index_name ( request )
125141
126142 store = OpenSearchDocumentStore (
127143 hosts = hosts ,
0 commit comments