From 4445faeeacc842ac526dc0be20e0eea599589bf0 Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Wed, 25 May 2022 21:14:09 -0700 Subject: [PATCH] Add basic model tagging tests --- core/tests/tests_models.py | 41 ++++++++++++++++++++++++++++++++++---- core/tests/tests_utils.py | 21 ++++++++++++------- 2 files changed, 51 insertions(+), 11 deletions(-) diff --git a/core/tests/tests_models.py b/core/tests/tests_models.py index ccc16337..654b35bf 100644 --- a/core/tests/tests_models.py +++ b/core/tests/tests_models.py @@ -181,16 +181,16 @@ class PumpingTestCase(TestCase): self.child = models.Child.objects.create( first_name="First", last_name="Last", birth_date=timezone.localdate() ) - self.temp = models.Pumping.objects.create( + self.pumping = models.Pumping.objects.create( child=self.child, time=timezone.localtime() - timezone.timedelta(days=1), amount=98.6, ) def test_pumping_create(self): - self.assertEqual(self.temp, models.Pumping.objects.first()) - self.assertEqual(str(self.temp), "Pumping") - self.assertEqual(self.temp.amount, 98.6) + self.assertEqual(self.pumping, models.Pumping.objects.first()) + self.assertEqual(str(self.pumping), "Pumping") + self.assertEqual(self.pumping.amount, 98.6) class SleepTestCase(TestCase): @@ -211,6 +211,39 @@ class SleepTestCase(TestCase): self.assertEqual(sleep.duration, sleep.end - sleep.start) +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() + ) + + class TemperatureTestCase(TestCase): def setUp(self): call_command("migrate", verbosity=0) diff --git a/core/tests/tests_utils.py b/core/tests/tests_utils.py index ecee04f9..4a83d62b 100644 --- a/core/tests/tests_utils.py +++ b/core/tests/tests_utils.py @@ -2,18 +2,25 @@ from django.test import TestCase from django.utils import timezone -from core.utils import duration_string, duration_parts +from core import utils class UtilsTestCase(TestCase): def test_duration_string(self): duration = timezone.timedelta(hours=1, minutes=30, seconds=45) - self.assertEqual(duration_string(duration), "1 hour, 30 minutes, 45 seconds") - self.assertEqual(duration_string(duration, "m"), "1 hour, 30 minutes") - self.assertEqual(duration_string(duration, "h"), "1 hour") - self.assertRaises(TypeError, lambda: duration_string("1 hour")) + self.assertEqual( + utils.duration_string(duration), "1 hour, 30 minutes, 45 seconds" + ) + self.assertEqual(utils.duration_string(duration, "m"), "1 hour, 30 minutes") + self.assertEqual(utils.duration_string(duration, "h"), "1 hour") + self.assertRaises(TypeError, lambda: utils.duration_string("1 hour")) def test_duration_parts(self): duration = timezone.timedelta(hours=1, minutes=30, seconds=45) - self.assertEqual(duration_parts(duration), (1, 30, 45)) - self.assertRaises(TypeError, lambda: duration_parts("1 hour")) + self.assertEqual(utils.duration_parts(duration), (1, 30, 45)) + self.assertRaises(TypeError, lambda: utils.duration_parts("1 hour")) + + def test_random_color(self): + color = utils.random_color() + self.assertIsInstance(color, str) + self.assertIn(color, utils.COLORS)