Skip to content

Commit 8317d57

Browse files
authored
Fixes for Elasticsearch tests (#234)
1 parent 1aa915d commit 8317d57

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from collections.abc import AsyncGenerator
22
from datetime import datetime, timedelta, timezone
3+
from typing import TYPE_CHECKING, Any
34

45
import pytest
56
from dirty_equals import IsFloat, IsStr
@@ -19,6 +20,9 @@
1920
from tests.conftest import docker_container, should_skip_docker_tests
2021
from tests.stores.base import BaseStoreTests, ContextManagerStoreTestMixin
2122

23+
if TYPE_CHECKING:
24+
from elastic_transport._response import ObjectApiResponse
25+
2226
TEST_SIZE_LIMIT = 1 * 1024 * 1024 # 1MB
2327
ES_HOST = "localhost"
2428
ES_PORT = 9200
@@ -41,7 +45,12 @@ async def ping_elasticsearch() -> bool:
4145
es_client: AsyncElasticsearch = get_elasticsearch_client()
4246

4347
async with es_client:
44-
return await es_client.ping()
48+
if not await es_client.ping():
49+
return False
50+
51+
status: ObjectApiResponse[dict[str, Any]] = await es_client.options(ignore_status=404).cluster.health(wait_for_status="green")
52+
53+
return status.body.get("status") == "green"
4554

4655

4756
async def cleanup_elasticsearch_indices(elasticsearch_client: AsyncElasticsearch):
@@ -102,10 +111,7 @@ async def setup_elasticsearch(self, request: pytest.FixtureRequest) -> AsyncGene
102111
@pytest.fixture
103112
async def es_client(self) -> AsyncGenerator[AsyncElasticsearch, None]:
104113
async with AsyncElasticsearch(hosts=[ES_URL]) as es_client:
105-
try:
106-
yield es_client
107-
finally:
108-
await es_client.close()
114+
yield es_client
109115

110116
@override
111117
@pytest.fixture

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# DO NOT CHANGE! Change the original file instead.
44
from collections.abc import Generator
55
from datetime import datetime, timedelta, timezone
6+
from typing import TYPE_CHECKING, Any
67

78
import pytest
89
from dirty_equals import IsFloat, IsStr
@@ -22,6 +23,9 @@
2223
from tests.code_gen.conftest import docker_container, should_skip_docker_tests
2324
from tests.code_gen.stores.base import BaseStoreTests, ContextManagerStoreTestMixin
2425

26+
if TYPE_CHECKING:
27+
from elastic_transport._response import ObjectApiResponse
28+
2529
TEST_SIZE_LIMIT = 1 * 1024 * 1024 # 1MB
2630
ES_HOST = "localhost"
2731
ES_PORT = 9200
@@ -42,7 +46,12 @@ def ping_elasticsearch() -> bool:
4246
es_client: Elasticsearch = get_elasticsearch_client()
4347

4448
with es_client:
45-
return es_client.ping()
49+
if not es_client.ping():
50+
return False
51+
52+
status: ObjectApiResponse[dict[str, Any]] = es_client.options(ignore_status=404).cluster.health(wait_for_status="green")
53+
54+
return status.body.get("status") == "green"
4655

4756

4857
def cleanup_elasticsearch_indices(elasticsearch_client: Elasticsearch):
@@ -103,10 +112,7 @@ def setup_elasticsearch(self, request: pytest.FixtureRequest) -> Generator[None,
103112
@pytest.fixture
104113
def es_client(self) -> Generator[Elasticsearch, None, None]:
105114
with Elasticsearch(hosts=[ES_URL]) as es_client:
106-
try:
107-
yield es_client
108-
finally:
109-
es_client.close()
115+
yield es_client
110116

111117
@override
112118
@pytest.fixture

0 commit comments

Comments
 (0)