Skip to content

Commit 4c1906a

Browse files
committed
Refactor tests
1 parent df45d85 commit 4c1906a

File tree

10 files changed

+287
-291
lines changed

10 files changed

+287
-291
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ The process for contributing to any of the Elasticsearch repositories is similar
5050

5151
2. Run the linter and test suite to ensure your changes do not break existing code:
5252

53-
```
53+
```bash
5454
# Install Nox for task management
5555
$ python -m pip install nox
5656

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 & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -14,109 +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 DummyTransportTestCase
23-
24-
25-
class TestNormalizeHosts:
26-
def test_none_uses_defaults(self):
27-
assert [{}] == _normalize_hosts(None)
28-
29-
def test_strings_are_used_as_hostnames(self):
30-
assert [{"host": "elastic.co"}] == _normalize_hosts(["elastic.co"])
31-
32-
def test_strings_are_parsed_for_port_and_user(self):
33-
assert [
34-
{"host": "elastic.co", "port": 42},
35-
{"host": "elastic.co", "http_auth": "user:secre]"},
36-
] == _normalize_hosts(["elastic.co:42", "user:secre%[email protected]"])
37-
38-
def test_strings_are_parsed_for_scheme(self):
39-
assert [
40-
{"host": "elastic.co", "port": 42, "use_ssl": True},
41-
{
42-
"host": "elastic.co",
43-
"http_auth": "user:secret",
44-
"use_ssl": True,
45-
"port": 443,
46-
"url_prefix": "/prefix",
47-
},
48-
] == _normalize_hosts(
49-
["https://elastic.co:42", "https://user:[email protected]/prefix"]
50-
)
51-
52-
def test_dicts_are_left_unchanged(self):
53-
assert [{"host": "local", "extra": 123}] == _normalize_hosts(
54-
[{"host": "local", "extra": 123}]
55-
)
56-
57-
def test_single_string_is_wrapped_in_list(self):
58-
assert [{"host": "elastic.co"}] == _normalize_hosts("elastic.co")
59-
60-
61-
class TestClient(DummyTransportTestCase):
62-
def test_request_timeout_is_passed_through_unescaped(self):
63-
self.client.ping(request_timeout=0.1)
64-
calls = self.assert_url_called("HEAD", "/")
65-
assert [({"request_timeout": 0.1}, {}, None)] == calls
66-
67-
def test_params_is_copied_when(self):
68-
rt = object()
69-
params = dict(request_timeout=rt)
70-
self.client.ping(params=params)
71-
self.client.ping(params=params)
72-
calls = self.assert_url_called("HEAD", "/", 2)
73-
assert [
74-
({"request_timeout": rt}, {}, None),
75-
({"request_timeout": rt}, {}, None),
76-
] == calls
77-
assert not (calls[0][0] is calls[1][0])
78-
79-
def test_headers_is_copied_when(self):
80-
hv = "value"
81-
headers = dict(Authentication=hv)
82-
self.client.ping(headers=headers)
83-
self.client.ping(headers=headers)
84-
calls = self.assert_url_called("HEAD", "/", 2)
85-
assert [
86-
({}, {"authentication": hv}, None),
87-
({}, {"authentication": hv}, None),
88-
] == calls
89-
assert not (calls[0][0] is calls[1][0])
90-
91-
def test_from_in_search(self):
92-
self.client.search(index="i", from_=10)
93-
calls = self.assert_url_called("POST", "/i/_search")
94-
assert [({"from": "10"}, {}, None)] == calls
95-
96-
def test_repr_contains_hosts(self):
97-
assert "<Elasticsearch([{}])>" == repr(self.client)
98-
99-
def test_repr_subclass(self):
100-
class OtherElasticsearch(Elasticsearch):
101-
pass
102-
103-
assert "<OtherElasticsearch([{}])>" == repr(OtherElasticsearch())
104-
105-
def test_repr_contains_hosts_passed_in(self):
106-
assert "es.org" in repr(Elasticsearch(["es.org:123"]))
107-
108-
def test_repr_truncates_host_to_5(self):
109-
hosts = [{"host": "es" + str(i)} for i in range(10)]
110-
es = Elasticsearch(hosts)
111-
assert "es5" not in repr(es)
112-
assert "..." in repr(es)
113-
114-
def test_index_uses_post_if_id_is_empty(self):
115-
self.client.index(index="my-index", id="", body={})
116-
117-
self.assert_url_called("POST", "/my-index/_doc")
118-
119-
def test_index_uses_put_if_id_is_not_empty(self):
120-
self.client.index(index="my-index", id=0, body={})
121-
122-
self.assert_url_called("PUT", "/my-index/_doc/0")

test_elasticsearch/test_cases.py renamed to test_elasticsearch/test_client/common.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717

1818
from collections import defaultdict
1919

20-
from elasticsearch import Elasticsearch
20+
from elasticsearch.connection import Connection
2121

2222

23-
class DummyTransport(object):
24-
def __init__(self, hosts, responses=None, **_):
23+
class DummyTransport(Connection):
24+
def __init__(self, hosts, **kwargs):
2525
self.hosts = hosts
26-
self.responses = responses
26+
self.responses = kwargs.pop("responses", None)
2727
self.call_count = 0
2828
self.calls = defaultdict(list)
2929

@@ -36,15 +36,8 @@ def perform_request(self, method, url, params=None, headers=None, body=None):
3636
return resp
3737

3838

39-
class DummyTransportTestCase:
40-
def setup_method(self, _):
41-
self.client = Elasticsearch(transport_class=DummyTransport)
42-
43-
def assert_call_count_equals(self, count):
44-
assert count == self.client.transport.call_count
45-
46-
def assert_url_called(self, method, url, count=1):
47-
assert (method, url) in self.client.transport.calls
48-
calls = self.client.transport.calls[(method, url)]
49-
assert count == len(calls)
50-
return calls
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": b"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 DummyTransportTestCase
19-
20-
21-
class TestCluster(DummyTransportTestCase):
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: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,29 @@
1717

1818
import pytest
1919

20-
from test_elasticsearch.test_cases import DummyTransportTestCase
20+
from elasticsearch.client import Elasticsearch
2121

22+
from .common import DummyTransport, assert_helper
2223

23-
class TestIndices(DummyTransportTestCase):
24+
25+
class TestIndices:
2426
def test_create_one_index(self):
25-
self.client.indices.create("test-index")
26-
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")
2730

2831
def test_delete_multiple_indices(self):
29-
self.client.indices.delete(["test-index", "second.index", "third/index"])
30-
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")
3135

3236
def test_exists_index(self):
33-
self.client.indices.exists("second.index,third/index")
34-
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")
3540

36-
def test_passing_empty_value_for_required_param_raises_exception(self):
37-
with pytest.raises(ValueError):
38-
self.client.indices.exists(index=None)
39-
with pytest.raises(ValueError):
40-
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)
4144
with pytest.raises(ValueError):
42-
self.client.indices.exists(index="")
45+
client.indices.exists(index=index)

0 commit comments

Comments
 (0)