Skip to content

Commit 136b690

Browse files
committed
Convert to pytest test_clients/
1 parent 83a9198 commit 136b690

File tree

9 files changed

+336
-299
lines changed

9 files changed

+336
-299
lines changed

test_elasticsearch/test_async/test_server/test_clients.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,10 @@ async def test_indices_analyze(self, async_client):
2929

3030

3131
class TestBulk:
32-
async def test_bulk_works_with_string_body(self, async_client):
33-
docs = '{ "index" : { "_index" : "bulk_test_index", "_id" : "1" } }\n{"answer": 42}'
34-
response = await async_client.bulk(body=docs)
35-
36-
assert response["errors"] is False
37-
assert len(response["items"]) == 1
32+
docs = '{ "index" : { "_index" : "bulk_test_index", "_id" : "1" } }\n{"answer": 42}'
3833

39-
async def test_bulk_works_with_bytestring_body(self, async_client):
40-
docs = b'{ "index" : { "_index" : "bulk_test_index", "_id" : "2" } }\n{"answer": 42}'
34+
@pytest.mark.parametrize("docs", [docs, docs.encode("utf-8")])
35+
async def test_bulk_works_with_string_bytestring_body(self, docs, async_client):
4136
response = await async_client.bulk(body=docs)
4237

4338
assert response["errors"] is False

test_elasticsearch/test_client/__init__.py

