Skip to content

Commit 7379b25

Browse files
committed
Fixes bug when chaining expressions with OR
1 parent b6223d1 commit 7379b25

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

pyodata/v2/service.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1268,7 +1268,14 @@ 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})"
1275+
1276+
return combined
1277+
1278+
return expressions[0]
12721279

12731280
# pylint: disable=too-many-return-statements, too-many-branches
12741281
def _build_expression(self, field_name, operator, value):

tests/test_service_v2.py

+23-3
Original file line numberDiff line numberDiff line change
@@ -2541,7 +2541,7 @@ def test_count_with_chainable_filter_multiple(service):
25412541

25422542
responses.add(
25432543
responses.GET,
2544-
f"{service.url}/Employees/$count?%24filter=ID%20eq%2023%20and%20NickName%20eq%20%27Steve%27",
2544+
f"{service.url}/Employees/$count?%24filter=%28ID%20eq%2023%29%20and%20%28NickName%20eq%20%27Steve%27%29",
25452545
json=3,
25462546
status=200)
25472547

@@ -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=%28startswith%28NameFirst%2C+%27Steve%27%29+eq+true%29+and+%28ID+eq+23+or+ID+eq+25+or+ID+eq+28%29"
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"""
@@ -2561,7 +2581,7 @@ def test_count_with_chainable_filter_or(service):
25612581

25622582
responses.add(
25632583
responses.GET,
2564-
f"{service.url}/Employees/$count?$filter=%28ID%20eq%2023%20and%20NickName%20eq%20%27Steve%27%29%20or%20%28ID%20eq%2025%20and%20NickName%20eq%20%27Tim%27%29",
2584+
f"{service.url}/Employees/$count?$filter=%28%28ID%20eq%2023%29%20and%20%28NickName%20eq%20%27Steve%27%29%29%20or%20%28%28ID%20eq%2025%29%20and%20%28NickName%20eq%20%27Tim%27%29%29",
25652585
json=3,
25662586
status=200)
25672587

@@ -2580,7 +2600,7 @@ def test_count_with_multiple_chainable_filters_startswith(service):
25802600

25812601
responses.add(
25822602
responses.GET,
2583-
f"{service.url}/Employees/$count?$filter=%28ID%20eq%2023%20and%20startswith%28NickName%2C%20%27Ste%27%29%20eq%20true%29%20or%20%28ID%20eq%2025%20and%20NickName%20eq%20%27Tim%27%29",
2603+
f"{service.url}/Employees/$count?$filter=%28%28ID%20eq%2023%29%20and%20%28startswith%28NickName%2C%20%27Ste%27%29%20eq%20true%29%29%20or%20%28%28ID%20eq%2025%29%20and%20%28NickName%20eq%20%27Tim%27%29%29",
25842604
json=3,
25852605
status=200)
25862606

0 commit comments

Comments
 (0)