Fix open numeric ranges

Also improve InvalidQueryError message and update
NumericQuery._convert() docstring.

Fix #1288.
This commit is contained in:
Bruno Cauet 2015-01-31 14:16:15 +01:00
parent 927a53d59b
commit 557330e994
2 changed files with 10 additions and 3 deletions

View file

@ -25,7 +25,7 @@ from datetime import datetime, timedelta
class InvalidQueryError(ValueError):
def __init__(self, what, expected, detail=None):
message = "{0!r} is not {1}".format(what, expected)
message = "'{0}' is not {1}".format(what, expected)
if detail:
message = "{0}: {1}".format(message, detail)
super(InvalidQueryError, self).__init__(message)
@ -214,10 +214,14 @@ class NumericQuery(FieldQuery):
a float.
"""
def _convert(self, s):
"""Convert a string to a numeric type (float or int). If the
string cannot be converted, return None.
"""Convert a string to a numeric type (float or int).
Return None if `s` is empty.
Raise an InvalidQueryError if the string cannot be converted.
"""
# This is really just a bit of fun premature optimization.
if not s:
return None
try:
return int(s)
except ValueError:

View file

@ -334,6 +334,9 @@ class MatchTest(_common.TestCase):
q = dbcore.query.NumericQuery('bitrate', '200000..300000')
self.assertFalse(q.match(self.item))
def test_open_range(self):
dbcore.query.NumericQuery('bitrate', '100000..')
class PathQueryTest(_common.LibTestCase, TestHelper, AssertsMixin):
def setUp(self):