Files
opensearch-pyd/test_elasticsearch/test_client/__init__.py
T

100 lines
3.4 KiB
Python
Raw Normal View History

2014-05-29 01:16:17 +02:00
from __future__ import unicode_literals
from elasticsearch.client import _normalize_hosts, Elasticsearch
2013-05-04 20:00:39 +02:00
from ..test_cases import TestCase, ElasticsearchTestCase
2013-08-28 19:11:28 +02:00
2019-03-29 09:25:23 -06:00
2013-05-04 20:00:39 +02:00
class TestNormalizeHosts(TestCase):
def test_none_uses_defaults(self):
self.assertEquals([{}], _normalize_hosts(None))
def test_strings_are_used_as_hostnames(self):
2015-03-23 15:25:45 -07:00
self.assertEquals([{"host": "elastic.co"}], _normalize_hosts(["elastic.co"]))
2013-05-04 20:00:39 +02:00
2014-11-13 11:47:54 +01:00
def test_strings_are_parsed_for_port_and_user(self):
2013-05-04 20:00:39 +02:00
self.assertEquals(
2019-03-29 09:25:23 -06:00
[
{"host": "elastic.co", "port": 42},
{"host": "elastic.co", "http_auth": "user:secre]"},
],
_normalize_hosts(["elastic.co:42", "user:secre%[email protected]"]),
2013-05-04 20:00:39 +02:00
)
2014-11-13 11:47:54 +01:00
def test_strings_are_parsed_for_scheme(self):
self.assertEquals(
2014-11-14 15:55:32 +01:00
[
2019-03-29 09:25:23 -06:00
{"host": "elastic.co", "port": 42, "use_ssl": True},
{
2015-03-23 15:25:45 -07:00
"host": "elastic.co",
"http_auth": "user:secret",
"use_ssl": True,
"port": 443,
2019-03-29 09:25:23 -06:00
"url_prefix": "/prefix",
},
2014-11-14 15:55:32 +01:00
],
2019-03-29 09:25:23 -06:00
_normalize_hosts(
["https://elastic.co:42", "https://user:[email protected]/prefix"]
),
2014-11-13 11:47:54 +01:00
)
2013-05-04 20:00:39 +02:00
def test_dicts_are_left_unchanged(self):
2019-03-29 09:25:23 -06:00
self.assertEquals(
[{"host": "local", "extra": 123}],
_normalize_hosts([{"host": "local", "extra": 123}]),
)
def test_single_string_is_wrapped_in_list(self):
2019-03-29 09:25:23 -06:00
self.assertEquals([{"host": "elastic.co"}], _normalize_hosts("elastic.co"))
class TestClient(ElasticsearchTestCase):
def test_request_timeout_is_passed_through_unescaped(self):
2019-03-29 09:25:23 -06:00
self.client.ping(request_timeout=0.1)
calls = self.assert_url_called("HEAD", "/")
self.assertEquals([({"request_timeout": 0.1}, None)], calls)
def test_params_is_copied_when(self):
rt = object()
params = dict(request_timeout=rt)
self.client.ping(params=params)
self.client.ping(params=params)
2019-03-29 09:25:23 -06:00
calls = self.assert_url_called("HEAD", "/", 2)
self.assertEquals(
2019-03-29 09:25:23 -06:00
[({"request_timeout": rt}, None), ({"request_timeout": rt}, None)], calls
)
self.assertFalse(calls[0][0] is calls[1][0])
def test_from_in_search(self):
2019-03-29 09:25:23 -06:00
self.client.search(index="i", from_=10)
calls = self.assert_url_called("GET", "/i/_search")
self.assertEquals([({"from": "10"}, None)], calls)
def test_repr_contains_hosts(self):
2019-03-29 09:25:23 -06:00
self.assertEquals("<Elasticsearch([{}])>", repr(self.client))
2018-01-24 16:21:29 +02:00
def test_repr_subclass(self):
2019-03-29 09:25:23 -06:00
class OtherElasticsearch(Elasticsearch):
pass
self.assertEqual("<OtherElasticsearch([{}])>", repr(OtherElasticsearch()))
2018-01-24 16:21:29 +02:00
def test_repr_contains_hosts_passed_in(self):
2019-03-29 09:25:23 -06:00
self.assertIn("es.org", repr(Elasticsearch(["es.org:123"])))
2018-01-24 16:21:29 +02:00
def test_repr_truncates_host_to_5(self):
hosts = [{"host": "es" + str(i)} for i in range(10)]
es = Elasticsearch(hosts)
self.assertNotIn("es5", repr(es))
2019-03-29 09:25:23 -06:00
self.assertIn("...", repr(es))
2014-12-30 18:50:52 +01:00
def test_index_uses_post_if_id_is_empty(self):
2019-03-29 09:25:23 -06:00
self.client.index(index="my-index", id="", body={})
2014-12-30 18:50:52 +01:00
2019-03-29 09:25:23 -06:00
self.assert_url_called("POST", "/my-index/_doc")
2014-12-30 18:50:52 +01:00
def test_index_uses_put_if_id_is_not_empty(self):
2019-03-29 09:25:23 -06:00
self.client.index(index="my-index", id=0, body={})
2014-12-30 18:50:52 +01:00
self.assert_url_called("POST", "/my-index/_doc/0")