Skip to content

Commit 01037c2

Browse files
committed
Add pytest for async REST API tests
1 parent d0ad40d commit 01037c2

File tree

16 files changed

+508
-122
lines changed

16 files changed

+508
-122
lines changed

elasticsearch/_async/client/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,6 +1297,7 @@ async def rank_eval(self, body, index=None, params=None, headers=None):
12971297

12981298
@query_params(
12991299
"max_docs",
1300+
"prefer_v2_templates",
13001301
"refresh",
13011302
"requests_per_second",
13021303
"scroll",
@@ -1316,6 +1317,8 @@ async def reindex(self, body, params=None, headers=None):
13161317
prototype for the index request.
13171318
:arg max_docs: Maximum number of documents to process (default:
13181319
all documents)
1320+
:arg prefer_v2_templates: favor V2 templates instead of V1
1321+
templates during index creation
13191322
:arg refresh: Should the affected indexes be refreshed?
13201323
:arg requests_per_second: The throttle to set on this request in
13211324
sub-requests per second. -1 means no throttle.

elasticsearch/_async/client/cat.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ async def pending_tasks(self, params=None, headers=None):
326326
"GET", "/_cat/pending_tasks", params=params, headers=headers
327327
)
328328

329-
@query_params("format", "h", "help", "local", "master_timeout", "s", "size", "v")
329+
@query_params("format", "h", "help", "local", "master_timeout", "s", "time", "v")
330330
async def thread_pool(self, thread_pool_patterns=None, params=None, headers=None):
331331
"""
332332
Returns cluster-wide thread pool statistics per node. By default the active,
@@ -345,8 +345,8 @@ async def thread_pool(self, thread_pool_patterns=None, params=None, headers=None
345345
to master node
346346
:arg s: Comma-separated list of column names or column aliases
347347
to sort by
348-
:arg size: The multiplier in which to display values Valid
349-
choices: , k, m, g, t, p
348+
:arg time: The unit in which to display time values Valid
349+
choices: d, h, m, s, ms, micros, nanos
350350
:arg v: Verbose mode. Display column headers
351351
"""
352352
return await self.transport.perform_request(

elasticsearch/_async/client/cluster.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ async def stats(self, node_id=None, params=None, headers=None):
137137
"GET",
138138
"/_cluster/stats"
139139
if node_id in SKIP_IN_PATH
140-
else _make_path("_cluster/stats/nodes", node_id),
140+
else _make_path("_cluster", "stats", "nodes", node_id),
141141
params=params,
142142
headers=headers,
143143
)

elasticsearch/_async/client/indices.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,20 +1301,19 @@ async def get_index_template(self, name=None, params=None, headers=None):
13011301
"GET", _make_path("_index_template", name), params=params, headers=headers
13021302
)
13031303

1304-
@query_params("create", "master_timeout", "order")
1304+
@query_params("cause", "create", "master_timeout")
13051305
async def put_index_template(self, name, body, params=None, headers=None):
13061306
"""
13071307
Creates or updates an index template.
13081308
`<https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-templates.html>`_
13091309
13101310
:arg name: The name of the template
13111311
:arg body: The template definition
1312+
:arg cause: User defined reason for creating/updating the index
1313+
template
13121314
:arg create: Whether the index template should only be added if
13131315
new or can also replace an existing one
13141316
:arg master_timeout: Specify timeout for connection to master
1315-
:arg order: The order for this template when merging multiple
1316-
matching ones (higher numbers are merged later, overriding the lower
1317-
numbers)
13181317
"""
13191318
for param in (name, body):
13201319
if param in SKIP_IN_PATH:
@@ -1349,7 +1348,7 @@ async def exists_index_template(self, name, params=None, headers=None):
13491348
"HEAD", _make_path("_index_template", name), params=params, headers=headers
13501349
)
13511350

