2017-10-22 03:00:28 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.core.management import call_command
|
2017-10-28 17:27:33 +00:00
|
|
|
from django.test import TestCase
|
|
|
|
from django.test import Client as HttpClient
|
|
|
|
from django.utils import timezone
|
2020-01-29 22:04:19 +00:00
|
|
|
from django.utils.formats import get_format
|
2017-10-22 03:00:28 +00:00
|
|
|
|
|
|
|
from faker import Factory
|
|
|
|
|
|
|
|
from core import models
|
|
|
|
|
|
|
|
|
2020-01-29 22:04:19 +00:00
|
|
|
class FormsTestCaseBase(TestCase):
|
2020-01-30 18:39:29 +00:00
|
|
|
c = None
|
2020-01-29 22:42:59 +00:00
|
|
|
child = None
|
2020-01-30 18:39:29 +00:00
|
|
|
user = None
|
2020-01-29 22:42:59 +00:00
|
|
|
|
2017-10-22 03:00:28 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2020-01-29 22:04:19 +00:00
|
|
|
super(FormsTestCaseBase, cls).setUpClass()
|
2017-10-22 03:00:28 +00:00
|
|
|
fake = Factory.create()
|
|
|
|
call_command('migrate', verbosity=0)
|
|
|
|
|
|
|
|
cls.c = HttpClient()
|
|
|
|
|
|
|
|
fake_user = fake.simple_profile()
|
2020-01-30 18:39:29 +00:00
|
|
|
credentials = {
|
2017-10-22 03:00:28 +00:00
|
|
|
'username': fake_user['username'],
|
|
|
|
'password': fake.password()
|
|
|
|
}
|
|
|
|
cls.user = User.objects.create_user(
|
2020-01-30 18:39:29 +00:00
|
|
|
is_superuser=True, **credentials)
|
|
|
|
cls.c.login(**credentials)
|
2017-10-22 03:00:28 +00:00
|
|
|
|
2020-01-29 22:42:59 +00:00
|
|
|
cls.child = models.Child.objects.create(
|
2020-01-30 18:39:29 +00:00
|
|
|
first_name='Child',
|
|
|
|
last_name='One',
|
2020-01-29 22:42:59 +00:00
|
|
|
birth_date=timezone.localdate()
|
|
|
|
)
|
|
|
|
|
2020-01-29 22:04:19 +00:00
|
|
|
@staticmethod
|
|
|
|
def localdate_string(datetime=None):
|
|
|
|
""" Converts an object to a local date string for form input. """
|
|
|
|
date_format = get_format('DATE_INPUT_FORMATS')[0]
|
|
|
|
return timezone.localdate(datetime).strftime(date_format)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def localtime_string(datetime=None):
|
|
|
|
""" Converts an object to a local time string for form input. """
|
|
|
|
datetime_format = get_format('DATETIME_INPUT_FORMATS')[0]
|
|
|
|
return timezone.localtime(datetime).strftime(datetime_format)
|
|
|
|
|
|
|
|
|
2020-01-30 18:39:29 +00:00
|
|
|
class InitialValuesTestCase(FormsTestCaseBase):
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
super(InitialValuesTestCase, cls).setUpClass()
|
|
|
|
cls.timer = models.Timer.objects.create(
|
|
|
|
user=cls.user,
|
|
|
|
start=timezone.localtime() - timezone.timedelta(minutes=30)
|
|
|
|
)
|
2020-01-29 22:04:19 +00:00
|
|
|
|
2020-01-30 18:39:29 +00:00
|
|
|
def test_child_with_one_child(self):
|
|
|
|
page = self.c.get('/sleep/add/')
|
|
|
|
self.assertEqual(page.context['form'].initial['child'], self.child)
|
2020-01-29 22:04:19 +00:00
|
|
|
|
2020-01-30 18:39:29 +00:00
|
|
|
def test_child_with_parameter(self):
|
2020-01-30 19:35:43 +00:00
|
|
|
child_two = models.Child.objects.create(
|
|
|
|
first_name='Child',
|
|
|
|
last_name='Two',
|
|
|
|
birth_date=timezone.localdate()
|
|
|
|
)
|
|
|
|
|
2020-01-30 18:39:29 +00:00
|
|
|
page = self.c.get('/sleep/add/')
|
|
|
|
self.assertTrue('child' not in page.context['form'].initial)
|
2020-01-29 22:04:19 +00:00
|
|
|
|
2020-01-30 18:39:29 +00:00
|
|
|
page = self.c.get('/sleep/add/?child={}'.format(self.child.slug))
|
|
|
|
self.assertEqual(page.context['form'].initial['child'], self.child)
|
2020-01-29 22:04:19 +00:00
|
|
|
|
2020-01-30 19:35:43 +00:00
|
|
|
page = self.c.get('/sleep/add/?child={}'.format(child_two.slug))
|
|
|
|
self.assertEqual(page.context['form'].initial['child'], child_two)
|
2020-01-29 22:04:19 +00:00
|
|
|
|
2020-01-30 18:39:29 +00:00
|
|
|
def test_feeding_type(self):
|
2020-01-30 19:35:43 +00:00
|
|
|
child_two = models.Child.objects.create(
|
|
|
|
first_name='Child',
|
|
|
|
last_name='Two',
|
|
|
|
birth_date=timezone.localdate()
|
|
|
|
)
|
2020-03-28 10:54:07 +00:00
|
|
|
child_three = models.Child.objects.create(
|
|
|
|
first_name='Child',
|
|
|
|
last_name='Three',
|
|
|
|
birth_date=timezone.localdate()
|
|
|
|
)
|
|
|
|
start_time = timezone.localtime() - timezone.timedelta(hours=4)
|
2020-03-30 12:25:57 +00:00
|
|
|
end_time = timezone.localtime() - timezone.timedelta(hours=3,
|
|
|
|
minutes=30)
|
2020-01-30 18:39:29 +00:00
|
|
|
f_one = models.Feeding.objects.create(
|
2020-01-29 22:42:59 +00:00
|
|
|
child=self.child,
|
2020-03-28 10:54:07 +00:00
|
|
|
start=start_time,
|
|
|
|
end=end_time,
|
2020-01-30 18:39:29 +00:00
|
|
|
type='breast milk',
|
|
|
|
method='left breast'
|
|
|
|
)
|
|
|
|
f_two = models.Feeding.objects.create(
|
2020-01-30 19:35:43 +00:00
|
|
|
child=child_two,
|
2020-03-28 10:54:07 +00:00
|
|
|
start=start_time,
|
|
|
|
end=end_time,
|
2020-01-30 18:39:29 +00:00
|
|
|
type='formula',
|
|
|
|
method='bottle'
|
2020-01-29 22:42:59 +00:00
|
|
|
)
|
2020-03-28 10:54:07 +00:00
|
|
|
f_three = models.Feeding.objects.create(
|
|
|
|
child=child_three,
|
|
|
|
start=start_time,
|
|
|
|
end=end_time,
|
|
|
|
type='fortified breast milk',
|
|
|
|
method='bottle'
|
|
|
|
)
|
2020-01-29 22:42:59 +00:00
|
|
|
|
2020-01-30 18:39:29 +00:00
|
|
|
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)
|
2020-03-28 10:54:07 +00:00
|
|
|
self.assertFalse('method' in page.context['form'].initial)
|
2020-01-30 18:39:29 +00:00
|
|
|
|
2020-01-30 19:35:43 +00:00
|
|
|
page = self.c.get('/feedings/add/?child={}'.format(child_two.slug))
|
2020-01-30 18:39:29 +00:00
|
|
|
self.assertEqual(page.context['form'].initial['type'], f_two.type)
|
2020-03-28 10:54:07 +00:00
|
|
|
self.assertEqual(page.context['form'].initial['method'], f_two.method)
|
|
|
|
|
|
|
|
page = self.c.get('/feedings/add/?child={}'.format(child_three.slug))
|
|
|
|
self.assertEqual(page.context['form'].initial['type'], f_three.type)
|
2020-03-30 12:25:57 +00:00
|
|
|
self.assertEqual(page.context['form'].initial['method'],
|
|
|
|
f_three.method)
|
2020-01-30 18:39:29 +00:00
|
|
|
|
|
|
|
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()
|
2020-01-29 22:04:19 +00:00
|
|
|
params = {
|
2020-01-30 18:39:29 +00:00
|
|
|
'child': self.child.id,
|
|
|
|
'start': self.localtime_string(self.timer.start),
|
|
|
|
'end': self.localtime_string(end)
|
2020-01-29 22:04:19 +00:00
|
|
|
}
|
2020-01-30 18:39:29 +00:00
|
|
|
page = self.c.post('/sleep/add/?timer={}'.format(self.timer.id),
|
|
|
|
params, follow=True)
|
2020-01-29 22:04:19 +00:00
|
|
|
self.assertEqual(page.status_code, 200)
|
2020-01-30 18:39:29 +00:00
|
|
|
self.timer.refresh_from_db()
|
|
|
|
self.assertFalse(self.timer.active)
|
|
|
|
self.assertEqual(self.localtime_string(self.timer.end), params['end'])
|
2020-01-29 22:04:19 +00:00
|
|
|
|
|
|
|
|
2020-01-30 19:35:43 +00:00
|
|
|
class ChildFormsTestCase(FormsTestCaseBase):
|
2020-01-29 22:04:19 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2020-01-30 19:35:43 +00:00
|
|
|
super(ChildFormsTestCase, cls).setUpClass()
|
2020-01-29 22:04:19 +00:00
|
|
|
cls.child = models.Child.objects.first()
|
|
|
|
|
|
|
|
def test_add(self):
|
2017-11-01 20:14:42 +00:00
|
|
|
params = {
|
2017-10-22 03:00:28 +00:00
|
|
|
'first_name': 'Child',
|
2020-01-30 19:35:43 +00:00
|
|
|
'last_name': 'Two',
|
2020-01-29 22:04:19 +00:00
|
|
|
'birth_date': timezone.localdate()
|
2017-11-01 20:14:42 +00:00
|
|
|
}
|
2020-01-29 22:04:19 +00:00
|
|
|
page = self.c.post('/children/add/', params, follow=True)
|
|
|
|
self.assertEqual(page.status_code, 200)
|
|
|
|
self.assertContains(page, 'Child entry added')
|
2017-11-01 20:14:42 +00:00
|
|
|
|
2020-01-29 22:04:19 +00:00
|
|
|
def test_edit(self):
|
|
|
|
params = {
|
|
|
|
'first_name': 'Name',
|
|
|
|
'last_name': 'Changed',
|
|
|
|
'birth_date': self.child.birth_date
|
|
|
|
}
|
|
|
|
page = self.c.post('/children/{}/edit/'.format(self.child.slug),
|
|
|
|
params, follow=True)
|
|
|
|
self.assertEqual(page.status_code, 200)
|
|
|
|
self.child.refresh_from_db()
|
|
|
|
self.assertEqual(self.child.last_name, params['last_name'])
|
|
|
|
self.assertContains(page, 'Child entry updated')
|
2017-11-01 20:14:42 +00:00
|
|
|
|
2020-01-29 22:04:19 +00:00
|
|
|
def test_delete(self):
|
2017-11-01 20:14:42 +00:00
|
|
|
params = {'confirm_name': 'Incorrect'}
|
2020-01-29 22:04:19 +00:00
|
|
|
page = self.c.post('/children/{}/delete/'.format(self.child.slug),
|
|
|
|
params, follow=True)
|
2017-11-01 20:14:42 +00:00
|
|
|
self.assertEqual(page.status_code, 200)
|
|
|
|
self.assertFormError(page, 'form', 'confirm_name',
|
|
|
|
'Name does not match child name.')
|
|
|
|
|
2020-01-29 22:04:19 +00:00
|
|
|
params['confirm_name'] = str(self.child)
|
|
|
|
page = self.c.post('/children/{}/delete/'.format(self.child.slug),
|
|
|
|
params, follow=True)
|
|
|
|
self.assertEqual(page.status_code, 200)
|
|
|
|
self.assertContains(page, 'Child entry deleted')
|
|
|
|
|
|
|
|
|
2020-01-30 19:35:43 +00:00
|
|
|
class DiaperChangeFormsTestCase(FormsTestCaseBase):
|
2020-01-29 22:04:19 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2020-01-30 19:35:43 +00:00
|
|
|
super(DiaperChangeFormsTestCase, cls).setUpClass()
|
2020-01-29 22:42:59 +00:00
|
|
|
cls.change = models.DiaperChange.objects.create(
|
|
|
|
child=cls.child,
|
|
|
|
time=timezone.localtime(),
|
|
|
|
wet=True,
|
|
|
|
solid=True,
|
|
|
|
color='black',
|
|
|
|
amount=0.45
|
|
|
|
)
|
2017-10-22 03:00:28 +00:00
|
|
|
|
2020-01-29 22:04:19 +00:00
|
|
|
def test_add(self):
|
|
|
|
child = models.Child.objects.first()
|
2017-11-01 20:14:42 +00:00
|
|
|
params = {
|
2020-01-29 22:04:19 +00:00
|
|
|
'child': child.id,
|
|
|
|
'time': self.localtime_string(),
|
2020-01-26 21:22:20 +00:00
|
|
|
'color': 'black',
|
|
|
|
'amount': 0.45
|
2017-11-01 20:14:42 +00:00
|
|
|
}
|
2020-01-29 22:04:19 +00:00
|
|
|
page = self.c.post('/changes/add/', params)
|
2017-11-01 20:14:42 +00:00
|
|
|
self.assertEqual(page.status_code, 200)
|
|
|
|
self.assertFormError(page, 'form', None,
|
|
|
|
'Wet and/or solid is required.')
|
|
|
|
|
2020-01-29 22:04:19 +00:00
|
|
|
params.update({'wet': 1, 'solid': 1, 'color': 'black'})
|
|
|
|
page = self.c.post('/changes/add/', params, follow=True)
|
|
|
|
self.assertEqual(page.status_code, 200)
|
|
|
|
self.assertContains(
|
|
|
|
page,
|
|
|
|
'Diaper Change entry for {} added'.format(str(child))
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_edit(self):
|
2017-10-22 03:00:28 +00:00
|
|
|
params = {
|
2020-01-29 22:04:19 +00:00
|
|
|
'child': self.change.child.id,
|
|
|
|
'time': self.localtime_string(),
|
|
|
|
'wet': self.change.wet,
|
|
|
|
'solid': self.change.solid,
|
|
|
|
'color': self.change.color,
|
|
|
|
'amount': 1.23
|
|
|
|
}
|
|
|
|
page = self.c.post('/changes/{}/'.format(self.change.id),
|
|
|
|
params, follow=True)
|
|
|
|
self.assertEqual(page.status_code, 200)
|
|
|
|
self.change.refresh_from_db()
|
|
|
|
self.assertEqual(self.change.amount, params['amount'])
|
|
|
|
self.assertContains(
|
|
|
|
page,
|
|
|
|
'Diaper Change entry for {} updated'.format(str(self.change.child))
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_delete(self):
|
|
|
|
page = self.c.post('/changes/{}/delete/'.format(self.change.id),
|
|
|
|
follow=True)
|
|
|
|
self.assertEqual(page.status_code, 200)
|
|
|
|
self.assertContains(page, 'Diaper Change entry deleted')
|
|
|
|
|
|
|
|
|
2020-01-30 19:35:43 +00:00
|
|
|
class FeedingFormsTestCase(FormsTestCaseBase):
|
2020-01-29 22:04:19 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2020-01-30 19:35:43 +00:00
|
|
|
super(FeedingFormsTestCase, cls).setUpClass()
|
2020-01-29 22:42:59 +00:00
|
|
|
cls.feeding = models.Feeding.objects.create(
|
|
|
|
child=cls.child,
|
|
|
|
start=timezone.localtime() - timezone.timedelta(hours=2),
|
|
|
|
end=timezone.localtime() - timezone.timedelta(hours=1, minutes=30),
|
|
|
|
type='breast milk',
|
|
|
|
method='left breast',
|
|
|
|
amount=2.5
|
|
|
|
)
|
2020-01-29 22:04:19 +00:00
|
|
|
|
|
|
|
def test_add(self):
|
|
|
|
end = timezone.localtime()
|
|
|
|
start = end - timezone.timedelta(minutes=30)
|
|
|
|
params = {
|
2020-01-29 22:42:59 +00:00
|
|
|
'child': self.child.id,
|
2020-01-29 22:04:19 +00:00
|
|
|
'start': self.localtime_string(start),
|
|
|
|
'end': self.localtime_string(end),
|
2017-11-01 01:01:21 +00:00
|
|
|
'type': 'formula',
|
2020-02-17 22:20:38 +00:00
|
|
|
'method': 'bottle',
|
2017-10-22 03:00:28 +00:00
|
|
|
'amount': 0
|
|
|
|
}
|
2020-01-29 22:04:19 +00:00
|
|
|
page = self.c.post('/feedings/add/', params, follow=True)
|
|
|
|
self.assertEqual(page.status_code, 200)
|
|
|
|
self.assertContains(
|
|
|
|
page,
|
2020-01-29 22:42:59 +00:00
|
|
|
'Feeding entry for {} added'.format(str(self.child))
|
2020-01-29 22:04:19 +00:00
|
|
|
)
|
2017-10-22 03:00:28 +00:00
|
|
|
|
2020-01-29 22:04:19 +00:00
|
|
|
def test_edit(self):
|
|
|
|
end = timezone.localtime()
|
|
|
|
start = end - timezone.timedelta(minutes=30)
|
2019-05-17 04:33:26 +00:00
|
|
|
params = {
|
2020-01-29 22:04:19 +00:00
|
|
|
'child': self.feeding.child.id,
|
|
|
|
'start': self.localtime_string(start),
|
|
|
|
'end': self.localtime_string(end),
|
|
|
|
'type': self.feeding.type,
|
|
|
|
'method': self.feeding.method,
|
|
|
|
'amount': 100
|
2019-05-17 04:33:26 +00:00
|
|
|
}
|
2020-01-29 22:04:19 +00:00
|
|
|
page = self.c.post('/feedings/{}/'.format(self.feeding.id),
|
|
|
|
params, follow=True)
|
|
|
|
self.assertEqual(page.status_code, 200)
|
|
|
|
self.feeding.refresh_from_db()
|
|
|
|
self.assertEqual(self.feeding.amount, params['amount'])
|
|
|
|
self.assertContains(
|
|
|
|
page,
|
|
|
|
'Feeding entry for {} updated'.format(str(self.feeding.child))
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_delete(self):
|
|
|
|
page = self.c.post('/feedings/{}/delete/'.format(self.feeding.id),
|
|
|
|
follow=True)
|
|
|
|
self.assertEqual(page.status_code, 200)
|
|
|
|
self.assertContains(page, 'Feeding entry deleted')
|
2019-05-17 04:33:26 +00:00
|
|
|
|
|
|
|
|
2020-01-30 19:35:43 +00:00
|
|
|
class SleepFormsTestCase(FormsTestCaseBase):
|
2020-01-29 22:04:19 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2020-01-30 19:35:43 +00:00
|
|
|
super(SleepFormsTestCase, cls).setUpClass()
|
2020-01-29 22:42:59 +00:00
|
|
|
cls.sleep = models.Sleep.objects.create(
|
|
|
|
child=cls.child,
|
|
|
|
start=timezone.localtime() - timezone.timedelta(hours=6),
|
|
|
|
end=timezone.localtime() - timezone.timedelta(hours=4)
|
|
|
|
)
|
2017-10-28 17:27:33 +00:00
|
|
|
|
2020-01-29 22:04:19 +00:00
|
|
|
def test_add(self):
|
2021-10-27 02:43:26 +00:00
|
|
|
# Prevent potential sleep entry intersection errors.
|
|
|
|
models.Sleep.objects.all().delete()
|
|
|
|
|
2020-01-29 22:04:19 +00:00
|
|
|
end = timezone.localtime()
|
|
|
|
start = end - timezone.timedelta(minutes=2)
|
2017-10-28 17:27:33 +00:00
|
|
|
params = {
|
2020-01-29 22:42:59 +00:00
|
|
|
'child': self.child.id,
|
2020-01-29 22:04:19 +00:00
|
|
|
'start': self.localtime_string(start),
|
|
|
|
'end': self.localtime_string(end),
|
2017-10-28 17:27:33 +00:00
|
|
|
}
|
|
|
|
|
2020-01-29 22:04:19 +00:00
|
|
|
page = self.c.post('/sleep/add/', params, follow=True)
|
|
|
|
self.assertEqual(page.status_code, 200)
|
|
|
|
self.assertContains(
|
|
|
|
page,
|
2020-01-29 22:42:59 +00:00
|
|
|
'Sleep entry for {} added'.format(str(self.child))
|
2020-01-29 22:04:19 +00:00
|
|
|
)
|
2017-11-04 03:29:55 +00:00
|
|
|
|
2020-01-29 22:04:19 +00:00
|
|
|
def test_edit(self):
|
|
|
|
end = timezone.localtime()
|
|
|
|
start = end - timezone.timedelta(minutes=2)
|
2017-10-22 03:00:28 +00:00
|
|
|
params = {
|
2020-01-29 22:04:19 +00:00
|
|
|
'child': self.sleep.child.id,
|
|
|
|
'start': self.localtime_string(start),
|
|
|
|
'end': self.localtime_string(end),
|
2017-10-22 03:00:28 +00:00
|
|
|
}
|
2020-01-29 22:04:19 +00:00
|
|
|
page = self.c.post('/sleep/{}/'.format(self.sleep.id),
|
|
|
|
params, follow=True)
|
|
|
|
self.assertEqual(page.status_code, 200)
|
|
|
|
self.sleep.refresh_from_db()
|
|
|
|
self.assertEqual(
|
|
|
|
self.localtime_string(self.sleep.end),
|
|
|
|
params['end']
|
|
|
|
)
|
|
|
|
self.assertContains(
|
|
|
|
page,
|
|
|
|
'Sleep entry for {} updated'.format(str(self.sleep.child))
|
|
|
|
)
|
2017-10-22 03:00:28 +00:00
|
|
|
|
2020-01-29 22:04:19 +00:00
|
|
|
def test_delete(self):
|
|
|
|
page = self.c.post('/sleep/{}/delete/'.format(self.sleep.id),
|
|
|
|
follow=True)
|
|
|
|
self.assertEqual(page.status_code, 200)
|
|
|
|
self.assertContains(page, 'Sleep entry deleted')
|
2017-11-01 16:44:07 +00:00
|
|
|
|
2020-01-29 22:04:19 +00:00
|
|
|
|
2020-01-30 19:35:43 +00:00
|
|
|
class TemperatureFormsTestCase(FormsTestCaseBase):
|
2020-01-29 22:04:19 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2020-01-30 19:35:43 +00:00
|
|
|
super(TemperatureFormsTestCase, cls).setUpClass()
|
2020-01-29 22:42:59 +00:00
|
|
|
cls.temp = models.Temperature.objects.create(
|
|
|
|
child=cls.child,
|
|
|
|
temperature=98.6,
|
|
|
|
time=timezone.localtime() - timezone.timedelta(days=1)
|
|
|
|
)
|
2020-01-29 22:04:19 +00:00
|
|
|
|
|
|
|
def test_add(self):
|
2017-11-10 02:15:09 +00:00
|
|
|
params = {
|
2020-01-29 22:42:59 +00:00
|
|
|
'child': self.child.id,
|
2020-01-29 22:04:19 +00:00
|
|
|
'temperature': '98.6',
|
|
|
|
'time': self.localtime_string()
|
2017-11-10 02:15:09 +00:00
|
|
|
}
|
|
|
|
|
2020-01-29 22:04:19 +00:00
|
|
|
page = self.c.post('/temperature/add/', params, follow=True)
|
|
|
|
self.assertEqual(page.status_code, 200)
|
|
|
|
self.assertContains(
|
|
|
|
page,
|
2020-01-29 22:42:59 +00:00
|
|
|
'Temperature entry for {} added'.format(str(self.child))
|
2020-01-29 22:04:19 +00:00
|
|
|
)
|
2017-11-10 02:15:09 +00:00
|
|
|
|
2020-01-29 22:04:19 +00:00
|
|
|
def test_edit(self):
|
2017-11-10 02:15:09 +00:00
|
|
|
params = {
|
2020-01-29 22:04:19 +00:00
|
|
|
'child': self.temp.child.id,
|
|
|
|
'temperature': self.temp.temperature + 2,
|
|
|
|
'time': self.localtime_string()
|
2017-11-10 02:15:09 +00:00
|
|
|
}
|
2020-01-29 22:04:19 +00:00
|
|
|
page = self.c.post('/temperature/{}/'.format(self.temp.id),
|
|
|
|
params, follow=True)
|
|
|
|
self.assertEqual(page.status_code, 200)
|
|
|
|
self.temp.refresh_from_db()
|
|
|
|
self.assertEqual(self.temp.temperature, params['temperature'])
|
|
|
|
self.assertContains(
|
|
|
|
page,
|
|
|
|
'Temperature entry for {} updated'.format(str(self.temp.child))
|
|
|
|
)
|
2017-11-10 02:15:09 +00:00
|
|
|
|
2020-01-29 22:04:19 +00:00
|
|
|
def test_delete(self):
|
|
|
|
page = self.c.post('/temperature/{}/delete/'.format(self.temp.id),
|
|
|
|
follow=True)
|
2017-11-10 02:15:09 +00:00
|
|
|
self.assertEqual(page.status_code, 200)
|
2020-01-29 22:04:19 +00:00
|
|
|
self.assertContains(page, 'Temperature entry deleted')
|
2017-11-10 02:15:09 +00:00
|
|
|
|
2020-01-29 22:04:19 +00:00
|
|
|
|
2020-01-30 19:35:43 +00:00
|
|
|
class TummyTimeFormsTestCase(FormsTestCaseBase):
|
2020-01-29 22:04:19 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2020-01-30 19:35:43 +00:00
|
|
|
super(TummyTimeFormsTestCase, cls).setUpClass()
|
2020-01-29 22:42:59 +00:00
|
|
|
cls.tt = models.TummyTime.objects.create(
|
|
|
|
child=cls.child,
|
|
|
|
start=timezone.localtime() - timezone.timedelta(hours=2),
|
|
|
|
end=timezone.localtime() - timezone.timedelta(hours=1, minutes=50)
|
|
|
|
)
|
2020-01-29 22:04:19 +00:00
|
|
|
|
|
|
|
def test_add(self):
|
|
|
|
end = timezone.localtime()
|
|
|
|
start = end - timezone.timedelta(minutes=2)
|
2017-11-04 03:29:55 +00:00
|
|
|
params = {
|
2020-01-29 22:42:59 +00:00
|
|
|
'child': self.child.id,
|
2020-01-29 22:04:19 +00:00
|
|
|
'start': self.localtime_string(start),
|
|
|
|
'end': self.localtime_string(end),
|
2017-11-04 03:29:55 +00:00
|
|
|
'milestone': ''
|
|
|
|
}
|
|
|
|
|
2020-01-29 22:04:19 +00:00
|
|
|
page = self.c.post('/tummy-time/add/', params, follow=True)
|
2017-11-01 16:44:07 +00:00
|
|
|
self.assertEqual(page.status_code, 200)
|
2020-01-29 22:04:19 +00:00
|
|
|
self.assertContains(
|
|
|
|
page,
|
2020-01-29 22:42:59 +00:00
|
|
|
'Tummy Time entry for {} added'.format(str(self.child))
|
2020-01-29 22:04:19 +00:00
|
|
|
)
|
2017-11-04 03:29:55 +00:00
|
|
|
|
2020-01-29 22:04:19 +00:00
|
|
|
def test_edit(self):
|
|
|
|
end = timezone.localtime()
|
|
|
|
start = end - timezone.timedelta(minutes=1, seconds=32)
|
|
|
|
params = {
|
|
|
|
'child': self.tt.child.id,
|
|
|
|
'start': self.localtime_string(start),
|
|
|
|
'end': self.localtime_string(end),
|
|
|
|
'milestone': 'Moved head!'
|
|
|
|
}
|
|
|
|
page = self.c.post('/tummy-time/{}/'.format(self.tt.id),
|
|
|
|
params, follow=True)
|
2017-11-04 03:29:55 +00:00
|
|
|
self.assertEqual(page.status_code, 200)
|
2020-01-29 22:04:19 +00:00
|
|
|
self.tt.refresh_from_db()
|
|
|
|
self.assertEqual(self.tt.milestone, params['milestone'])
|
|
|
|
self.assertContains(
|
|
|
|
page,
|
|
|
|
'Tummy Time entry for {} updated'.format(str(self.tt.child))
|
|
|
|
)
|
2017-11-04 03:29:55 +00:00
|
|
|
|
2020-01-29 22:04:19 +00:00
|
|
|
def test_delete(self):
|
|
|
|
page = self.c.post('/tummy-time/{}/delete/'.format(self.tt.id),
|
|
|
|
follow=True)
|
|
|
|
self.assertEqual(page.status_code, 200)
|
|
|
|
self.assertContains(page, 'Tummy Time entry deleted')
|
|
|
|
|
|
|
|
|
2020-01-30 19:35:43 +00:00
|
|
|
class TimerFormsTestCase(FormsTestCaseBase):
|
2020-01-30 21:32:01 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
super(TimerFormsTestCase, cls).setUpClass()
|
|
|
|
cls.timer = models.Timer.objects.create(user=cls.user)
|
|
|
|
|
2020-01-29 22:04:19 +00:00
|
|
|
def test_add(self):
|
2017-11-05 19:18:30 +00:00
|
|
|
params = {
|
2020-01-29 22:42:59 +00:00
|
|
|
'child': self.child.id,
|
2020-01-29 22:04:19 +00:00
|
|
|
'name': 'Test Timer',
|
|
|
|
'start': self.localtime_string()
|
2017-11-05 19:18:30 +00:00
|
|
|
}
|
2020-02-07 04:14:01 +00:00
|
|
|
page = self.c.post('/timers/add/', params, follow=True)
|
2020-01-29 22:04:19 +00:00
|
|
|
self.assertEqual(page.status_code, 200)
|
|
|
|
self.assertContains(page, params['name'])
|
|
|
|
self.assertContains(page, params['child'])
|
2017-11-05 19:18:30 +00:00
|
|
|
|
2020-01-29 22:04:19 +00:00
|
|
|
def test_edit(self):
|
2020-01-30 21:32:01 +00:00
|
|
|
start_time = self.timer.start - timezone.timedelta(hours=1)
|
2020-01-29 22:04:19 +00:00
|
|
|
params = {
|
|
|
|
'name': 'New Timer Name',
|
|
|
|
'start': self.localtime_string(start_time)
|
|
|
|
}
|
2020-02-07 04:14:01 +00:00
|
|
|
page = self.c.post('/timers/{}/edit/'.format(self.timer.id), params,
|
2020-01-29 22:04:19 +00:00
|
|
|
follow=True)
|
2017-11-04 03:29:55 +00:00
|
|
|
self.assertEqual(page.status_code, 200)
|
2020-01-29 22:04:19 +00:00
|
|
|
self.assertContains(page, params['name'])
|
2020-01-30 21:32:01 +00:00
|
|
|
self.timer.refresh_from_db()
|
|
|
|
self.assertEqual(
|
|
|
|
self.localtime_string(self.timer.start), params['start'])
|
|
|
|
|
|
|
|
def test_edit_stopped(self):
|
|
|
|
self.timer.stop()
|
|
|
|
params = {
|
|
|
|
'name': 'Edit stopped timer',
|
|
|
|
'start': self.localtime_string(self.timer.start),
|
|
|
|
'end': self.localtime_string(self.timer.end),
|
|
|
|
}
|
2020-02-07 04:14:01 +00:00
|
|
|
page = self.c.post('/timers/{}/edit/'.format(self.timer.id), params,
|
2020-01-30 21:32:01 +00:00
|
|
|
follow=True)
|
|
|
|
self.assertEqual(page.status_code, 200)
|
2017-11-05 19:18:30 +00:00
|
|
|
|
2020-02-07 05:50:22 +00:00
|
|
|
def test_delete_inactive(self):
|
|
|
|
models.Timer.objects.create(user=self.user)
|
|
|
|
self.assertEqual(models.Timer.objects.count(), 2)
|
|
|
|
self.timer.stop()
|
|
|
|
page = self.c.post('/timers/delete-inactive/', follow=True)
|
|
|
|
self.assertEqual(page.status_code, 200)
|
|
|
|
messages = list(page.context['messages'])
|
|
|
|
self.assertEqual(len(messages), 1)
|
|
|
|
self.assertEqual(str(messages[0]), 'All inactive timers deleted.')
|
|
|
|
self.assertEqual(models.Timer.objects.count(), 1)
|
|
|
|
|
2020-01-29 22:04:19 +00:00
|
|
|
|
2020-01-30 18:39:29 +00:00
|
|
|
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.')
|
|
|
|
|
|
|
|
|
2020-01-29 22:04:19 +00:00
|
|
|
class WeightFormsTest(FormsTestCaseBase):
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
super(WeightFormsTest, cls).setUpClass()
|
2020-01-29 22:42:59 +00:00
|
|
|
cls.weight = models.Weight.objects.create(
|
|
|
|
child=cls.child,
|
|
|
|
weight=8,
|
|
|
|
date=timezone.localdate() - timezone.timedelta(days=2)
|
|
|
|
)
|
2020-01-29 22:04:19 +00:00
|
|
|
|
|
|
|
def test_add(self):
|
|
|
|
params = {
|
2020-01-29 22:42:59 +00:00
|
|
|
'child': self.child.id,
|
2020-01-29 22:04:19 +00:00
|
|
|
'weight': 8.5,
|
|
|
|
'date': self.localdate_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
page = self.c.post('/weight/add/', params, follow=True)
|
|
|
|
self.assertEqual(page.status_code, 200)
|
|
|
|
self.assertContains(
|
|
|
|
page,
|
2020-01-29 22:42:59 +00:00
|
|
|
'Weight entry for {} added'.format(str(self.child))
|
2017-11-05 19:18:30 +00:00
|
|
|
)
|
|
|
|
|
2020-01-29 22:04:19 +00:00
|
|
|
def test_edit(self):
|
2017-11-05 19:18:30 +00:00
|
|
|
params = {
|
2020-01-29 22:04:19 +00:00
|
|
|
'child': self.weight.child.id,
|
|
|
|
'weight': self.weight.weight + 1,
|
|
|
|
'date': self.localdate_string()
|
2017-11-05 19:18:30 +00:00
|
|
|
}
|
2020-01-29 22:04:19 +00:00
|
|
|
page = self.c.post('/weight/{}/'.format(self.weight.id),
|
|
|
|
params, follow=True)
|
2017-11-05 19:18:30 +00:00
|
|
|
self.assertEqual(page.status_code, 200)
|
2020-01-29 22:04:19 +00:00
|
|
|
self.weight.refresh_from_db()
|
|
|
|
self.assertEqual(self.weight.weight, params['weight'])
|
|
|
|
self.assertContains(
|
2017-11-05 19:18:30 +00:00
|
|
|
page,
|
2020-01-29 22:04:19 +00:00
|
|
|
'Weight entry for {} updated'.format(str(self.weight.child))
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_delete(self):
|
|
|
|
page = self.c.post('/weight/{}/delete/'.format(self.weight.id),
|
|
|
|
follow=True)
|
|
|
|
self.assertEqual(page.status_code, 200)
|
|
|
|
self.assertContains(page, 'Weight entry deleted')
|