Lines changed: 0 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -14,116 +14,3 @@
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17-
18-
from __future__ import unicode_literals
19-
20-
from elasticsearch.client import Elasticsearch, _normalize_hosts
21-
22-
from ..test_cases import ElasticsearchTestCase, TestCase
23-
24-
25-
class TestNormalizeHosts(TestCase):
26-
def test_none_uses_defaults(self):
27-
self.assertEqual([{}], _normalize_hosts(None))
28-
29-
def test_strings_are_used_as_hostnames(self):
30-
self.assertEqual([{"host": "elastic.co"}], _normalize_hosts(["elastic.co"]))
31-
32-
def test_strings_are_parsed_for_port_and_user(self):
33-
self.assertEqual(
34-
[
35-
{"host": "elastic.co", "port": 42},
36-
{"host": "elastic.co", "http_auth": "user:secre]"},
37-
],
38-
_normalize_hosts(["elastic.co:42", "user:secre%5D@elastic.co"]),
39-
)
40-
41-
def test_strings_are_parsed_for_scheme(self):
42-
self.assertEqual(
43-
[
44-
{"host": "elastic.co", "port": 42, "use_ssl": True},
45-
{
46-
"host": "elastic.co",
47-
"http_auth": "user:secret",
48-
"use_ssl": True,
49-
"port": 443,
50-
"url_prefix": "/prefix",
51-
},
52-
],
53-
_normalize_hosts(
54-
["https://elastic.co:42", "https://user:secret@elastic.co/prefix"]
55-
),
56-
)
57-
58-
def test_dicts_are_left_unchanged(self):
59-
self.assertEqual(
60-
[{"host": "local", "extra": 123}],
61-
_normalize_hosts([{"host": "local", "extra": 123}]),
62-
)
63-
64-
def test_single_string_is_wrapped_in_list(self):
65-
self.assertEqual([{"host": "elastic.co"}], _normalize_hosts("elastic.co"))
66-
67-
68-
class TestClient(ElasticsearchTestCase):
69-
def test_request_timeout_is_passed_through_unescaped(self):
70-
self.client.ping(request_timeout=0.1)
71-
calls = self.assert_url_called("HEAD", "/")
72-
self.assertEqual([({"request_timeout": 0.1}, {}, None)], calls)
73-
74-
def test_params_is_copied_when(self):
75-
rt = object()
76-
params = dict(request_timeout=rt)
77-
self.client.ping(params=params)
78-
self.client.ping(params=params)
79-
calls = self.assert_url_called("HEAD", "/", 2)
80-
self.assertEqual(
81-
[({"request_timeout": rt}, {}, None), ({"request_timeout": rt}, {}, None)],
82-
calls,
83-
)
84-
self.assertFalse(calls[0][0] is calls[1][0])
85-
86-
def test_headers_is_copied_when(self):
87-
hv = "value"
88-
headers = dict(Authentication=hv)
89-
self.client.ping(headers=headers)
90-
self.client.ping(headers=headers)
91-
calls = self.assert_url_called("HEAD", "/", 2)
92-
self.assertEqual(
93-
[({}, {"authentication": hv}, None), ({}, {"authentication": hv}, None)],
94-
calls,
95-
)
96-
self.assertFalse(calls[0][0] is calls[1][0])
97-
98-
def test_from_in_search(self):
99-
self.client.search(index="i", from_=10)
100-
calls = self.assert_url_called("POST", "/i/_search")
101-
self.assertEqual([({"from": "10"}, {}, None)], calls)
102-
103-
def test_repr_contains_hosts(self):
104-
self.assertEqual("<Elasticsearch([{}])>", repr(self.client))
105-
106-
def test_repr_subclass(self):
107-
class OtherElasticsearch(Elasticsearch):
108-
pass
109-
110-
self.assertEqual("<OtherElasticsearch([{}])>", repr(OtherElasticsearch()))
111-
112-
def test_repr_contains_hosts_passed_in(self):
113-
self.assertIn("es.org", repr(Elasticsearch(["es.org:123"])))
114-
115-
def test_repr_truncates_host_to_5(self):
116-
hosts = [{"host": "es" + str(i)} for i in range(10)]
117-
es = Elasticsearch(hosts)
118-
self.assertNotIn("es5", repr(es))
119-
self.assertIn("...", repr(es))
120-
121-
def test_index_uses_post_if_id_is_empty(self):
122-
self.client.index(index="my-index", id="", body={})
123-
124-
self.assert_url_called("POST", "/my-index/_doc")
125-
126-
def test_index_uses_put_if_id_is_not_empty(self):
127-
self.client.index(index="my-index", id=0, body={})
128-
129-
self.assert_url_called("PUT", "/my-index/_doc/0")
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
from collections import defaultdict
19+
20+
from elasticsearch.connection import Connection
21+
22+
23+
class DummyTransport(Connection):
24+
def __init__(self, hosts, **kwargs):
25+
self.hosts = hosts
26+
self.responses = kwargs.pop("responses", None)
27+
self.call_count = 0
28+
self.calls = defaultdict(list)
29+
30+
def perform_request(self, method, url, params=None, headers=None, body=None):
31+
resp = 200, {}
32+
if self.responses:
33+
resp = self.responses[self.call_count]
34+
self.call_count += 1
35+
self.calls[(method, url)].append((params, headers, body))
36+
return resp
37+
38+
39+
def assert_helper(client, method, url, count=1):
40+
calls = client.transport.calls
41+
assert (method, url) in calls
42+
assert count == len(calls[(method, url)])
43+
return calls[(method, url)]
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
19+
import pytest
20+
21+
from elasticsearch.client import Elasticsearch
22+
23+
from .common import DummyTransport, assert_helper
24+
25+
26+
class TestClient:
27+
def test_request_timeout_is_passed_through_unescaped(self):
28+
client = Elasticsearch(transport_class=DummyTransport)
29+
client.ping(request_timeout=0.1)
30+
calls = assert_helper(client, "HEAD", "/")
31+
assert [({"request_timeout": 0.1}, {}, None)] == calls
32+
33+
def test_params_is_copied_when(self):
34+
client = Elasticsearch(transport_class=DummyTransport)
35+
params = {"request_timeout": object()}
36+
client.ping(params=params)
37+
client.ping(params=params)
38+
calls = assert_helper(client, "HEAD", "/", 2)
39+
assert [(params, {}, None), (params, {}, None)] == calls
40+
assert calls[0][0] is not calls[1][0]
41+
42+
def test_headers_is_copied_when(self):
43+
client = Elasticsearch(transport_class=DummyTransport)
44+
headers = {"authentication": "value"}
45+
client.ping(headers=headers)
46+
client.ping(headers=headers)
47+
calls = assert_helper(client, "HEAD", "/", 2)
48+
assert [({}, headers, None), ({}, headers, None)] == calls
49+
assert calls[0][0] is not calls[1][0]
50+
51+
def test_from_in_search(self):
52+
client = Elasticsearch(transport_class=DummyTransport)
53+
client.search(index="i", from_=10)
54+
calls = assert_helper(client, "POST", "/i/_search")
55+
assert [({"from": "10"}, {}, None)] == calls
56+
57+
def test_repr_contains_hosts(self):
58+
client = Elasticsearch(transport_class=DummyTransport)
59+
assert "<Elasticsearch([{}])>" == repr(client)
60+
61+
def test_repr_subclass(self):
62+
class OtherElasticsearch(Elasticsearch):
63+
pass
64+
65+
assert "<OtherElasticsearch([{}])>" == repr(OtherElasticsearch())
66+
67+
def test_repr_contains_hosts_passed_in(self):
68+
assert "es.org" in repr(Elasticsearch(["es.org:123"]))
69+
70+
def test_repr_truncates_host_to_5(self):
71+
hosts = [{"host": "es" + str(i)} for i in range(10)]
72+
es = Elasticsearch(hosts)
73+
assert '{"host": "es5"}' not in repr(es)
74+
assert "..." in repr(es)
75+
76+
@pytest.mark.parametrize(
77+
["id", "request_method", "url"],
78+
[("", "POST", "/my-index/_doc"), (0, "PUT", "/my-index/_doc/0")],
79+
)
80+
def test_index_uses_id(self, id, request_method, url):
81+
client = Elasticsearch(transport_class=DummyTransport)
82+
client.index(index="my-index", id=id, body={})
83+
84+
assert_helper(client, request_method, url)