1352-
@query_params("master_timeout")
1351+
@query_params("cause", "create", "master_timeout")
13531352
async def simulate_index_template(self, name, body=None, params=None, headers=None):
13541353
"""
13551354
Simulate matching the given index name against the index templates in the
@@ -1360,6 +1359,11 @@ async def simulate_index_template(self, name, body=None, params=None, headers=No
13601359
name)
13611360
:arg body: New index template definition, which will be included
13621361
in the simulation, as if it already exists in the system
1362+
:arg cause: User defined reason for dry-run creating the new
1363+
template for simulation purposes
1364+
:arg create: Whether the index template we optionally defined in
1365+
the body should only be dry-run added if new or can also replace an
1366+
existing one
13631367
:arg master_timeout: Specify timeout for connection to master
13641368
"""
13651369
if name in SKIP_IN_PATH:

elasticsearch/client/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,6 +1297,7 @@ def rank_eval(self, body, index=None, params=None, headers=None):
12971297

12981298
@query_params(
12991299
"max_docs",
1300+
"prefer_v2_templates",
13001301
"refresh",
13011302
"requests_per_second",
13021303
"scroll",
@@ -1316,6 +1317,8 @@ def reindex(self, body, params=None, headers=None):
13161317
prototype for the index request.
13171318
:arg max_docs: Maximum number of documents to process (default:
13181319
all documents)
1320+
:arg prefer_v2_templates: favor V2 templates instead of V1
1321+
templates during index creation
13191322
:arg refresh: Should the affected indexes be refreshed?
13201323
:arg requests_per_second: The throttle to set on this request in
13211324
sub-requests per second. -1 means no throttle.

elasticsearch/client/cat.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ def pending_tasks(self, params=None, headers=None):
326326
"GET", "/_cat/pending_tasks", params=params, headers=headers
327327
)
328328

329-
@query_params("format", "h", "help", "local", "master_timeout", "s", "size", "v")
329+
@query_params("format", "h", "help", "local", "master_timeout", "s", "time", "v")
330330
def thread_pool(self, thread_pool_patterns=None, params=None, headers=None):
331331
"""
332332
Returns cluster-wide thread pool statistics per node. By default the active,
@@ -345,8 +345,8 @@ def thread_pool(self, thread_pool_patterns=None, params=None, headers=None):
345345
to master node
346346
:arg s: Comma-separated list of column names or column aliases
347347
to sort by
348-
:arg size: The multiplier in which to display values Valid
349-
choices: , k, m, g, t, p
348+
:arg time: The unit in which to display time values Valid
349+
choices: d, h, m, s, ms, micros, nanos
350350
:arg v: Verbose mode. Display column headers
351351
"""
352352
return self.transport.perform_request(

elasticsearch/client/indices.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,20 +1299,19 @@ def get_index_template(self, name=None, params=None, headers=None):
12991299
"GET", _make_path("_index_template", name), params=params, headers=headers
13001300
)
13011301

1302-
@query_params("create", "master_timeout", "order")
1302+
@query_params("cause", "create", "master_timeout")
13031303
def put_index_template(self, name, body, params=None, headers=None):
13041304
"""
13051305
Creates or updates an index template.
13061306
`<https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-templates.html>`_
13071307
13081308
:arg name: The name of the template
13091309
:arg body: The template definition
1310+
:arg cause: User defined reason for creating/updating the index
1311+
template
13101312
:arg create: Whether the index template should only be added if
13111313
new or can also replace an existing one
13121314
:arg master_timeout: Specify timeout for connection to master
1313-
:arg order: The order for this template when merging multiple
1314-
matching ones (higher numbers are merged later, overriding the lower
1315-
numbers)
13161315
"""
13171316
for param in (name, body):
13181317
if param in SKIP_IN_PATH:
@@ -1347,7 +1346,7 @@ def exists_index_template(self, name, params=None, headers=None):
13471346
"HEAD", _make_path("_index_template", name), params=params, headers=headers
13481347
)
13491348

