Skip to content

Commit

Permalink
fixup! Allow the use of percent string in Bool.__and__ method
Browse files Browse the repository at this point in the history
  • Loading branch information
Godefroy-Amaury committed Jul 25, 2024
1 parent a17e4a4 commit 79d4d8b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 37 deletions.
6 changes: 0 additions & 6 deletions opensearchpy/helpers/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
# under the License.

import collections.abc as collections_abc
import contextlib
from itertools import chain
from typing import Any, Optional

Expand Down Expand Up @@ -221,11 +220,6 @@ def __and__(self, other: "Bool") -> Any:

for qx in (self, other):
min_should_match = qx._min_should_match

# attempt to convert a string integer representation to int
with contextlib.suppress(ValueError):
min_should_match = int(min_should_match)

# all subqueries are required
if (
isinstance(min_should_match, int)
Expand Down
38 changes: 7 additions & 31 deletions test_opensearchpy/test_helpers/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from typing import Any, Type, Union
from typing import Any

import pytest
from pytest import raises
Expand Down Expand Up @@ -567,18 +567,11 @@ def test_script_score() -> None:


@pytest.mark.parametrize( # type: ignore[misc]
"minimum_should_match, expected_type",
[
("1", int),
("-1", int),
("50%", str),
("-50%", str),
("1<50%", str),
],
"minimum_should_match",
[1, -1, "1", "-1", "50%", "-50%", "1<50%"],
)
def test_bool_with_minimum_should_match_as_string(
def test_bool_with_minimum_should_match_as_int_or_string(
minimum_should_match: str,
expected_type: Type[Union[str, int]],
) -> None:

q1 = query.Bool(
Expand All @@ -601,7 +594,7 @@ def test_bool_with_minimum_should_match_as_string(

d1 = {
"bool": {
"minimum_should_match": minimum_should_match,
"minimum_should_match": minimum_should_match, # input type is preserved
"should": [
{"term": {"field": "aa1"}},
{"term": {"field": "aa2"}},
Expand All @@ -611,7 +604,7 @@ def test_bool_with_minimum_should_match_as_string(

d2 = {
"bool": {
"minimum_should_match": minimum_should_match,
"minimum_should_match": minimum_should_match, # input type is preserved
"should": [
{"term": {"field": "bb1"}},
{"term": {"field": "bb2"}},
Expand All @@ -620,24 +613,7 @@ def test_bool_with_minimum_should_match_as_string(
}

d3 = {
"bool": {
"should": [
{"term": {"field": "aa1"}},
{"term": {"field": "aa2"}},
],
"must": [
{
"bool": {
"should": [
{"term": {"field": "bb1"}},
{"term": {"field": "bb2"}},
],
"minimum_should_match": expected_type(minimum_should_match),
}
}
],
"minimum_should_match": expected_type(minimum_should_match),
}
"bool": {**d1["bool"], "must": [d2]},
}

assert q1.to_dict() == d1
Expand Down

0 comments on commit 79d4d8b

Please sign in to comment.