Skip to content

Commit 16a47fe

Browse files
committed
Improve test_rewrite_parameters tests
1 parent 564d37c commit 16a47fe

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

elasticsearch/_sync/client/utils.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ def _merge_kwargs_no_duplicates(kwargs: Dict[str, Any], values: Dict[str, Any])
289289
if key in kwargs:
290290
raise ValueError(
291291
f"Received multiple values for '{key}', specify parameters "
292-
"directly instead of using 'body' or 'params'"
292+
"directly instead of using 'params'"
293293
)
294294
kwargs[key] = val
295295

@@ -301,10 +301,16 @@ def _merge_body_fields_no_duplicates(
301301
if key in body:
302302
raise ValueError(
303303
f"Received multiple values for '{key}', specify parameters "
304-
"directly instead of using 'body' or 'params'"
304+
"using either body or parameters, not both."
305305
)
306306
if key in body_fields:
307-
# deprecate this
307+
warnings.warn(
308+
f"Received '{key}' via a specific parameter in the presence of a "
309+
"'body' parameter, which is deprecated and will be removed in a future "
310+
"version. Instead, use only 'body' or only specific paremeters.",
311+
category=DeprecationWarning,
312+
stacklevel=warn_stacklevel(),
313+
)
308314
body[key] = kwargs.pop(key)
309315

310316

@@ -398,7 +404,7 @@ def wrapped(*args: Any, **kwargs: Any) -> Any:
398404
)
399405
kwargs[body_name] = body
400406

401-
elif body_fields:
407+
elif body_fields is not None:
402408
if not hasattr(body, "items"):
403409
raise ValueError(
404410
"Couldn't merge 'body' with other parameters as it wasn't a mapping. "

test_elasticsearch/test_client/test_rewrite_parameters.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,19 @@ def wrapped_func_default(self, *args, **kwargs):
4242
def wrapped_func_body_name(self, *args, **kwargs):
4343
self.calls.append((args, kwargs))
4444

45-
@_rewrite_parameters(body_fields=True)
45+
@_rewrite_parameters(body_fields=("query", "source"))
4646
def wrapped_func_body_fields(self, *args, **kwargs):
4747
self.calls.append((args, kwargs))
4848

4949
@_rewrite_parameters(
50-
body_fields=True, ignore_deprecated_options={"api_key", "body", "params"}
50+
body_fields=("query",), ignore_deprecated_options={"api_key", "body", "params"}
5151
)
5252
def wrapped_func_ignore(self, *args, **kwargs):
5353
self.calls.append((args, kwargs))
5454

55-
@_rewrite_parameters(body_fields=True, parameter_aliases={"_source": "source"})
55+
@_rewrite_parameters(
56+
body_fields=("source",), parameter_aliases={"_source": "source"}
57+
)
5658
def wrapped_func_aliases(self, *args, **kwargs):
5759
self.calls.append((args, kwargs))
5860

@@ -81,6 +83,16 @@ def test_default(self):
8183
((), {"query": {"match_all": {}}, "key": "value"}),
8284
]
8385

86+
def test_default_params_conflict(self):
87+
with pytest.raises(ValueError) as e:
88+
self.wrapped_func_default(
89+
query={"match_all": {}},
90+
params={"query": {"match_all": {}}},
91+
)
92+
assert str(e.value) == (
93+
"Received multiple values for 'query', specify parameters directly instead of using 'params'"
94+
)
95+
8496
def test_body_name_using_body(self):
8597
with warnings.catch_warnings(record=True) as w:
8698
self.wrapped_func_body_name(
@@ -167,6 +179,25 @@ def test_error_on_params_merge(self, params):
167179
"using 'params' use individual API parameters"
168180
)
169181

182+
def test_body_fields_merge(self):
183+
with warnings.catch_warnings(record=True) as w:
184+
self.wrapped_func_body_fields(source=False, body={"query": {}})
185+
186+
assert len(w) == 1
187+
assert w[0].category == DeprecationWarning
188+
assert str(w[0].message) == (
189+
f"Received 'source' via a specific parameter in the presence of a "
190+
"'body' parameter, which is deprecated and will be removed in a future "
191+
"version. Instead, use only 'body' or only specific paremeters."
192+
)
193+
194+
def test_body_fields_conflict(self):
195+
with pytest.raises(ValueError) as e:
196+
self.wrapped_func_body_fields(query={"match_all": {}}, body={"query": {}})
197+
assert str(e.value) == (
198+
"Received multiple values for 'query', specify parameters using either body or parameters, not both."
199+
)
200+
170201
def test_ignore_deprecated_options(self):
171202
with warnings.catch_warnings(record=True) as w:
172203
self.wrapped_func_ignore(

0 commit comments

Comments
 (0)