Merge pull request #2517 from discopatrick/date-value-field-validation

Date value field validation
This commit is contained in:
Adrian Sampson 2017-04-19 11:36:18 -04:00
commit 31c7330de4
3 changed files with 34 additions and 10 deletions

View file

@ -547,21 +547,24 @@ class Period(object):
@classmethod
def parse(cls, string):
"""Parse a date and return a `Period` object or `None` if the
string is empty.
"""Parse a date and return a `Period` object, or `None` if the
string is empty, or raise an InvalidQueryArgumentTypeError if
the string could not be parsed to a date.
"""
if not string:
return None
ordinal = string.count('-')
if ordinal >= len(cls.date_formats):
# Too many components.
return None
raise InvalidQueryArgumentTypeError(string,
'a valid datetime string')
date_format = cls.date_formats[ordinal]
try:
date = datetime.strptime(string, date_format)
except ValueError:
# Parsing failed.
return None
raise InvalidQueryArgumentTypeError(string,
'a valid datetime string')
precision = cls.precisions[ordinal]
return cls(date, precision)

View file

@ -21,7 +21,8 @@ from test import _common
from datetime import datetime
import unittest
import time
from beets.dbcore.query import _parse_periods, DateInterval, DateQuery
from beets.dbcore.query import _parse_periods, DateInterval, DateQuery,\
InvalidQueryArgumentTypeError
def _date(string):
@ -117,10 +118,27 @@ class DateQueryTest(_common.LibTestCase):
class DateQueryConstructTest(unittest.TestCase):
def test_long_numbers(self):
DateQuery('added', '1409830085..1412422089')
with self.assertRaises(InvalidQueryArgumentTypeError):
DateQuery('added', '1409830085..1412422089')
def test_too_many_components(self):
DateQuery('added', '12-34-56-78')
with self.assertRaises(InvalidQueryArgumentTypeError):
DateQuery('added', '12-34-56-78')
def test_invalid_date_query(self):
q_list = [
'2001-01-0a',
'2001-0a',
'200a',
'2001-01-01..2001-01-0a',
'2001-0a..2001-01',
'200a..2002',
'20aa..',
'..2aa'
]
for q in q_list:
with self.assertRaises(InvalidQueryArgumentTypeError):
DateQuery('added', q)
def suite():

View file

@ -891,9 +891,12 @@ class NotQueryTest(DummyDataTestCase):
self.assertNegationProperties(q)
def test_type_date(self):
q = dbcore.query.DateQuery(u'mtime', u'0.0')
q = dbcore.query.DateQuery(u'added', u'2000-01-01')
not_results = self.lib.items(dbcore.query.NotQuery(q))
self.assert_items_matched(not_results, [])
# query date is in the past, thus the 'not' results should contain all
# items
self.assert_items_matched(not_results, [u'foo bar', u'baz qux',
u'beets 4 eva'])
self.assertNegationProperties(q)
def test_type_false(self):
@ -992,7 +995,7 @@ class NotQueryTest(DummyDataTestCase):
AttributeError: type object 'NoneQuery' has no attribute 'field'
at NoneQuery.match() (due to being @classmethod, and no self?)
"""
classes = [(dbcore.query.DateQuery, [u'mtime', u'0.0']),
classes = [(dbcore.query.DateQuery, [u'added', u'2001-01-01']),
(dbcore.query.MatchQuery, [u'artist', u'one']),
# (dbcore.query.NoneQuery, ['rg_track_gain']),
(dbcore.query.NumericQuery, [u'year', u'2002']),