Skip to content

Commit 9ec2d74

Browse files
committed
[IMP] Add domain syntax for an OR query with a list
* Add logic to allow for an or query by using a list
1 parent 49cef5d commit 9ec2d74

3 files changed

Lines changed: 43 additions & 1 deletion

File tree

README.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ Perform a search for a patient with the last name Smith
6464
for row in res:
6565
print row.fname
6666

67+
Perform a search for a patient with the last name Smith or Jones
68+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
69+
70+
::
71+
72+
res = cp.search(
73+
cp['Patient'],
74+
{'lname': ['Smith', 'Jones']},
75+
)
76+
for row in res:
77+
print row.fname
78+
6779
Perform a patient search, but only return the ``mname`` column
6880
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6981

carepoint/db/carepoint.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from contextlib import contextmanager
1111

12-
from sqlalchemy import bindparam
12+
from sqlalchemy import bindparam, or_
1313
from sqlalchemy.ext.declarative import declarative_base
1414
from sqlalchemy.inspection import inspect
1515
from sqlalchemy.orm import sessionmaker
@@ -209,6 +209,16 @@ def _unwrap_filters(self, model_obj, filters=None):
209209
model_obj, col_name, _operator, _filter
210210
))
211211

212+
elif isinstance(col_filter, (list, tuple)):
213+
query = []
214+
for _filter in col_filter:
215+
query.append(
216+
self._create_criterion(
217+
model_obj, col_name, '==', _filter,
218+
),
219+
)
220+
new_filters.append(or_(*query))
221+
212222
else:
213223
new_filters.append(self._create_criterion(
214224
model_obj, col_name, '==', col_filter

carepoint/tests/db/test_carepoint.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,26 @@ def test_unwrap_filters_dict(self):
550550
self.carepoint._unwrap_filters(model_obj, filters)
551551
mk.assert_has_calls(expect, any_order=True)
552552

553+
@mock.patch('carepoint.db.carepoint.or_')
554+
def test_unwrap_filters_list(self, or_):
555+
""" It should create a proper or query for lists. """
556+
model_obj = self.__get_model_obj()
557+
col_expect = 'test_col'
558+
query_expect = ['test_1', 'test_2']
559+
filters = {
560+
col_expect: query_expect
561+
}
562+
expect = [
563+
mock.call(model_obj, col_expect, '==', query_expect[0]),
564+
mock.call(model_obj, col_expect, '==', query_expect[1]),
565+
]
566+
criterion_return = 'Criterion'
567+
with mock.patch.object(self.carepoint, '_create_criterion') as mk:
568+
mk.return_value = criterion_return
569+
self.carepoint._unwrap_filters(model_obj, filters)
570+
mk.assert_has_calls(expect, any_order=True)
571+
or_.assert_called_once_with(criterion_return, criterion_return)
572+
553573
def test_unwrap_filters_str(self):
554574
model_obj = self.__get_model_obj()
555575
col_expect = 'test_col'

0 commit comments

Comments
 (0)