Skip to content

Commit 71b8043

Browse files
committed
Adds brackets around expressions when several are used
1 parent b6223d1 commit 71b8043

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

pyodata/v2/service.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1268,8 +1268,15 @@ def _decode_expression(self, expr, val):
12681268

12691269
# pylint: disable=no-self-use
12701270
def _combine_expressions(self, expressions):
1271-
return ' and '.join(expressions)
1271+
if len(expressions) > 1:
1272+
combined = ""
1273+
for counter, expr in enumerate(expressions):
1274+
combined += f"{' and ' if counter > 0 else ''}({expr})"
12721275

1276+
return combined
1277+
1278+
return expressions[0]
1279+
12731280
# pylint: disable=too-many-return-statements, too-many-branches
12741281
def _build_expression(self, field_name, operator, value):
12751282
target_field = self.proprty_obj(field_name)

tests/test_service_v2.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2553,6 +2553,26 @@ def test_count_with_chainable_filter_multiple(service):
25532553
assert request.execute() == 3
25542554

25552555

2556+
@responses.activate
2557+
def test_chaining_fix(service):
2558+
from pyodata.v2.service import FilterExpression as Q
2559+
2560+
url = f"{service.url}/Employees?%24filter=startswith%28NameFirst%2C+%27Steve%27%29+eq+true+and+ID+eq+23+or+ID+eq+25+or+ID+eq+28"
2561+
2562+
responses.add(
2563+
responses.GET,
2564+
url,
2565+
json=3,
2566+
status=200)
2567+
2568+
employees = service.entity_sets.Employees.get_entities()
2569+
request = employees.filter(Q(NameFirst__startswith="Steve"), Q(ID__in=[23, 25, 28]))
2570+
2571+
assert isinstance(request, pyodata.v2.service.GetEntitySetRequest)
2572+
2573+
assert request.execute() == 3
2574+
2575+
25562576
@responses.activate
25572577
def test_count_with_chainable_filter_or(service):
25582578
"""Check getting $count with $filter with FilterExpression syntax or"""

0 commit comments

Comments
 (0)