Update tests for new validators.

This commit is contained in:
Christopher Charbonneau Wells 2017-11-03 23:29:55 -04:00
parent c5612b102d
commit fe3e753d65
2 changed files with 34 additions and 21 deletions

View File

@ -23,10 +23,7 @@ def validate_duration(model, max_duration=timedelta(hours=24)):
'Start time must come before end time.', 'Start time must come before end time.',
code='end_before_start') code='end_before_start')
if model.end - model.start > max_duration: if model.end - model.start > max_duration:
raise ValidationError( raise ValidationError('Duration too long.', code='max_duration')
'Duration too long (%(timesince)s).',
params={'timesince': timesince(model.start, model.end)},
code='max_duration')
def validate_time(time, field_name): def validate_time(time, field_name):
@ -38,7 +35,7 @@ def validate_time(time, field_name):
""" """
if time > timezone.localtime(): if time > timezone.localtime():
raise ValidationError( raise ValidationError(
{field_name: 'Times can not be in the future.'}, {field_name: 'Date/time can not be in the future.'},
code='time_invalid') code='time_invalid')

View File

@ -100,13 +100,6 @@ class FormsTestCase(TestCase):
page = self.c.post('/feedings/{}/'.format(entry.id), params) page = self.c.post('/feedings/{}/'.format(entry.id), params)
self.assertEqual(page.status_code, 302) self.assertEqual(page.status_code, 302)
params['start'] = '2001-01-01 1:01'
page = self.c.post('/feedings/{}/'.format(entry.id), params)
self.assertEqual(page.status_code, 200)
self.assertFormError(page, 'form', None,
'Start time must come before end time.')
params['start'] = '2000-01-01 1:01'
params['method'] = 'left breast' params['method'] = 'left breast'
page = self.c.post('/feedings/{}/'.format(entry.id), params) page = self.c.post('/feedings/{}/'.format(entry.id), params)
self.assertEqual(page.status_code, 200) self.assertEqual(page.status_code, 200)
@ -130,19 +123,14 @@ class FormsTestCase(TestCase):
page = self.c.post('/sleep/{}/'.format(entry.id), params) page = self.c.post('/sleep/{}/'.format(entry.id), params)
self.assertEqual(page.status_code, 302) self.assertEqual(page.status_code, 302)
params['start'] = '2001-01-01 1:01'
page = self.c.post('/sleep/{}/'.format(entry.id), params)
self.assertEqual(page.status_code, 200)
self.assertFormError(page, 'form', None,
'Start time must come before end time.')
def test_timer_forms(self): def test_timer_forms(self):
timer = models.Timer.objects.create(user=self.user) start_time = timezone.localtime()
timer = models.Timer.objects.create(user=self.user, start=start_time)
timer.save() timer.save()
params = { params = {
'name': 'New', 'name': 'New',
'start': timer.start.strftime('%Y-%m-%d %H:%M:%S') 'start': start_time.strftime('%Y-%m-%d %H:%M:%S')
} }
page = self.c.post('/timer/{}/edit/'.format(timer.id), params) page = self.c.post('/timer/{}/edit/'.format(timer.id), params)
self.assertEqual(page.status_code, 302) self.assertEqual(page.status_code, 302)
@ -158,6 +146,13 @@ class FormsTestCase(TestCase):
timer.refresh_from_db() timer.refresh_from_db()
self.assertEqual(timer.start, start_time) self.assertEqual(timer.start, start_time)
# Test changing a stopped timer
timer.end = timer.start + timezone.timedelta(hours=1)
timer.save()
params['name'] = 'New timer name'
page = self.c.post('/timer/{}/edit/'.format(timer.id), params)
self.assertEqual(page.status_code, 302)
def test_tummytime_forms(self): def test_tummytime_forms(self):
params = { params = {
'child': 1, 'child': 1,
@ -176,8 +171,29 @@ class FormsTestCase(TestCase):
page = self.c.post('/tummy-time/{}/'.format(entry.id), params) page = self.c.post('/tummy-time/{}/'.format(entry.id), params)
self.assertEqual(page.status_code, 302) self.assertEqual(page.status_code, 302)
params['start'] = '2001-01-01 1:01' def test_validators(self):
params = {
'child': 1,
'start': '2001-01-01 1:01',
'end': '2000-01-01 1:11',
'milestone': ''
}
entry = models.TummyTime.objects.first()
page = self.c.post('/tummy-time/{}/'.format(entry.id), params) page = self.c.post('/tummy-time/{}/'.format(entry.id), params)
self.assertEqual(page.status_code, 200) self.assertEqual(page.status_code, 200)
self.assertFormError(page, 'form', None, self.assertFormError(page, 'form', None,
'Start time must come before end time.') 'Start time must come before end time.')
params['start'] = '1999-01-01 1:11'
page = self.c.post('/tummy-time/{}/'.format(entry.id), params)
self.assertEqual(page.status_code, 200)
self.assertFormError(page, 'form', None, 'Duration too long.')
tomorrow = (timezone.localtime() + timezone.timedelta(days=1))
params['end'] = tomorrow.strftime('%Y-%m-%d %H:%M:%S')
page = self.c.post('/tummy-time/{}/'.format(entry.id), params)
self.assertEqual(page.status_code, 200)
self.assertFormError(page, 'form', 'end',
'Date/time can not be in the future.')