Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date value field validation #2517

Merged
merged 9 commits into from
Apr 19, 2017
11 changes: 7 additions & 4 deletions beets/dbcore/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,21 +547,24 @@ def __init__(self, date, precision):

@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)

Expand Down
24 changes: 21 additions & 3 deletions test/test_datequery.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
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):
Expand Down Expand Up @@ -117,10 +118,27 @@ def test_single_day_nonmatch_fast(self):

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():
Expand Down
9 changes: 6 additions & 3 deletions test/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -891,9 +891,12 @@ def test_type_boolean(self):
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):
Expand Down Expand Up @@ -992,7 +995,7 @@ def test_fast_vs_slow(self):
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']),
Expand Down