|
14 | 14 | # KIND, either express or implied. See the License for the |
15 | 15 | # specific language governing permissions and limitations |
16 | 16 | # 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") |
0 commit comments