Merge "SQL: Fix event-list with multiple trait query filters"

This commit is contained in:
Jenkins 2015-09-30 12:54:26 +00:00 committed by Gerrit Code Review
commit b7d91ebdaa
2 changed files with 16 additions and 6 deletions

View File

@ -70,7 +70,7 @@ def _build_trait_query(session, trait_type, key, value, op='eq'):
'ne': (trait_model.value != value)}
conditions = [trait_model.key == key, op_dict[op]]
return (session.query(trait_model.event_id.label('ev_id'))
.filter(*conditions), trait_model)
.filter(*conditions))
class Connection(base.Connection):
@ -247,16 +247,16 @@ class Connection(base.Connection):
key = trait_filter.pop('key')
op = trait_filter.pop('op', 'eq')
trait_type, value = list(trait_filter.items())[0]
trait_subq, t_model = _build_trait_query(session, trait_type,
key, value, op)
trait_subq = _build_trait_query(session, trait_type,
key, value, op)
for trait_filter in filters:
key = trait_filter.pop('key')
op = trait_filter.pop('op', 'eq')
trait_type, value = list(trait_filter.items())[0]
q, model = _build_trait_query(session, trait_type,
key, value, op)
q = _build_trait_query(session, trait_type,
key, value, op)
trait_subq = trait_subq.filter(
q.filter(model.event_id == t_model.event_id).exists())
trait_subq.subquery().c.ev_id == q.subquery().c.ev_id)
trait_subq = trait_subq.subquery()
query = (session.query(models.Event.id)

View File

@ -290,6 +290,16 @@ class TestEventAPI(EventTestBase):
self.assertEqual(0, len(data))
def test_get_events_multiple_filters_same_field_different_values(self):
data = self.get_json(self.PATH, headers=HEADERS,
q=[{'field': 'trait_A',
'value': 'my_Foo_text',
'type': 'string'},
{'field': 'trait_A',
'value': 'my_Bar_text',
'type': 'string'}])
self.assertEqual(0, len(data))
def test_get_events_not_filters(self):
data = self.get_json(self.PATH, headers=HEADERS,
q=[])