test_elasticsearch/test_client/test_cluster.py

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,38 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
from test_elasticsearch.test_cases import ElasticsearchTestCase
19-
20-
21-
class TestCluster(ElasticsearchTestCase):
22-
def test_stats_without_node_id(self):
23-
self.client.cluster.stats()
24-
self.assert_url_called("GET", "/_cluster/stats")
25-
26-
def test_stats_with_node_id(self):
27-
self.client.cluster.stats("node-1")
28-
self.assert_url_called("GET", "/_cluster/stats/nodes/node-1")
29-
30-
self.client.cluster.stats(node_id="node-2")
31-
self.assert_url_called("GET", "/_cluster/stats/nodes/node-2")
32-
33-
def test_state_with_index_without_metric_defaults_to_all(self):
34-
self.client.cluster.state()
35-
self.assert_url_called("GET", "/_cluster/state")
36-
37-
self.client.cluster.state(metric="cluster_name")
38-
self.assert_url_called("GET", "/_cluster/state/cluster_name")
39-
40-
self.client.cluster.state(index="index-1")
41-
self.assert_url_called("GET", "/_cluster/state/_all/index-1")
42-
43-
self.client.cluster.state(index="index-1", metric="cluster_name")
44-
self.assert_url_called("GET", "/_cluster/state/cluster_name/index-1")
18+
import pytest
19+
20+
from elasticsearch.client import Elasticsearch
21+
22+
from .common import DummyTransport, assert_helper
23+
24+
25+
class TestCluster:
26+
@pytest.mark.parametrize("node_id", [None, "node-1", "node-2"])
27+
def test_stats_node_id(self, node_id):
28+
client = Elasticsearch(transport_class=DummyTransport)
29+
client.cluster.stats(node_id=node_id)
30+
url = "/_cluster/stats"
31+
if node_id:
32+
url += "/nodes/" + node_id
33+
assert_helper(client, "GET", url)
34+
35+
@pytest.mark.parametrize(
36+
["index", "metric", "url_suffix"],
37+
[
38+
(None, None, None),
39+
(None, "cluster_name", "/cluster_name"),
40+
("index-1", None, "/_all/index-1"),
41+
("index-1", "cluster_name", "/cluster_name/index-1"),
42+
],
43+
)
44+
def test_state_with_index_without_metric_defaults_to_all(
45+
self, index, metric, url_suffix
46+
):
47+
client = Elasticsearch(transport_class=DummyTransport)
48+
client.cluster.state(index=index, metric=metric)
49+
url = "/_cluster/state"
50+
if url_suffix:
51+
url += url_suffix
52+
assert_helper(client, "GET", url)

test_elasticsearch/test_client/test_indices.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,31 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
from test_elasticsearch.test_cases import ElasticsearchTestCase
18+
import pytest
1919

20+
from elasticsearch.client import Elasticsearch
2021

21-
class TestIndices(ElasticsearchTestCase):
22+
from .common import DummyTransport, assert_helper
23+
24+
25+
class TestIndices:
2226
def test_create_one_index(self):
23-
self.client.indices.create("test-index")
24-
self.assert_url_called("PUT", "/test-index")
27+
client = Elasticsearch(transport_class=DummyTransport)
28+
client.indices.create("test-index")
29+
assert_helper(client, "PUT", "/test-index")
2530

2631
def test_delete_multiple_indices(self):
27-
self.client.indices.delete(["test-index", "second.index", "third/index"])
28-
self.assert_url_called("DELETE", "/test-index,second.index,third%2Findex")
32+
client = Elasticsearch(transport_class=DummyTransport)
33+
client.indices.delete(["test-index", "second.index", "third/index"])
34+
assert_helper(client, "DELETE", "/test-index,second.index,third%2Findex")
2935

3036
def test_exists_index(self):
31-
self.client.indices.exists("second.index,third/index")
32-
self.assert_url_called("HEAD", "/second.index,third%2Findex")
37+
client = Elasticsearch(transport_class=DummyTransport)
38+
client.indices.exists("second.index,third/index")
39+
assert_helper(client, "HEAD", "/second.index,third%2Findex")
3340

34-
def test_passing_empty_value_for_required_param_raises_exception(self):
35-
self.assertRaises(ValueError, self.client.indices.exists, index=None)
36-
self.assertRaises(ValueError, self.client.indices.exists, index=[])
37-
self.assertRaises(ValueError, self.client.indices.exists, index="")
41+
@pytest.mark.parametrize("index", [None, [], ""])
42+
def test_passing_empty_value_for_required_param_raises_exception(self, index):
43+
client = Elasticsearch(transport_class=DummyTransport)
44+
with pytest.raises(ValueError):
45+
client.indices.exists(index=index)

0 commit comments

Comments
 (0)