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 5f75ff9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 35 deletions.
5 changes: 0 additions & 5 deletions opensearchpy/helpers/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,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
36 changes: 6 additions & 30 deletions test_opensearchpy/test_helpers/test_query.py
Original file line number Diff line number Diff line change
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 5f75ff9

Please sign in to comment.