Skip to content

Commit 1185087

Browse files
committed
Refactor cleanup to use managed ES client
1 parent 0ef5175 commit 1185087

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

key-value/key-value-aio/tests/stores/elasticsearch/test_elasticsearch.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
4349
class 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]

key-value/key-value-sync/tests/code_gen/stores/elasticsearch/test_elasticsearch.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ def ping_elasticsearch() -> bool:
4141
return es_client.ping()
4242

4343

44+
def cleanup_elasticsearch_indices(elasticsearch_client: Elasticsearch):
45+
indices = elasticsearch_client.options(ignore_status=404).indices.get(index="kv-store-e2e-test-*")
46+
for index in indices:
47+
_ = elasticsearch_client.options(ignore_status=404).indices.delete(index=index)
48+
49+
4450
class ElasticsearchFailedToStartError(Exception):
4551
pass
4652

@@ -118,11 +124,11 @@ def es_client(self) -> Generator[Elasticsearch, None, None]:
118124
with Elasticsearch(hosts=[ES_URL]) as es_client:
119125
yield es_client
120126

121-
def _cleanup(self):
122-
elasticsearch_client = get_elasticsearch_client()
123-
indices = elasticsearch_client.options(ignore_status=404).indices.get(index="kv-store-e2e-test-*")
124-
for index in indices:
125-
_ = elasticsearch_client.options(ignore_status=404).indices.delete(index=index)
127+
@pytest.fixture(autouse=True)
128+
def cleanup_elasticsearch_indices(self, es_client: Elasticsearch):
129+
cleanup_elasticsearch_indices(elasticsearch_client=es_client)
130+
yield
131+
cleanup_elasticsearch_indices(elasticsearch_client=es_client)
126132

127133
@pytest.mark.skip(reason="Distributed Caches are unbounded")
128134
@override
@@ -151,7 +157,6 @@ class TestElasticsearchStoreNativeMode(BaseTestElasticsearchStore):
151157
@override
152158
@pytest.fixture
153159
def store(self) -> ElasticsearchStore:
154-
self._cleanup()
155160
return ElasticsearchStore(url=ES_URL, index_prefix="kv-store-e2e-test", native_storage=True)
156161

157162
def test_value_stored_as_flattened_object(self, store: ElasticsearchStore, es_client: Elasticsearch):
@@ -207,11 +212,10 @@ class TestElasticsearchStoreNonNativeMode(BaseTestElasticsearchStore):
207212
@override
208213
@pytest.fixture
209214
def store(self) -> ElasticsearchStore:
210-
self._cleanup()
211215
return ElasticsearchStore(url=ES_URL, index_prefix="kv-store-e2e-test", native_storage=False)
212216

213217
def test_value_stored_as_json_string(self, store: ElasticsearchStore, es_client: Elasticsearch):
214-
"""Verify values are stored as flattened objects, not JSON strings"""
218+
"""Verify values are stored as JSON strings"""
215219
store.put(collection="test", key="test_key", value={"name": "Alice", "age": 30})
216220

217221
index_name = store._sanitize_index_name(collection="test") # pyright: ignore[reportPrivateUsage]

0 commit comments

Comments
 (0)