Add tests for initial values settings on core models

This commit is contained in:
Christopher C. Wells 2020-01-30 10:39:29 -08:00 committed by Christopher Charbonneau Wells
parent bd79a3f2d1
commit 2b0e15d8ec
1 changed files with 157 additions and 74 deletions

View File

@ -12,7 +12,9 @@ from core import models
class FormsTestCaseBase(TestCase): class FormsTestCaseBase(TestCase):
c = None
child = None child = None
user = None
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
@ -23,17 +25,17 @@ class FormsTestCaseBase(TestCase):
cls.c = HttpClient() cls.c = HttpClient()
fake_user = fake.simple_profile() fake_user = fake.simple_profile()
cls.credentials = { credentials = {
'username': fake_user['username'], 'username': fake_user['username'],
'password': fake.password() 'password': fake.password()
} }
cls.user = User.objects.create_user( cls.user = User.objects.create_user(
is_superuser=True, **cls.credentials) is_superuser=True, **credentials)
cls.c.login(**cls.credentials) cls.c.login(**credentials)
cls.child = models.Child.objects.create( cls.child = models.Child.objects.create(
first_name='Test', first_name='Child',
last_name='Child', last_name='One',
birth_date=timezone.localdate() birth_date=timezone.localdate()
) )
@ -50,79 +52,85 @@ class FormsTestCaseBase(TestCase):
return timezone.localtime(datetime).strftime(datetime_format) return timezone.localtime(datetime).strftime(datetime_format)
class FormValidationTestCase(FormsTestCaseBase): class InitialValuesTestCase(FormsTestCaseBase):
def test_validate_date(self): @classmethod
future = timezone.localtime() + timezone.timedelta(days=1) def setUpClass(cls):
params = { super(InitialValuesTestCase, cls).setUpClass()
'child': self.child, cls.child_two = models.Child.objects.create(
'weight': '8.5', first_name='Child',
'date': self.localdate_string(future) last_name='Two',
} birth_date=timezone.localdate()
entry = models.Weight.objects.create(**params) )
cls.timer = models.Timer.objects.create(
page = self.c.post('/weight/{}/'.format(entry.id), params, follow=True) user=cls.user,
self.assertEqual(page.status_code, 200) start=timezone.localtime() - timezone.timedelta(minutes=30)
self.assertFormError(page, 'form', 'date',
'Date can not be in the future.')
def test_validate_duration(self):
end = timezone.localtime() - timezone.timedelta(minutes=10)
start = end + timezone.timedelta(minutes=5)
params = {
'child': self.child,
'start': self.localtime_string(start),
'end': self.localtime_string(end),
'milestone': ''
}
page = self.c.post('/tummy-time/add/', params, follow=True)
self.assertEqual(page.status_code, 200)
self.assertFormError(page, 'form', None,
'Start time must come before end time.')
start = end - timezone.timedelta(weeks=53)
params['start'] = self.localtime_string(start)
page = self.c.post('/tummy-time/add/', params, follow=True)
self.assertEqual(page.status_code, 200)
self.assertFormError(page, 'form', None, 'Duration too long.')
def test_validate_time(self):
future = timezone.localtime() + timezone.timedelta(hours=1)
params = {
'child': self.child,
'start': self.localtime_string(),
'end': self.localtime_string(future),
'milestone': ''
}
page = self.c.post('/tummy-time/add/', params, follow=True)
self.assertEqual(page.status_code, 200)
self.assertFormError(page, 'form', 'end',
'Date/time can not be in the future.')
def test_validate_unique_period(self):
entry = models.TummyTime.objects.create(
child=self.child,
start=timezone.localtime() - timezone.timedelta(minutes=10),
end=timezone.localtime() - timezone.timedelta(minutes=5),
) )
start = entry.start - timezone.timedelta(minutes=2) def test_child_with_one_child(self):
end = entry.end + timezone.timedelta(minutes=2) page = self.c.get('/sleep/add/')
params = { self.assertEqual(page.context['form'].initial['child'], self.child)
'child': entry.child.id,
'start': self.localtime_string(start),
'end': self.localtime_string(end),
'milestone': ''
}
page = self.c.post('/tummy-time/add/', params, follow=True) def test_child_with_parameter(self):
page = self.c.get('/sleep/add/')
self.assertTrue('child' not in page.context['form'].initial)
page = self.c.get('/sleep/add/?child={}'.format(self.child.slug))
self.assertEqual(page.context['form'].initial['child'], self.child)
page = self.c.get('/sleep/add/?child={}'.format(self.child_two.slug))
self.assertEqual(page.context['form'].initial['child'], self.child_two)
def test_feeding_type(self):
f_one = models.Feeding.objects.create(
child=self.child,
start=timezone.localtime() - timezone.timedelta(hours=4),
end=timezone.localtime() - timezone.timedelta(hours=3, minutes=30),
type='breast milk',
method='left breast'
)
f_two = models.Feeding.objects.create(
child=self.child_two,
start=timezone.localtime() - timezone.timedelta(hours=4),
end=timezone.localtime() - timezone.timedelta(hours=3, minutes=30),
type='formula',
method='bottle'
)
page = self.c.get('/feedings/add/')
self.assertTrue('type' not in page.context['form'].initial)
page = self.c.get('/feedings/add/?child={}'.format(self.child.slug))
self.assertEqual(page.context['form'].initial['type'], f_one.type)
page = self.c.get('/feedings/add/?child={}'.format(
self.child_two.slug))
self.assertEqual(page.context['form'].initial['type'], f_two.type)
def test_timer_set(self):
self.timer.stop()
page = self.c.get('/sleep/add/')
self.assertTrue('start' not in page.context['form'].initial)
self.assertTrue('end' not in page.context['form'].initial)
page = self.c.get('/sleep/add/?timer={}'.format(self.timer.id))
self.assertEqual(page.context['form'].initial['start'],
self.timer.start)
self.assertEqual(page.context['form'].initial['end'], self.timer.end)
def test_timer_stop_on_save(self):
end = timezone.localtime()
params = {
'child': self.child.id,
'start': self.localtime_string(self.timer.start),
'end': self.localtime_string(end)
}
page = self.c.post('/sleep/add/?timer={}'.format(self.timer.id),
params, follow=True)
self.assertEqual(page.status_code, 200) self.assertEqual(page.status_code, 200)
self.assertFormError( self.timer.refresh_from_db()
page, self.assertFalse(self.timer.active)
'form', self.assertEqual(self.localtime_string(self.timer.end), params['end'])
None,
'Another entry intersects the specified time period.')
class ChildFormsTestCaseBase(FormsTestCaseBase): class ChildFormsTestCaseBase(FormsTestCaseBase):
@ -477,6 +485,81 @@ class TimerFormsTestCaseBase(FormsTestCaseBase):
self.assertEqual(self.localtime_string(timer.start), params['start']) self.assertEqual(self.localtime_string(timer.start), params['start'])
class ValidationsTestCase(FormsTestCaseBase):
def test_validate_date(self):
future = timezone.localtime() + timezone.timedelta(days=1)
params = {
'child': self.child,
'weight': '8.5',
'date': self.localdate_string(future)
}
entry = models.Weight.objects.create(**params)
page = self.c.post('/weight/{}/'.format(entry.id), params, follow=True)
self.assertEqual(page.status_code, 200)
self.assertFormError(page, 'form', 'date',
'Date can not be in the future.')
def test_validate_duration(self):
end = timezone.localtime() - timezone.timedelta(minutes=10)
start = end + timezone.timedelta(minutes=5)
params = {
'child': self.child,
'start': self.localtime_string(start),
'end': self.localtime_string(end),
'milestone': ''
}
page = self.c.post('/tummy-time/add/', params, follow=True)
self.assertEqual(page.status_code, 200)
self.assertFormError(page, 'form', None,
'Start time must come before end time.')
start = end - timezone.timedelta(weeks=53)
params['start'] = self.localtime_string(start)
page = self.c.post('/tummy-time/add/', params, follow=True)
self.assertEqual(page.status_code, 200)
self.assertFormError(page, 'form', None, 'Duration too long.')
def test_validate_time(self):
future = timezone.localtime() + timezone.timedelta(hours=1)
params = {
'child': self.child,
'start': self.localtime_string(),
'end': self.localtime_string(future),
'milestone': ''
}
page = self.c.post('/tummy-time/add/', params, follow=True)
self.assertEqual(page.status_code, 200)
self.assertFormError(page, 'form', 'end',
'Date/time can not be in the future.')
def test_validate_unique_period(self):
entry = models.TummyTime.objects.create(
child=self.child,
start=timezone.localtime() - timezone.timedelta(minutes=10),
end=timezone.localtime() - timezone.timedelta(minutes=5),
)
start = entry.start - timezone.timedelta(minutes=2)
end = entry.end + timezone.timedelta(minutes=2)
params = {
'child': entry.child.id,
'start': self.localtime_string(start),
'end': self.localtime_string(end),
'milestone': ''
}
page = self.c.post('/tummy-time/add/', params, follow=True)
self.assertEqual(page.status_code, 200)
self.assertFormError(
page,
'form',
None,
'Another entry intersects the specified time period.')
class WeightFormsTest(FormsTestCaseBase): class WeightFormsTest(FormsTestCaseBase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):