1350-
@query_params("master_timeout")
1349+
@query_params("cause", "create", "master_timeout")
13511350
def simulate_index_template(self, name, body=None, params=None, headers=None):
13521351
"""
13531352
Simulate matching the given index name against the index templates in the
@@ -1358,6 +1357,11 @@ def simulate_index_template(self, name, body=None, params=None, headers=None):
13581357
name)
13591358
:arg body: New index template definition, which will be included
13601359
in the simulation, as if it already exists in the system
1360+
:arg cause: User defined reason for dry-run creating the new
1361+
template for simulation purposes
1362+
:arg create: Whether the index template we optionally defined in
1363+
the body should only be dry-run added if new or can also replace an
1364+
existing one
13611365
:arg master_timeout: Specify timeout for connection to master
13621366
"""
13631367
if name in SKIP_IN_PATH:

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
],
6363
python_requires=">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, <4",
6464
install_requires=install_requires,
65+
test_suite="test_elasticsearch.run_tests.run_all",
6566
tests_require=tests_require,
6667
extras_require={
6768
"develop": tests_require + docs_require + generate_require,

test_elasticsearch/run_tests.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,14 @@ def run_all(argv=None):
7878
"--log-level=DEBUG",
7979
"--cache-clear",
8080
"-vv",
81-
abspath(dirname(__file__)),
8281
]
8382

83+
# Skip all async tests unless Python 3.6+
84+
if sys.version_info < (3, 6):
85+
argv.append("--ignore=test_elasticsearch/test_async/")
86+
87+
argv.append(abspath(dirname(__file__)))
88+
8489
exit_code = 0
8590
try:
8691
subprocess.check_call(argv, stdout=sys.stdout, stderr=sys.stderr)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Licensed to Elasticsearch B.V under one or more agreements.
2+
# Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
# See the LICENSE file in the project root for more information
4+
5+
import sys
6+
import pytest
7+
8+
pytestmark = pytest.mark.skipif(
9+
sys.version_info < (3, 6), reason="'test_async' is only run on Python 3.6+"
10+
)

test_elasticsearch/test_async/test_connection.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,25 @@
44
# See the LICENSE file in the project root for more information
55

66
import ssl
7+
import gzip
8+
import io
79
from mock import Mock, patch
810
import warnings
911
from platform import python_version
12+
import aiohttp
13+
import pytest
1014

1115
from elasticsearch import AIOHttpConnection
1216
from elasticsearch import __versionstr__
1317
from ..test_cases import TestCase, SkipTest
1418

19+
pytestmark = pytest.mark.asyncio
20+
21+
22+
def gzip_decompress(data):
23+
buf = gzip.GzipFile(fileobj=io.BytesIO(data), mode="rb")
24+
return buf.read()
25+
1526

1627
class TestAIOHttpConnection(TestCase):
1728
async def _get_mock_connection(self, connection_params={}, response_body=b"{}"):
@@ -232,14 +243,15 @@ def test_uses_https_if_verify_certs_is_off(self):
232243
self.assertEqual(con.scheme, "https")
233244
self.assertEqual(con.host, "https://localhost:9200")
234245

235-
def nowarn_when_test_uses_https_if_verify_certs_is_off(self):
246+
async def test_nowarn_when_test_uses_https_if_verify_certs_is_off(self):
236247
with warnings.catch_warnings(record=True) as w:
237-
con = Urllib3HttpConnection(
248+
con = AIOHttpConnection(
238249
use_ssl=True, verify_certs=False, ssl_show_warn=False
239250
)
251+
con._create_aiohttp_session()
240252
self.assertEqual(0, len(w))
241253

242-
self.assertIsInstance(con.pool, urllib3.HTTPSConnectionPool)
254+
self.assertIsInstance(con.session, aiohttp.ClientSession)
243255

244256
def test_doesnt_use_https_if_not_specified(self):
245257
con = AIOHttpConnection()

test_elasticsearch/test_async/test_connection_pool.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# See the LICENSE file in the project root for more information
44

55
import time
6+
import pytest
67

78
from elasticsearch import (
89
AsyncConnectionPool,
@@ -15,6 +16,9 @@
1516
from ..test_cases import TestCase
1617

1718

19+
pytestmark = pytest.mark.asyncio
20+
21+
1822
class TestConnectionPool(TestCase):
1923
def test_dummy_cp_raises_exception_on_more_connections(self):
2024
self.assertRaises(ImproperlyConfigured, AsyncDummyConnectionPool, [])

test_elasticsearch/test_async/test_helpers.py

Lines changed: 0 additions & 96 deletions
This file was deleted.

0 commit comments

Comments
 (0)