@@ -40,6 +40,12 @@ async def ping_elasticsearch() -> bool:
4040 return await es_client .ping ()
4141
4242
43+ async def cleanup_elasticsearch_indices (elasticsearch_client : AsyncElasticsearch ):
44+ indices = await elasticsearch_client .options (ignore_status = 404 ).indices .get (index = "kv-store-e2e-test-*" )
45+ for index in indices :
46+ _ = await elasticsearch_client .options (ignore_status = 404 ).indices .delete (index = index )
47+
48+
4349class ElasticsearchFailedToStartError (Exception ):
4450 pass
4551
@@ -117,11 +123,11 @@ async def es_client(self) -> AsyncGenerator[AsyncElasticsearch, None]:
117123 async with AsyncElasticsearch (hosts = [ES_URL ]) as es_client :
118124 yield es_client
119125
120- async def _cleanup ( self ):
121- elasticsearch_client = get_elasticsearch_client ()
122- indices = await elasticsearch_client . options ( ignore_status = 404 ). indices . get ( index = "kv-store-e2e-test-*" )
123- for index in indices :
124- _ = await elasticsearch_client . options ( ignore_status = 404 ). indices . delete ( index = index )
126+ @ pytest . fixture ( autouse = True )
127+ async def cleanup_elasticsearch_indices ( self , es_client : AsyncElasticsearch ):
128+ await cleanup_elasticsearch_indices ( elasticsearch_client = es_client )
129+ yield
130+ await cleanup_elasticsearch_indices ( elasticsearch_client = es_client )
125131
126132 @pytest .mark .skip (reason = "Distributed Caches are unbounded" )
127133 @override
@@ -150,7 +156,6 @@ class TestElasticsearchStoreNativeMode(BaseTestElasticsearchStore):
150156 @override
151157 @pytest .fixture
152158 async def store (self ) -> ElasticsearchStore :
153- await self ._cleanup ()
154159 return ElasticsearchStore (url = ES_URL , index_prefix = "kv-store-e2e-test" , native_storage = True )
155160
156161 async def test_value_stored_as_flattened_object (self , store : ElasticsearchStore , es_client : AsyncElasticsearch ):
@@ -214,11 +219,10 @@ class TestElasticsearchStoreNonNativeMode(BaseTestElasticsearchStore):
214219 @override
215220 @pytest .fixture
216221 async def store (self ) -> ElasticsearchStore :
217- await self ._cleanup ()
218222 return ElasticsearchStore (url = ES_URL , index_prefix = "kv-store-e2e-test" , native_storage = False )
219223
220224 async def test_value_stored_as_json_string (self , store : ElasticsearchStore , es_client : AsyncElasticsearch ):
221- """Verify values are stored as flattened objects, not JSON strings"""
225+ """Verify values are stored as JSON strings"""
222226 await store .put (collection = "test" , key = "test_key" , value = {"name" : "Alice" , "age" : 30 })
223227
224228 index_name = store ._sanitize_index_name (collection = "test" ) # pyright: ignore[reportPrivateUsage]
0 commit comments