using a pattern may avoid copy-paste error when used 3 times after

fixed an error with the weeks that didn't use the sign correctly
added more tests, this is where py.test fixtures would shine
This commit is contained in:
euri10 2017-01-29 03:51:24 +01:00
parent 3e76c219fb
commit af679de8ec
2 changed files with 49 additions and 5 deletions

View file

@ -551,10 +551,11 @@ class Period(object):
"""Parse a date and return a `Period` object or `None` if the
string is empty.
"""
if re.match('@([+|-]?)(\d+)([y|m|w|d])', string) is not None:
sign = re.match('@([+|-]?)(\d+)([y|m|w|d])', string).group(1)
quantity = re.match('@([+|-]?)(\d+)([y|m|w|d])', string).group(2)
timespan = re.match('@([+|-]?)(\d+)([y|m|w|d])', string).group(3)
pattern_dq = '@([+|-]?)(\d+)([y|m|w|d])'
if re.match(pattern_dq, string) is not None:
sign = re.match(pattern_dq, string).group(1)
quantity = re.match(pattern_dq, string).group(2)
timespan = re.match(pattern_dq, string).group(3)
if sign == '-':
m = -1
else:
@ -564,7 +565,7 @@ class Period(object):
elif timespan == 'm':
date = datetime.now() + m * timedelta(days=int(quantity) * 30)
elif timespan == 'w':
date = datetime.now() + timedelta(days=int(quantity) * 7)
date = datetime.now() + m * timedelta(days=int(quantity) * 7)
elif timespan == 'd':
date = datetime.now() + m * timedelta(days=int(quantity))
precision = 'relative'

View file

@ -160,6 +160,49 @@ class DateQueryTestRelative(_common.LibTestCase):
self.assertEqual(len(matched), 0)
class DateQueryTestRelativeMore(_common.LibTestCase):
def setUp(self):
super(DateQueryTestRelativeMore, self).setUp()
self.i.added = _parsetime(datetime.now().strftime('%Y-%m-%d %H:%M'))
self.i.store()
def test_relative(self):
for timespan in ['d', 'w', 'm', 'y']:
query = DateQuery('added', '@-4'+timespan+'..@+4'+timespan)
matched = self.lib.items(query)
self.assertEqual(len(matched), 1)
def test_relative_fail(self):
for timespan in ['d', 'w', 'm', 'y']:
query = DateQuery('added', '@-2'+timespan+'..@-1'+timespan)
matched = self.lib.items(query)
self.assertEqual(len(matched), 0)
def test_start_relative(self):
for timespan in ['d', 'w', 'm', 'y']:
query = DateQuery('added', '@-4' + timespan + '..')
matched = self.lib.items(query)
self.assertEqual(len(matched), 1)
def test_start_relative_fail(self):
for timespan in ['d', 'w', 'm', 'y']:
query = DateQuery('added', '@4' + timespan + '..')
matched = self.lib.items(query)
self.assertEqual(len(matched), 0)
def test_end_relative(self):
for timespan in ['d', 'w', 'm', 'y']:
query = DateQuery('added', '..@+4' + timespan)
matched = self.lib.items(query)
self.assertEqual(len(matched), 1)
def test_end_relative_fail(self):
for timespan in ['d', 'w', 'm', 'y']:
query = DateQuery('added', '..@-4' + timespan)
matched = self.lib.items(query)
self.assertEqual(len(matched), 0)
class DateQueryConstructTest(unittest.TestCase):
def test_long_numbers(self):
DateQuery('added', '1409830085..1412422089')