Skip to content

Commit ea43c06

Browse files
dsl testing fixes
1 parent 5fae88a commit ea43c06

File tree

3 files changed

+27
-49
lines changed

3 files changed

+27
-49
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ dev = [
5959
"aiohttp",
6060
"pytest",
6161
"pytest-cov",
62+
"pytest-mock",
6263
"pytest-asyncio",
6364
"coverage",
6465
"jinja2",

test_elasticsearch/test_dsl/conftest.py

Lines changed: 25 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import time
2323
from datetime import datetime
2424
from typing import Any, AsyncGenerator, Dict, Generator, Tuple, cast
25-
from unittest import SkipTest, TestCase
25+
from unittest import SkipTest
2626
from unittest.mock import AsyncMock, Mock
2727

2828
import pytest_asyncio
@@ -37,6 +37,7 @@
3737
from elasticsearch.exceptions import ConnectionError
3838
from elasticsearch.helpers import bulk
3939

40+
from ..utils import CA_CERTS
4041
from .test_integration._async import test_document as async_document
4142
from .test_integration._sync import test_document as sync_document
4243
from .test_integration.test_data import (
@@ -47,21 +48,21 @@
4748
create_git_index,
4849
)
4950

50-
if "ELASTICSEARCH_URL" in os.environ:
51-
ELASTICSEARCH_URL = os.environ["ELASTICSEARCH_URL"]
52-
else:
53-
ELASTICSEARCH_URL = "http://localhost:9200"
5451

55-
56-
def get_test_client(wait: bool = True, **kwargs: Any) -> Elasticsearch:
52+
def get_test_client(
53+
elasticsearch_url, wait: bool = True, **kwargs: Any
54+
) -> Elasticsearch:
5755
# construct kwargs from the environment
5856
kw: Dict[str, Any] = {"request_timeout": 30}
5957

58+
if elasticsearch_url.startswith("https://"):
59+
kw["ca_certs"] = CA_CERTS
60+
6061
if "PYTHON_CONNECTION_CLASS" in os.environ:
6162
kw["node_class"] = os.environ["PYTHON_CONNECTION_CLASS"]
6263

6364
kw.update(kwargs)
64-
client = Elasticsearch(ELASTICSEARCH_URL, **kw)
65+
client = Elasticsearch(elasticsearch_url, **kw)
6566

6667
# wait for yellow status
6768
for tries_left in range(100 if wait else 1, 0, -1):
@@ -76,15 +77,17 @@ def get_test_client(wait: bool = True, **kwargs: Any) -> Elasticsearch:
7677
raise SkipTest("Elasticsearch failed to start.")
7778

7879

79-
async def get_async_test_client(wait: bool = True, **kwargs: Any) -> AsyncElasticsearch:
80+
async def get_async_test_client(
81+
elasticsearch_url, wait: bool = True, **kwargs: Any
82+
) -> AsyncElasticsearch:
8083
# construct kwargs from the environment
8184
kw: Dict[str, Any] = {"request_timeout": 30}
8285

83-
if "PYTHON_CONNECTION_CLASS" in os.environ:
84-
kw["node_class"] = os.environ["PYTHON_CONNECTION_CLASS"]
86+
if elasticsearch_url.startswith("https://"):
87+
kw["ca_certs"] = CA_CERTS
8588

8689
kw.update(kwargs)
87-
client = AsyncElasticsearch(ELASTICSEARCH_URL, **kw)
90+
client = AsyncElasticsearch(elasticsearch_url, **kw)
8891

8992
# wait for yellow status
9093
for tries_left in range(100 if wait else 1, 0, -1):
@@ -100,36 +103,6 @@ async def get_async_test_client(wait: bool = True, **kwargs: Any) -> AsyncElasti
100103
raise SkipTest("Elasticsearch failed to start.")
101104

102105

103-
class ElasticsearchTestCase(TestCase):
104-
client: Elasticsearch
105-
106-
@staticmethod
107-
def _get_client() -> Elasticsearch:
108-
return get_test_client()
109-
110-
@classmethod
111-
def setup_class(cls) -> None:
112-
cls.client = cls._get_client()
113-
114-
def teardown_method(self, _: Any) -> None:
115-
# Hidden indices expanded in wildcards in ES 7.7
116-
expand_wildcards = ["open", "closed"]
117-
if self.es_version() >= (7, 7):
118-
expand_wildcards.append("hidden")
119-
120-
self.client.indices.delete_data_stream(
121-
name="*", expand_wildcards=expand_wildcards
122-
)
123-
self.client.indices.delete(index="*", expand_wildcards=expand_wildcards)
124-
self.client.indices.delete_template(name="*")
125-
self.client.indices.delete_index_template(name="*")
126-
127-
def es_version(self) -> Tuple[int, ...]:
128-
if not hasattr(self, "_es_version"):
129-
self._es_version = _get_version(self.client.info()["version"]["number"])
130-
return self._es_version
131-
132-
133106
def _get_version(version_string: str) -> Tuple[int, ...]:
134107
if "." not in version_string:
135108
return ()
@@ -138,19 +111,23 @@ def _get_version(version_string: str) -> Tuple[int, ...]:
138111

139112

140113
@fixture(scope="session")
141-
def client() -> Elasticsearch:
114+
def client(elasticsearch_url) -> Elasticsearch:
142115
try:
143-
connection = get_test_client(wait="WAIT_FOR_ES" in os.environ)
116+
connection = get_test_client(
117+
elasticsearch_url, wait="WAIT_FOR_ES" in os.environ
118+
)
144119
add_connection("default", connection)
145120
return connection
146121
except SkipTest:
147122
skip()
148123

149124

150125
@pytest_asyncio.fixture
151-
async def async_client() -> AsyncGenerator[AsyncElasticsearch, None]:
126+
async def async_client(elasticsearch_url) -> AsyncGenerator[AsyncElasticsearch, None]:
152127
try:
153-
connection = await get_async_test_client(wait="WAIT_FOR_ES" in os.environ)
128+
connection = await get_async_test_client(
129+
elasticsearch_url, wait="WAIT_FOR_ES" in os.environ
130+
)
154131
add_async_connection("default", connection)
155132
yield connection
156133
await connection.close()
@@ -224,8 +201,8 @@ def data_client(client: Elasticsearch) -> Generator[Elasticsearch, None, None]:
224201
bulk(client, DATA, raise_on_error=True, refresh=True)
225202
bulk(client, FLAT_DATA, raise_on_error=True, refresh=True)
226203
yield client
227-
client.indices.delete(index="git")
228-
client.indices.delete(index="flat-git")
204+
client.options(ignore_status=404).indices.delete(index="git")
205+
client.options(ignore_status=404).indices.delete(index="flat-git")
229206

230207

231208
@pytest_asyncio.fixture

utils/run-unasync-dsl.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def main(check=False):
3737
("test_elasticsearch/test_dsl/_async/", "test_elasticsearch/test_dsl/_sync/"),
3838
(
3939
"test_elasticsearch/test_dsl/test_integration/_async/",
40-
"test_elasticsearch/test/dsl/test_integration/_sync/",
40+
"test_elasticsearch/test_dsl/test_integration/_sync/",
4141
),
4242
(
4343
"test_elasticsearch/test_dsl/test_integration/test_examples/_async/",

0 commit comments

Comments
 (0)