mybuddy/core/tests/tests_models.py

401 lines
14 KiB
Python
Raw Normal View History

2017-10-25 14:20:13 +00:00
# -*- coding: utf-8 -*-
import datetime
from django.contrib.auth import get_user_model
2017-10-25 14:20:13 +00:00
from django.core.management import call_command
from django.test import TestCase
from django.utils import timezone
from core import models
class BMITestCase(TestCase):
def setUp(self):
call_command("migrate", verbosity=0)
self.child = models.Child.objects.create(
first_name="First", last_name="Last", birth_date=timezone.localdate()
)
self.bmi = models.BMI.objects.create(
child=self.child,
date=timezone.localdate(),
bmi=63.2,
)
def test_weight_create(self):
self.assertEqual(self.bmi, models.BMI.objects.first())
self.assertEqual(str(self.bmi), "BMI")
self.assertEqual(self.bmi.bmi, 63.2)
2017-10-25 14:20:13 +00:00
class ChildTestCase(TestCase):
def setUp(self):
2022-02-10 00:00:30 +00:00
call_command("migrate", verbosity=0)
2017-10-25 14:20:13 +00:00
def test_child_create(self):
child = models.Child.objects.create(
2022-02-10 00:00:30 +00:00
first_name="First", last_name="Last", birth_date=timezone.localdate()
2017-10-25 14:20:13 +00:00
)
2022-02-10 00:00:30 +00:00
self.assertEqual(child, models.Child.objects.get(first_name="First"))
self.assertEqual(child.slug, "first-last")
self.assertEqual(str(child), "First Last")
self.assertEqual(child.name(), "First Last")
self.assertEqual(child.name(reverse=True), "Last, First")
2017-10-25 14:20:13 +00:00
2022-05-26 03:35:45 +00:00
def test_child_create_without_last_name(self):
child = models.Child.objects.create(
first_name="Nolastname", birth_date=timezone.localdate()
)
self.assertEqual(child, models.Child.objects.get(first_name="Nolastname"))
self.assertEqual(child.slug, "nolastname")
self.assertEqual(str(child), "Nolastname")
self.assertEqual(child.name(), "Nolastname")
self.assertEqual(child.name(reverse=True), "Nolastname")
def test_child_count(self):
self.assertEqual(models.Child.count(), 0)
models.Child.objects.create(
2022-02-10 00:00:30 +00:00
first_name="First 1", last_name="Last 1", birth_date=timezone.localdate()
)
self.assertEqual(models.Child.count(), 1)
child = models.Child.objects.create(
2022-02-10 00:00:30 +00:00
first_name="First 2", last_name="Last 2", birth_date=timezone.localdate()
)
self.assertEqual(models.Child.count(), 2)
child.delete()
self.assertEqual(models.Child.count(), 1)
2023-10-21 19:57:46 +00:00
def test_child_birth_datetime(self):
birth_date = timezone.localdate()
models.Child.objects.create(
first_name="First", last_name="Last", birth_date=birth_date
)
self.assertEqual(models.Child.objects.last().birth_datetime(), birth_date)
birth_time = datetime.datetime.now().time()
models.Child.objects.create(
first_name="Second",
last_name="Last",
birth_date=birth_date,
birth_time=birth_time,
)
self.assertEqual(
models.Child.objects.last().birth_datetime(),
timezone.make_aware(datetime.datetime.combine(birth_date, birth_time)),
)
2017-10-25 14:20:13 +00:00
class DiaperChangeTestCase(TestCase):
def setUp(self):
2022-02-10 00:00:30 +00:00
call_command("migrate", verbosity=0)
2017-10-25 14:20:13 +00:00
self.child = models.Child.objects.create(
2022-02-10 00:00:30 +00:00
first_name="First", last_name="Last", birth_date=timezone.localdate()
2017-10-25 14:20:13 +00:00
)
self.change = models.DiaperChange.objects.create(
child=self.child,
time=timezone.localtime() - timezone.timedelta(days=1),
wet=1,
solid=1,
2022-02-10 00:00:30 +00:00
color="black",
amount=1.25,
2017-10-25 14:20:13 +00:00
)
def test_diaperchange_create(self):
self.assertEqual(self.change, models.DiaperChange.objects.first())
2022-02-10 00:00:30 +00:00
self.assertEqual(str(self.change), "Diaper Change")
self.assertEqual(self.change.child, self.child)
self.assertTrue(self.change.wet)
self.assertTrue(self.change.solid)
2022-02-10 00:00:30 +00:00
self.assertEqual(self.change.color, "black")
self.assertEqual(self.change.amount, 1.25)
2017-10-25 14:20:13 +00:00
def test_diaperchange_attributes(self):
2022-02-10 00:00:30 +00:00
self.assertListEqual(self.change.attributes(), ["Wet", "Solid", "Black"])
2017-10-25 14:20:13 +00:00
class FeedingTestCase(TestCase):
2017-10-25 14:20:13 +00:00
def setUp(self):
2022-02-10 00:00:30 +00:00
call_command("migrate", verbosity=0)
2017-10-25 14:20:13 +00:00
self.child = models.Child.objects.create(
2022-02-10 00:00:30 +00:00
first_name="First", last_name="Last", birth_date=timezone.localdate()
2017-10-25 14:20:13 +00:00
)
def test_feeding_create(self):
feeding = models.Feeding.objects.create(
child=self.child,
start=timezone.localtime() - timezone.timedelta(minutes=30),
end=timezone.localtime(),
2022-02-10 00:00:30 +00:00
type="formula",
method="bottle",
amount=2,
2017-10-25 14:20:13 +00:00
)
self.assertEqual(feeding, models.Feeding.objects.first())
2022-02-10 00:00:30 +00:00
self.assertEqual(str(feeding), "Feeding")
self.assertEqual(feeding.duration, feeding.end - feeding.start)
2017-10-25 14:20:13 +00:00
def test_method_both_breasts(self):
feeding = models.Feeding.objects.create(
child=self.child,
start=timezone.localtime() - timezone.timedelta(minutes=30),
end=timezone.localtime(),
2022-02-10 00:00:30 +00:00
type="breast milk",
method="both breasts",
)
self.assertEqual(feeding, models.Feeding.objects.first())
2022-02-10 00:00:30 +00:00
self.assertEqual(str(feeding), "Feeding")
self.assertEqual(feeding.method, "both breasts")
2017-10-25 14:20:13 +00:00
class HeadCircumferenceTestCase(TestCase):
def setUp(self):
call_command("migrate", verbosity=0)
self.child = models.Child.objects.create(
first_name="First", last_name="Last", birth_date=timezone.localdate()
)
self.hc = models.HeadCircumference.objects.create(
child=self.child,
date=timezone.localdate(),
head_circumference=13.25,
)
def test_weight_create(self):
self.assertEqual(self.hc, models.HeadCircumference.objects.first())
self.assertEqual(str(self.hc), "Head Circumference")
self.assertEqual(self.hc.head_circumference, 13.25)
class HeightTestCase(TestCase):
def setUp(self):
call_command("migrate", verbosity=0)
self.child = models.Child.objects.create(
first_name="First", last_name="Last", birth_date=timezone.localdate()
)
self.height = models.Height.objects.create(
child=self.child,
date=timezone.localdate(),
height=34.5,
)
def test_weight_create(self):
self.assertEqual(self.height, models.Height.objects.first())
self.assertEqual(str(self.height), "Height")
self.assertEqual(self.height.height, 34.5)
2017-10-25 14:20:13 +00:00
class NoteTestCase(TestCase):
def setUp(self):
2022-02-10 00:00:30 +00:00
call_command("migrate", verbosity=0)
2017-10-25 14:20:13 +00:00
self.child = models.Child.objects.create(
2022-02-10 00:00:30 +00:00
first_name="First", last_name="Last", birth_date=timezone.localdate()
2017-10-25 14:20:13 +00:00
)
def test_note_create(self):
note = models.Note.objects.create(
2022-02-10 00:00:30 +00:00
child=self.child, note="Note", time=timezone.localtime()
)
2017-10-25 14:20:13 +00:00
self.assertEqual(note, models.Note.objects.first())
2022-02-10 00:00:30 +00:00
self.assertEqual(str(note), "Note")
2017-10-25 14:20:13 +00:00
class PumpingTestCase(TestCase):
def setUp(self):
call_command("migrate", verbosity=0)
self.child = models.Child.objects.create(
first_name="First", last_name="Last", birth_date=timezone.localdate()
)
start = timezone.localtime() - timezone.timedelta(days=1)
end = start + timezone.timedelta(minutes=14)
2022-05-26 04:14:09 +00:00
self.pumping = models.Pumping.objects.create(
child=self.child,
start=start,
end=end,
amount=98.6,
)
def test_pumping_create(self):
2022-05-26 04:14:09 +00:00
self.assertEqual(self.pumping, models.Pumping.objects.first())
self.assertEqual(str(self.pumping), "Pumping")
self.assertEqual(self.pumping.amount, 98.6)
2017-10-25 14:20:13 +00:00
class SleepTestCase(TestCase):
def setUp(self):
2022-02-10 00:00:30 +00:00
call_command("migrate", verbosity=0)
2017-10-25 14:20:13 +00:00
self.child = models.Child.objects.create(
2022-02-10 00:00:30 +00:00
first_name="First", last_name="Last", birth_date=timezone.localdate()
2017-10-25 14:20:13 +00:00
)
def test_sleep_create(self):
sleep = models.Sleep.objects.create(
child=self.child,
start=timezone.localtime() - timezone.timedelta(minutes=30),
end=timezone.localtime(),
)
self.assertEqual(sleep, models.Sleep.objects.first())
2022-02-10 00:00:30 +00:00
self.assertEqual(str(sleep), "Sleep")
self.assertEqual(sleep.duration, sleep.end - sleep.start)
2017-10-25 14:20:13 +00:00
def test_sleep_nap(self):
models.Sleep.settings.nap_start_min = datetime.time(0, 0, 0)
models.Sleep.settings.nap_start_max = datetime.time(23, 59, 59)
sleep = models.Sleep.objects.create(
child=self.child,
start=timezone.now(),
end=(timezone.now() + timezone.timedelta(hours=2)),
)
self.assertTrue(sleep.nap)
def test_sleep_not_nap(self):
models.Sleep.settings.nap_start_min = datetime.time(0, 0, 0)
models.Sleep.settings.nap_start_max = datetime.time(0, 0, 0)
sleep = models.Sleep.objects.create(
child=self.child,
start=timezone.now(),
end=(timezone.now() + timezone.timedelta(hours=8)),
)
self.assertFalse(sleep.nap)
sleep = models.Sleep.objects.create(
child=self.child,
start=timezone.now(),
end=(timezone.now() + timezone.timedelta(hours=8)),
nap=True,
)
self.assertTrue(sleep.nap)
2017-10-25 14:20:13 +00:00
2022-05-26 04:14:09 +00:00
class TagTestCase(TestCase):
def setUp(self):
call_command("migrate", verbosity=0)
self.child = models.Child.objects.create(
first_name="First", last_name="Last", birth_date=timezone.localdate()
)
def test_create_tag(self):
tag1 = models.Tag.objects.create(name="Tag 1")
self.assertEqual(tag1, models.Tag.objects.first())
tag2 = models.Tag.objects.create(name="Tag 2")
self.assertEqual(tag2, models.Tag.objects.filter(name="Tag 2").get())
def test_tag_complementary_color(self):
light_tag = models.Tag.objects.create(name="Light Tag", color="#ffffff")
self.assertEqual(light_tag.complementary_color, models.Tag.DARK_COLOR)
dark_tag = models.Tag.objects.create(name="Dark Tag", color="#000000")
self.assertEqual(dark_tag.complementary_color, models.Tag.LIGHT_COLOR)
def test_model_tagging(self):
temp = models.Temperature.objects.create(
child=self.child,
time=timezone.localtime() - timezone.timedelta(days=1),
temperature=98.6,
)
temp.tags.add("Tag 1")
self.assertEqual(
temp.tags.all().get(), models.Tag.objects.filter(name="Tag 1").get()
)
2019-05-17 04:33:26 +00:00
class TemperatureTestCase(TestCase):
def setUp(self):
2022-02-10 00:00:30 +00:00
call_command("migrate", verbosity=0)
2019-05-17 04:33:26 +00:00
self.child = models.Child.objects.create(
2022-02-10 00:00:30 +00:00
first_name="First", last_name="Last", birth_date=timezone.localdate()
2019-05-17 04:33:26 +00:00
)
self.temp = models.Temperature.objects.create(
child=self.child,
time=timezone.localtime() - timezone.timedelta(days=1),
2022-02-10 00:00:30 +00:00
temperature=98.6,
2019-05-17 04:33:26 +00:00
)
def test_temperature_create(self):
self.assertEqual(self.temp, models.Temperature.objects.first())
2022-02-10 00:00:30 +00:00
self.assertEqual(str(self.temp), "Temperature")
2019-05-17 04:33:26 +00:00
self.assertEqual(self.temp.temperature, 98.6)
2017-10-25 14:20:13 +00:00
class TimerTestCase(TestCase):
def setUp(self):
2022-02-10 00:00:30 +00:00
call_command("migrate", verbosity=0)
child = models.Child.objects.create(
2022-02-10 00:00:30 +00:00
first_name="First", last_name="Last", birth_date=timezone.localdate()
)
self.user = get_user_model().objects.first()
2017-10-25 14:20:13 +00:00
self.named = models.Timer.objects.create(
name="Named", user=self.user, child=child
2017-10-25 14:20:13 +00:00
)
self.unnamed = models.Timer.objects.create(user=self.user)
2017-10-25 14:20:13 +00:00
def test_timer_create(self):
2022-02-10 00:00:30 +00:00
self.assertEqual(self.named, models.Timer.objects.get(name="Named"))
self.assertEqual(str(self.named), "Named")
2017-10-25 14:20:13 +00:00
self.assertEqual(self.unnamed, models.Timer.objects.get(name=None))
2022-02-10 00:00:30 +00:00
self.assertEqual(str(self.unnamed), "Timer #{}".format(self.unnamed.id))
2017-10-25 14:20:13 +00:00
2020-01-30 21:32:01 +00:00
def test_timer_title_with_child(self):
self.assertEqual(self.named.title_with_child, str(self.named))
models.Child.objects.create(
2022-02-10 00:00:30 +00:00
first_name="Child", last_name="Two", birth_date=timezone.localdate()
2020-01-30 21:32:01 +00:00
)
self.assertEqual(
self.named.title_with_child,
2022-02-10 00:00:30 +00:00
"{} ({})".format(str(self.named), str(self.named.child)),
2020-01-30 21:32:01 +00:00
)
def test_timer_user_username(self):
self.assertEqual(self.named.user_username, self.user.get_username())
2022-02-10 00:00:30 +00:00
self.user.first_name = "User"
self.user.last_name = "Name"
self.user.save()
self.assertEqual(self.named.user_username, self.user.get_full_name())
2017-10-25 14:20:13 +00:00
def test_timer_restart(self):
self.named.restart()
self.assertGreaterEqual(timezone.localtime(), self.named.start)
2017-10-25 14:20:13 +00:00
def test_timer_duration(self):
timer = models.Timer.objects.create(user=get_user_model().objects.first())
2017-10-25 14:20:13 +00:00
timer.start = timezone.localtime() - timezone.timedelta(minutes=30)
timer.save()
timer.refresh_from_db()
2017-10-25 14:20:13 +00:00
self.assertEqual(
timer.duration().seconds, timezone.timedelta(minutes=30).seconds
)
2017-10-25 14:20:13 +00:00
class TummyTimeTestCase(TestCase):
def setUp(self):
2022-02-10 00:00:30 +00:00
call_command("migrate", verbosity=0)
2017-10-25 14:20:13 +00:00
self.child = models.Child.objects.create(
2022-02-10 00:00:30 +00:00
first_name="First", last_name="Last", birth_date=timezone.localdate()
2017-10-25 14:20:13 +00:00
)
def test_tummytime_create(self):
tummy_time = models.TummyTime.objects.create(
child=self.child,
start=timezone.localtime() - timezone.timedelta(minutes=30),
end=timezone.localtime(),
)
self.assertEqual(tummy_time, models.TummyTime.objects.first())
2022-02-10 00:00:30 +00:00
self.assertEqual(str(tummy_time), "Tummy Time")
self.assertEqual(tummy_time.duration, tummy_time.end - tummy_time.start)
class WeightTestCase(TestCase):
def setUp(self):
call_command("migrate", verbosity=0)
self.child = models.Child.objects.create(
first_name="First", last_name="Last", birth_date=timezone.localdate()
)
self.weight = models.Weight.objects.create(
child=self.child,
date=timezone.localdate(),
weight=23,
)
def test_weight_create(self):
self.assertEqual(self.weight, models.Weight.objects.first())
self.assertEqual(str(self.weight), "Weight")
self.assertEqual(self.weight.weight, 23)