From db5615fcb98792db8a452adaeaa7f15c9d73607a Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Fri, 27 May 2022 21:50:25 -0700 Subject: [PATCH] Add missing forms tests; separate notes and tags tests --- core/tests/tests_forms.py | 318 +++++++++++++++++++++++++++++--------- 1 file changed, 246 insertions(+), 72 deletions(-) diff --git a/core/tests/tests_forms.py b/core/tests/tests_forms.py index e61d875d..0751c4ec 100644 --- a/core/tests/tests_forms.py +++ b/core/tests/tests_forms.py @@ -145,6 +145,47 @@ class InitialValuesTestCase(FormsTestCaseBase): self.assertEqual(self.localtime_string(self.timer.end), params["end"]) +class BMIFormsTest(FormsTestCaseBase): + @classmethod + def setUpClass(cls): + super(BMIFormsTest, cls).setUpClass() + cls.bmi = models.BMI.objects.create( + child=cls.child, + bmi=30, + date=timezone.localdate() - timezone.timedelta(days=2), + ) + + def test_add(self): + params = { + "child": self.child.id, + "bmi": 35, + "date": self.localdate_string(), + } + + page = self.c.post("/bmi/add/", params, follow=True) + self.assertEqual(page.status_code, 200) + self.assertContains(page, "Bmi entry for {} added".format(str(self.child))) + + def test_edit(self): + params = { + "child": self.bmi.child.id, + "bmi": self.bmi.bmi + 1, + "date": self.bmi.date, + } + page = self.c.post("/bmi/{}/".format(self.bmi.id), params, follow=True) + self.assertEqual(page.status_code, 200) + self.bmi.refresh_from_db() + self.assertEqual(self.bmi.bmi, params["bmi"]) + self.assertContains( + page, "Bmi entry for {} updated".format(str(self.bmi.child)) + ) + + def test_delete(self): + page = self.c.post("/bmi/{}/delete/".format(self.bmi.id), follow=True) + self.assertEqual(page.status_code, 200) + self.assertContains(page, "Bmi entry deleted") + + class ChildFormsTestCase(FormsTestCaseBase): @classmethod def setUpClass(cls): @@ -294,102 +335,137 @@ class FeedingFormsTestCase(FormsTestCaseBase): self.assertContains(page, "Feeding entry deleted") -class NotesFormsTest(FormsTestCaseBase): - """ - Piggy-backs a bunch of tests for the tags-logic. - """ - +class HeadCircumferenceFormsTest(FormsTestCaseBase): @classmethod def setUpClass(cls): - super(NotesFormsTest, cls).setUpClass() - - cls.note = models.Note.objects.create( + super(HeadCircumferenceFormsTest, cls).setUpClass() + cls.head_circumference = models.HeadCircumference.objects.create( child=cls.child, - note="Setup note", - time=timezone.now() - timezone.timedelta(days=2), + head_circumference=15, + date=timezone.localdate() - timezone.timedelta(days=2), ) - cls.note.tags.add("oldtag") - cls.oldtag = models.Tag.objects.filter(slug="oldtag").first() - def test_add_no_tags(self): + def test_add(self): params = { "child": self.child.id, - "note": "note with no tags", - "time": (timezone.now() - timezone.timedelta(minutes=1)).isoformat(), + "head_circumference": 20, + "date": self.localdate_string(), } - page = self.c.post("/notes/add/", params, follow=True) + page = self.c.post("/head-circumference/add/", params, follow=True) self.assertEqual(page.status_code, 200) - self.assertContains(page, "note with no tags") - - def test_add_with_tags(self): - params = { - "child": self.child.id, - "note": "this note has tags", - "time": (timezone.now() - timezone.timedelta(minutes=1)).isoformat(), - "tags": 'A,B,"setup tag"', - } - - old_notes = list(models.Note.objects.all()) - - page = self.c.post("/notes/add/", params, follow=True) - self.assertEqual(page.status_code, 200) - self.assertContains(page, "this note has tags") - - new_notes = list(models.Note.objects.all()) - - # Find the new tag and extract its tags - old_pks = [n.pk for n in old_notes] - new_note = [n for n in new_notes if n.pk not in old_pks][0] - new_note_tag_names = [t.name for t in new_note.tags.all()] - - self.assertSetEqual(set(new_note_tag_names), {"A", "B", "setup tag"}) + self.assertContains( + page, "Head Circumference entry for {} added".format(str(self.child)) + ) def test_edit(self): - old_tag_last_used = self.oldtag.last_used + params = { + "child": self.head_circumference.child.id, + "head_circumference": self.head_circumference.head_circumference + 1, + "date": self.head_circumference.date, + } + page = self.c.post( + "/head-circumference/{}/".format(self.head_circumference.id), + params, + follow=True, + ) + self.assertEqual(page.status_code, 200) + self.head_circumference.refresh_from_db() + self.assertEqual( + self.head_circumference.head_circumference, params["head_circumference"] + ) + self.assertContains( + page, + "Head Circumference entry for {} updated".format( + str(self.head_circumference.child) + ), + ) + def test_delete(self): + page = self.c.post( + "/head-circumference/{}/delete/".format(self.head_circumference.id), + follow=True, + ) + self.assertEqual(page.status_code, 200) + self.assertContains(page, "Head Circumference entry deleted") + + +class HeightFormsTest(FormsTestCaseBase): + @classmethod + def setUpClass(cls): + super(HeightFormsTest, cls).setUpClass() + cls.height = models.Height.objects.create( + child=cls.child, + height=12.5, + date=timezone.localdate() - timezone.timedelta(days=2), + ) + + def test_add(self): + params = { + "child": self.child.id, + "height": 13.5, + "date": self.localdate_string(), + } + + page = self.c.post("/height/add/", params, follow=True) + self.assertEqual(page.status_code, 200) + self.assertContains(page, "Height entry for {} added".format(str(self.child))) + + def test_edit(self): + params = { + "child": self.height.child.id, + "height": self.height.height + 1, + "date": self.height.date, + } + page = self.c.post("/height/{}/".format(self.height.id), params, follow=True) + self.assertEqual(page.status_code, 200) + self.height.refresh_from_db() + self.assertEqual(self.height.height, params["height"]) + self.assertContains( + page, "Height entry for {} updated".format(str(self.height.child)) + ) + + def test_delete(self): + page = self.c.post("/height/{}/delete/".format(self.height.id), follow=True) + self.assertEqual(page.status_code, 200) + self.assertContains(page, "Height entry deleted") + + +class NoteFormsTest(FormsTestCaseBase): + @classmethod + def setUpClass(cls): + super(NoteFormsTest, cls).setUpClass() + cls.note = models.Note.objects.create( + child=cls.child, + note="Test note!", + time=timezone.localtime() - timezone.timedelta(days=2), + ) + + def test_add(self): + params = { + "child": self.child.id, + "note": "New note", + "time": self.localtime_string(), + } + + page = self.c.post("/notes/add/", params, follow=True) + self.assertEqual(page.status_code, 200) + self.assertContains(page, "Note entry for {} added".format(str(self.child))) + + def test_edit(self): params = { "child": self.note.child.id, - "note": "Edited note", - "time": self.localdate_string(), - "tags": "oldtag,newtag", + "note": "changed note", + "time": self.note.time, } page = self.c.post("/notes/{}/".format(self.note.id), params, follow=True) self.assertEqual(page.status_code, 200) - self.note.refresh_from_db() - self.oldtag.refresh_from_db() self.assertEqual(self.note.note, params["note"]) self.assertContains( page, "Note entry for {} updated".format(str(self.note.child)) ) - self.assertSetEqual( - set(t.name for t in self.note.tags.all()), {"oldtag", "newtag"} - ) - - # Old tag remains old, because it was not added - self.assertEqual(old_tag_last_used, self.oldtag.last_used) - - # Second phase: Remove all tags then add "oldtag" through posting - # which should update the last_used tag - self.note.tags.clear() - self.note.save() - - params = { - "child": self.note.child.id, - "note": "Edited note (2)", - "time": self.localdate_string(), - "tags": "oldtag", - } - page = self.c.post("/notes/{}/".format(self.note.id), params, follow=True) - self.assertEqual(page.status_code, 200) - - self.note.refresh_from_db() - self.oldtag.refresh_from_db() - - self.assertLess(old_tag_last_used, self.oldtag.last_used) - def test_delete(self): page = self.c.post("/notes/{}/delete/".format(self.note.id), follow=True) self.assertEqual(page.status_code, 200) @@ -485,6 +561,104 @@ class SleepFormsTestCase(FormsTestCaseBase): self.assertContains(page, "Sleep entry deleted") +class TaggedFormsTestCase(FormsTestCaseBase): + @classmethod + def setUpClass(cls): + super(TaggedFormsTestCase, cls).setUpClass() + + cls.note = models.Note.objects.create( + child=cls.child, + note="Setup note", + time=timezone.now() - timezone.timedelta(days=2), + ) + cls.note.tags.add("oldtag") + cls.oldtag = models.Tag.objects.filter(slug="oldtag").first() + + def test_add_no_tags(self): + params = { + "child": self.child.id, + "note": "note with no tags", + "time": (timezone.now() - timezone.timedelta(minutes=1)).isoformat(), + } + + page = self.c.post("/notes/add/", params, follow=True) + self.assertEqual(page.status_code, 200) + self.assertContains(page, "note with no tags") + + def test_add_with_tags(self): + params = { + "child": self.child.id, + "note": "this note has tags", + "time": (timezone.now() - timezone.timedelta(minutes=1)).isoformat(), + "tags": 'A,B,"setup tag"', + } + + old_notes = list(models.Note.objects.all()) + + page = self.c.post("/notes/add/", params, follow=True) + self.assertEqual(page.status_code, 200) + self.assertContains(page, "this note has tags") + + new_notes = list(models.Note.objects.all()) + + # Find the new tag and extract its tags + old_pks = [n.pk for n in old_notes] + new_note = [n for n in new_notes if n.pk not in old_pks][0] + new_note_tag_names = [t.name for t in new_note.tags.all()] + + self.assertSetEqual(set(new_note_tag_names), {"A", "B", "setup tag"}) + + def test_edit(self): + old_tag_last_used = self.oldtag.last_used + + params = { + "child": self.note.child.id, + "note": "Edited note", + "time": self.localdate_string(), + "tags": "oldtag,newtag", + } + page = self.c.post("/notes/{}/".format(self.note.id), params, follow=True) + self.assertEqual(page.status_code, 200) + + self.note.refresh_from_db() + self.oldtag.refresh_from_db() + self.assertEqual(self.note.note, params["note"]) + self.assertContains( + page, "Note entry for {} updated".format(str(self.note.child)) + ) + + self.assertSetEqual( + set(t.name for t in self.note.tags.all()), {"oldtag", "newtag"} + ) + + # Old tag remains old, because it was not added + self.assertEqual(old_tag_last_used, self.oldtag.last_used) + + # Second phase: Remove all tags then add "oldtag" through posting + # which should update the last_used tag + self.note.tags.clear() + self.note.save() + + params = { + "child": self.note.child.id, + "note": "Edited note (2)", + "time": self.localdate_string(), + "tags": "oldtag", + } + page = self.c.post("/notes/{}/".format(self.note.id), params, follow=True) + self.assertEqual(page.status_code, 200) + + self.note.refresh_from_db() + self.oldtag.refresh_from_db() + + self.assertLess(old_tag_last_used, self.oldtag.last_used) + + def test_delete(self): + page = self.c.post("/notes/{}/delete/".format(self.note.id), follow=True) + self.assertEqual(page.status_code, 200) + self.assertContains(page, "Note entry deleted") + + class TemperatureFormsTestCase(FormsTestCaseBase): @classmethod def setUpClass(cls):