From 44648ce33b1a7024458fa94ef814df5177440d92 Mon Sep 17 00:00:00 2001 From: bugbountyguy Date: Wed, 2 Mar 2022 21:00:06 -0600 Subject: [PATCH] add tests --- api/tests.py | 46 +++++++++++++++++++++++++++++++ core/tests/import/breastpump.csv | 24 ++++++++++++++++ core/tests/tests_forms.py | 43 +++++++++++++++++++++++++++++ core/tests/tests_import_export.py | 3 ++ core/tests/tests_models.py | 18 ++++++++++++ core/tests/tests_views.py | 12 ++++++++ reports/tests/tests_views.py | 3 ++ 7 files changed, 149 insertions(+) create mode 100644 core/tests/import/breastpump.csv diff --git a/api/tests.py b/api/tests.py index 29963d9e..08ade7e8 100644 --- a/api/tests.py +++ b/api/tests.py @@ -129,6 +129,52 @@ class ChildAPITestCase(TestBase.BabyBuddyAPITestCaseBase): self.assertEqual(response.data, entry) +class BreastpumpAPITestCase(TestBase.BabyBuddyAPITestCaseBase): + endpoint = reverse("api:breastpump-list") + model = models.Breastpump + + def test_get(self): + response = self.client.get(self.endpoint) + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual( + response.data["results"][0], + { + "id": 1, + "child": 1, + "amount": 50.0, + "time": "2017-11-17T12:52:00-05:00", + "notes": "new device", + }, + ) + + def test_post(self): + data = { + "child": 1, + "amount": "21", + "time": "2017-11-20T22:52:00-05:00", + "notes": "old device", + } + response = self.client.post(self.endpoint, data, format="json") + self.assertEqual(response.status_code, status.HTTP_201_CREATED) + obj = models.Breastpump.objects.get(pk=response.data["id"]) + self.assertEqual(str(obj.amount), data["amount"]) + self.assertEqual(obj.notes, data["notes"]) + + def test_patch(self): + endpoint = "{}{}/".format(self.endpoint, 1) + response = self.client.get(endpoint) + entry = response.data + entry["amount"] = 41 + response = self.client.patch( + endpoint, + { + "amount": entry["amount"], + }, + ) + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(response.data, entry) + + class DiaperChangeAPITestCase(TestBase.BabyBuddyAPITestCaseBase): endpoint = reverse("api:diaperchange-list") model = models.DiaperChange diff --git a/core/tests/import/breastpump.csv b/core/tests/import/breastpump.csv new file mode 100644 index 00000000..cc2e3033 --- /dev/null +++ b/core/tests/import/breastpump.csv @@ -0,0 +1,24 @@ +child_id,amount,time +1,100.2,2020-02-17 09:31:49 +1,99.62,2020-02-17 04:11:09 +1,101.45,2020-02-17 00:14:09 +1,97.41,2020-02-16 14:42:52 +1,99.53,2020-02-16 11:28:41 +1,98.92,2020-02-16 07:38:01 +1,97.69,2020-02-15 22:53:01 +1,98.37,2020-02-15 17:37:21 +1,101.73,2020-02-15 07:51:45 +1,98.31,2020-02-15 03:06:24 +1,95.31,2020-02-14 12:12:28 +1,99.5,2020-02-14 07:41:40 +1,99.9,2020-02-13 18:05:46 +1,98.33,2020-02-13 14:15:54 +1,98.78,2020-02-13 09:39:03 +1,98.51,2020-02-12 18:17:05 +1,100.76,2020-02-12 05:54:01 +1,98.96,2020-02-11 21:00:14 +1,95.15,2020-02-11 17:33:04 +1,95.89,2020-02-11 14:51:04 +1,98.57,2020-02-11 08:30:34 +1,99.16,2020-02-11 06:35:34 +1,98.37,2020-02-10 10:25:06 diff --git a/core/tests/tests_forms.py b/core/tests/tests_forms.py index 84c2ce7c..9cd08423 100644 --- a/core/tests/tests_forms.py +++ b/core/tests/tests_forms.py @@ -194,6 +194,49 @@ class ChildFormsTestCase(FormsTestCaseBase): self.assertContains(page, "Child entry deleted") +class BreastpumpFormsTestCase(FormsTestCaseBase): + @classmethod + def setUpClass(cls): + super(BreastpumpFormsTestCase, cls).setUpClass() + cls.bp = models.Breastpump.objects.create( + child=cls.child, + amount=50.0, + time=timezone.localtime() - timezone.timedelta(days=1), + ) + + def test_add(self): + params = { + "child": self.child.id, + "amount": "50.0", + "time": self.localtime_string(), + } + + page = self.c.post("/breastpump/add/", params, follow=True) + self.assertEqual(page.status_code, 200) + self.assertContains( + page, "Breastpump entry for {} added".format(str(self.child)) + ) + + def test_edit(self): + params = { + "child": self.bp.child.id, + "amount": self.bp.amount + 2, + "time": self.localtime_string(), + } + page = self.c.post("/breastpump/{}/".format(self.bp.id), params, follow=True) + self.assertEqual(page.status_code, 200) + self.bp.refresh_from_db() + self.assertEqual(self.bp.amount, params["amount"]) + self.assertContains( + page, "Breastpump entry for {} updated".format(str(self.bp.child)) + ) + + def test_delete(self): + page = self.c.post("/breastpump/{}/delete/".format(self.bp.id), follow=True) + self.assertEqual(page.status_code, 200) + self.assertContains(page, "Breastpump entry deleted") + + class DiaperChangeFormsTestCase(FormsTestCaseBase): @classmethod def setUpClass(cls): diff --git a/core/tests/tests_import_export.py b/core/tests/tests_import_export.py index eb9396a5..cbbe4549 100644 --- a/core/tests/tests_import_export.py +++ b/core/tests/tests_import_export.py @@ -41,6 +41,9 @@ class ImportTestCase(TestCase): def test_child(self): self.import_data(models.Child, 2) + def test_breastpump(self): + self.import_data(models.Breastpump, 23) + def test_diaperchange(self): self.import_data(models.DiaperChange, 75) diff --git a/core/tests/tests_models.py b/core/tests/tests_models.py index c10cf54d..237f3607 100644 --- a/core/tests/tests_models.py +++ b/core/tests/tests_models.py @@ -35,6 +35,24 @@ class ChildTestCase(TestCase): self.assertEqual(models.Child.count(), 1) +class BreastpumpTestCase(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.temp = models.Breastpump.objects.create( + child=self.child, + time=timezone.localtime() - timezone.timedelta(days=1), + amount=98.6, + ) + + def test_breastpump_create(self): + self.assertEqual(self.temp, models.Breastpump.objects.first()) + self.assertEqual(str(self.temp), "Breastpump") + self.assertEqual(self.temp.amount, 98.6) + + class DiaperChangeTestCase(TestCase): def setUp(self): call_command("migrate", verbosity=0) diff --git a/core/tests/tests_views.py b/core/tests/tests_views.py index b2a2d1a4..87243aaa 100644 --- a/core/tests/tests_views.py +++ b/core/tests/tests_views.py @@ -49,6 +49,18 @@ class ViewsTestCase(TestCase): page = self.c.get("/children/{}/delete/".format(entry.slug)) self.assertEqual(page.status_code, 200) + def test_breastpump_views(self): + page = self.c.get("/breastpump/") + self.assertEqual(page.status_code, 200) + page = self.c.get("/breastpump/add/") + self.assertEqual(page.status_code, 200) + + entry = models.Breastpump.objects.first() + page = self.c.get("/breastpump/{}/".format(entry.id)) + self.assertEqual(page.status_code, 200) + page = self.c.get("/breastpump/{}/delete/".format(entry.id)) + self.assertEqual(page.status_code, 200) + def test_diaperchange_views(self): page = self.c.get("/changes/") self.assertEqual(page.status_code, 200) diff --git a/reports/tests/tests_views.py b/reports/tests/tests_views.py index 1aed4a53..00981897 100644 --- a/reports/tests/tests_views.py +++ b/reports/tests/tests_views.py @@ -35,6 +35,9 @@ class ViewsTestCase(TestCase): page = self.c.get(base_url) self.assertEqual(page.status_code, 200) + page = self.c.get("{}/breastpump/amounts/".format(base_url)) + self.assertEqual(page.status_code, 200) + page = self.c.get("{}/changes/amounts/".format(base_url)) self.assertEqual(page.status_code, 200) page = self.c.get("{}/changes/lifetimes/".format(base_url))