1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import warnings
import pytest
from elasticsearch import AsyncElasticsearch, Elasticsearch
from elasticsearch._sync.client.utils import _rewrite_parameters
class TestRewriteParameters:
@property
def calls(self):
if not hasattr(self, "_calls"):
self._calls = []
return self._calls
def options(self, *args, **kwargs):
self.calls.append((args, kwargs))
return self
@_rewrite_parameters()
def wrapped_func_default(self, *args, **kwargs):
self.calls.append((args, kwargs))
@_rewrite_parameters(body_name="document")
def wrapped_func_body_name(self, *args, **kwargs):
self.calls.append((args, kwargs))
@_rewrite_parameters(body_fields=("query", "source"))
def wrapped_func_body_fields(self, *args, **kwargs):
self.calls.append((args, kwargs))
@_rewrite_parameters(
body_fields=("query",), ignore_deprecated_options={"api_key", "body", "params"}
)
def wrapped_func_ignore(self, *args, **kwargs):
self.calls.append((args, kwargs))
@_rewrite_parameters(
body_fields=("source",), parameter_aliases={"_source": "source"}
)
def wrapped_func_aliases(self, *args, **kwargs):
self.calls.append((args, kwargs))
def test_default(self):
with warnings.catch_warnings(record=True) as w:
self.wrapped_func_default(
api_key=("id", "api_key"),
query={"match_all": {}},
params={"key": "value", "ignore": 404},
)
assert len(w) == 2
assert w[0].category == DeprecationWarning
assert (
str(w[0].message)
== "The 'params' parameter is deprecated and will be removed in a future version. Instead use individual parameters."
)
assert w[1].category == DeprecationWarning
assert (
str(w[1].message)
== "Passing transport options in the API method is deprecated. Use 'Elasticsearch.options()' instead."
)
assert self.calls == [
((), {"api_key": ("id", "api_key"), "ignore_status": 404}),
((), {"query": {"match_all": {}}, "key": "value"}),
]
def test_default_params_conflict(self):
with pytest.raises(ValueError) as e:
self.wrapped_func_default(
query={"match_all": {}},
params={"query": {"match_all": {}}},
)
assert str(e.value) == (
"Received multiple values for 'query', specify parameters directly instead of using 'params'"
)
def test_body_name_using_body(self):
with warnings.catch_warnings(record=True) as w:
self.wrapped_func_body_name(
api_key=("id", "api_key"), body={"query": {"match_all": {}}}
)
assert len(w) == 1
assert w[0].category == DeprecationWarning
assert (
str(w[0].message)
== "Passing transport options in the API method is deprecated. Use 'Elasticsearch.options()' instead."
)
assert self.calls == [
((), {"api_key": ("id", "api_key")}),
((), {"document": {"query": {"match_all": {}}}}),
]
def test_body_name(self):
with warnings.catch_warnings(record=True) as w:
self.wrapped_func_body_name(
api_key=("id", "api_key"), document={"query": {"match_all": {}}}
)
assert len(w) == 1
assert w[0].category == DeprecationWarning
assert (
str(w[0].message)
== "Passing transport options in the API method is deprecated. Use 'Elasticsearch.options()' instead."
)
assert self.calls == [
((), {"api_key": ("id", "api_key")}),
((), {"document": {"query": {"match_all": {}}}}),
]
def test_body_name_duplicate(self):
with pytest.raises(TypeError) as e:
self.wrapped_func_body_name(body={}, document={})
assert str(e.value) == (
"Can't use 'document' and 'body' parameters together because 'document' is an alias for 'body'. "
"Instead you should only use the 'document' parameter. See https://github.com/elastic/elasticsearch-py"
"/issues/1698 for more information"
)
def test_body_fields(self):
with warnings.catch_warnings(record=True) as w:
self.wrapped_func_body_fields(
api_key=("id", "api_key"), body={"query": {"match_all": {}}}
)
assert len(w) == 1
assert w[0].category == DeprecationWarning
assert (
str(w[0].message)
== "Passing transport options in the API method is deprecated. Use 'Elasticsearch.options()' instead."
)
assert self.calls == [
((), {"api_key": ("id", "api_key")}),
((), {"body": {"query": {"match_all": {}}}}),
]
@pytest.mark.parametrize(
"body, kwargs",
[
('{"query": {"match_all": {}}}', {"query": {"match_all": {}}}),
(b'{"query": {"match_all": {}}}', {"query": {"match_all": {}}}),
],
)
def test_error_on_body_merge(self, body, kwargs):
with pytest.raises(ValueError) as e:
self.wrapped_func_body_fields(body=body, **kwargs)
assert str(e.value) == (
"Couldn't merge 'body' with other parameters as it wasn't a mapping."
)
@pytest.mark.parametrize(
"params", ['{"query": {"match_all": {}}}', b'{"query": {"match_all": {}}}']
)
def test_error_on_params_merge(self, params):
with pytest.raises(ValueError) as e:
self.wrapped_func_body_fields(params=params)
assert str(e.value) == (
"Couldn't merge 'params' with other parameters as it wasn't a mapping. Instead of "
"using 'params' use individual API parameters"
)
def test_body_fields_merge(self):
with warnings.catch_warnings(record=True) as w:
self.wrapped_func_body_fields(source=False, body={"query": {}})
assert len(w) == 1
assert w[0].category == DeprecationWarning
assert str(w[0].message) == (
"Received 'source' via a specific parameter in the presence of a "
"'body' parameter, which is deprecated and will be removed in a future "
"version. Instead, use only 'body' or only specific parameters."
)
def test_body_fields_conflict(self):
with pytest.raises(ValueError) as e:
self.wrapped_func_body_fields(query={"match_all": {}}, body={"query": {}})
assert str(e.value) == (
"Received multiple values for 'query', specify parameters using either body or parameters, not both."
)
def test_ignore_deprecated_options(self):
with warnings.catch_warnings(record=True) as w:
self.wrapped_func_ignore(
api_key=("id", "api_key"),
body={"query": {"match_all": {}}},
params={"key": "value"},
param=1,
http_auth=("key", "value"),
)
assert len(w) == 1
assert w[0].category == DeprecationWarning
assert (
str(w[0].message)
== "Passing transport options in the API method is deprecated. Use 'Elasticsearch.options()' instead."
)
assert self.calls == [
((), {"http_auth": ("key", "value")}),
(
(),
{
"api_key": ("id", "api_key"),
"body": {"query": {"match_all": {}}},
"params": {"key": "value"},
"param": 1,
},
),
]
def test_parameter_aliases(self):
self.wrapped_func_aliases(_source=["key1", "key2"])
assert self.calls == [((), {"source": ["key1", "key2"]})]
self.wrapped_func_aliases(source=["key3"])
assert self.calls[-1] == ((), {"source": ["key3"]})
def test_parameter_aliases_body(self):
with pytest.warns(
DeprecationWarning,
match=(
"Using 'source' alias in 'body' is deprecated and will be removed in a future version of elasticsearch-py. "
"Use '_source' directly instead."
),
):
self.wrapped_func_aliases(body={"source": ["key4"]})
# using the correct name does not warn
with warnings.catch_warnings():
warnings.simplefilter("error")
self.wrapped_func_aliases(body={"_source": ["key4"]})
def test_parameter_aliases_body_param(self):
with pytest.warns(
DeprecationWarning,
match=(
"Received 'source' via a specific parameter in the presence of a "
"'body' parameter, which is deprecated and will be removed in a future "
"version. Instead, use only 'body' or only specific parameters."
),
):
self.wrapped_func_aliases(
source=["key4"], body={"query": {"match_all": {}}}
)
# using the correct name does not warn
with warnings.catch_warnings():
warnings.simplefilter("error")
self.wrapped_func_aliases(
body={"query": {"match_all": {}}, "_source": ["key4"]}
)
@pytest.mark.parametrize("client_cls", [Elasticsearch, AsyncElasticsearch])
def test_positional_argument_error(self, client_cls):
client = client_cls("https://localhost:9200")
with pytest.raises(TypeError) as e:
client.search("index")
assert str(e.value) == (
"Positional arguments can't be used with Elasticsearch API methods. "
"Instead only use keyword arguments."
)
with pytest.raises(TypeError) as e:
client.indices.exists("index")
assert str(e.value) == (
"Positional arguments can't be used with Elasticsearch API methods. "
"Instead only use keyword arguments."
)
|