Make type guessing for query args more robust

Improve the error handling of the type-guesser for queries
against metadata fields, and add some test coverage for
specific cases that came out of the discussion of the
source of the bug.

Fixes bug #1221736

Change-Id: I27f84aa2538d2a69a3d7b7e93c36813df8d35204
This commit is contained in:
Doug Hellmann 2013-09-06 10:33:41 -04:00
parent a9f147c62a
commit 37a779065f
2 changed files with 35 additions and 1 deletions

View File

@ -175,7 +175,7 @@ class Query(_Base):
if not self.type:
try:
converted_value = ast.literal_eval(self.value)
except ValueError:
except (ValueError, SyntaxError):
msg = _('Failed to convert the metadata value %s'
' automatically') % (self.value)
LOG.debug(msg)

View File

@ -101,6 +101,40 @@ class TestQuery(tests_base.TestCase):
type='integer')
self.assertRaises(wsme.exc.ClientSideError, query._get_value_as_type)
def test_get_value_as_type_integer_expression_without_type(self):
# bug 1221736
query = Query(field='should_be_a_string',
op='eq',
value='123-1')
expected = '123-1'
self.assertEqual(query._get_value_as_type(), expected)
def test_get_value_as_type_boolean_expression_without_type(self):
# bug 1221736
query = Query(field='should_be_a_string',
op='eq',
value='True or False')
expected = 'True or False'
self.assertEqual(query._get_value_as_type(), expected)
def test_get_value_as_type_with_syntax_error(self):
# bug 1221736
value = 'WWW-Layer-4a80714f-0232-4580-aa5e-81494d1a4147-uolhh25p5xxm'
query = Query(field='group_id',
op='eq',
value=value)
expected = value
self.assertEqual(query._get_value_as_type(), expected)
def test_get_value_as_type_with_syntax_error_colons(self):
# bug 1221736
value = 'Ref::StackId'
query = Query(field='field_name',
op='eq',
value=value)
expected = value
self.assertEqual(query._get_value_as_type(), expected)
def _fake_db_func(self, resource, on_behalf_of, x, y,
metaquery={}, user=None, project=None,
start_timestamp=None, start_timestamp_op=None,