use default trait type in event list query

Currently, if we request event list api with trait queries, but unset
trait type or set it to '', then we will get 500 and database backend
will fail with KeyError. That is because we use query.type as trait
type, the query.type is checked only when it is set to bool(type) == True,
otherwise, we treat it as string value, but remain type itself unset.

This patch adds a simple check for it, and set it to default type string
when the type is unset or empty.

Change-Id: I503c35cdbc5c8d51dd9b19a314af64ef9fcecf29
Closes-Bug: #1407374
This commit is contained in:
ZhiQiang Fan
2015-01-14 18:51:22 +08:00
parent 98fa79a868
commit 568f4e0df8
2 changed files with 30 additions and 1 deletions

View File

@@ -170,8 +170,9 @@ def _event_query_to_event_filter(q):
if i.field in evt_model_filter:
evt_model_filter[i.field] = i.value
else:
trait_type = i.type or 'string'
traits_filter.append({"key": i.field,
i.type: i._get_value_as_type(),
trait_type: i._get_value_as_type(),
"op": i.op})
return storage.EventFilter(traits_filter=traits_filter, **evt_model_filter)

View File

@@ -177,6 +177,34 @@ class TestEventAPI(EventTestBase):
'value': 'Foo'}])
self.assertEqual(1, len(data))
def test_get_events_filter_trait_no_type(self):
data = self.get_json(self.PATH, headers=headers,
q=[{'field': 'trait_A',
'value': 'my_Foo_text'}])
self.assertEqual(1, len(data))
self.assertEqual('Foo', data[0]['event_type'])
def test_get_events_filter_trait_empty_type(self):
return
data = self.get_json(self.PATH, headers=headers,
q=[{'field': 'trait_A',
'value': 'my_Foo_text',
'type': ''}])
self.assertEqual(1, len(data))
self.assertEqual('Foo', data[0]['event_type'])
def test_get_events_filter_trait_invalid_type(self):
resp = self.get_json(self.PATH, headers=headers,
q=[{'field': 'trait_A',
'value': 'my_Foo_text',
'type': 'whats-up'}],
expect_errors=True)
self.assertEqual(400, resp.status_code)
self.assertEqual("The data type whats-up is not supported. The "
"supported data type list is: [\'integer\', "
"\'float\', \'string\', \'datetime\']",
resp.json['error_message']['faultstring'])
def test_get_events_filter_text_trait(self):
data = self.get_json(self.PATH, headers=headers,
q=[{'field': 'trait_A',