summaryrefslogtreecommitdiffstats
path: root/engagements/tests
diff options
context:
space:
mode:
Diffstat (limited to 'engagements/tests')
-rw-r--r--engagements/tests/__init__.py0
-rw-r--r--engagements/tests/test_forms.py45
-rw-r--r--engagements/tests/test_models.py30
-rw-r--r--engagements/tests/test_views.py91
4 files changed, 166 insertions, 0 deletions
diff --git a/engagements/tests/__init__.py b/engagements/tests/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/engagements/tests/__init__.py
diff --git a/engagements/tests/test_forms.py b/engagements/tests/test_forms.py
new file mode 100644
index 0000000..b6aab9a
--- /dev/null
+++ b/engagements/tests/test_forms.py
@@ -0,0 +1,45 @@
+from django.test import TestCase
+
+from engagements.forms import EngagementEffortCreateForm
+from engagements.models import Engagement, EngagementType, Organisation
+from myuser.models import TeamUser
+
+
+class EngagementEffortCreate(TestCase):
+ def setUp(self):
+ data = {
+ "proposed_start_date": "2022-10-01",
+ "engagement_type": EngagementType.objects.create(name="ET1"),
+ "external_party": Organisation.objects.create(name="O1"),
+ }
+ self.e = Engagement.objects.create(**data)
+ self.user = TeamUser.objects.create_user(email="ming@ming.com")
+
+ def test_basic_validation(self):
+ form = EngagementEffortCreateForm(
+ data={
+ "is_planned": True,
+ "proposed_start_date": "2022-10-10 10:00",
+ "proposed_end_date": "2022-10-10 12:00",
+ "engagement": self.e,
+ "effort_type": "PLANNING",
+ "officers": [self.user],
+ }
+ )
+ self.assertFalse(form.errors)
+
+ def test_basic_validation_on_bad_entry(self):
+ form = EngagementEffortCreateForm(
+ data={
+ "is_planned": True,
+ "proposed_start_date": "20240-10-10 10:00",
+ "proposed_end_date": "2022-10-10 12:00",
+ "engagement": self.e,
+ "effort_type": "bobbins",
+ "officers": [self.user],
+ "sub_instruments": [""],
+ }
+ )
+ self.assertTrue(form.errors["effort_type"])
+ self.assertTrue(form.errors["proposed_start_date"])
+ self.assertTrue(form.errors["sub_instruments"])
diff --git a/engagements/tests/test_models.py b/engagements/tests/test_models.py
new file mode 100644
index 0000000..08c5169
--- /dev/null
+++ b/engagements/tests/test_models.py
@@ -0,0 +1,30 @@
+import pytest
+from django.test import TestCase
+
+from engagements.utils import populate_database
+
+
+class TestModels(TestCase):
+ @classmethod
+ def setUpTestData(cls):
+ cls.data = populate_database()
+
+ @pytest.mark.django_db
+ def test_check_all_dcs(self):
+ dscs = self.data.get("sub_instruments")
+ self.assertEqual(dscs[0].title, "DSC 1 - Title 1")
+
+ @pytest.mark.django_db
+ def test_effort_by_type(self):
+ e = self.data["engagements"][0]
+ total_planning = sum([x.effort_total_planned_hours() for x in e.effort.filter(effort_type="PLANNING")])
+ total_travel = sum([x.effort_total_planned_hours() for x in e.effort.filter(effort_type="TRAVEL")])
+ total_regulation = sum([x.effort_total_planned_hours() for x in e.effort.filter(effort_type="REGULATION")])
+ assert total_planning == 4.25
+ assert total_regulation == 0
+ assert total_travel == 1
+
+ # TODO finish this test!
+ def test_total_effort_for_engagement(self):
+ e = self.data["engagements"][0]
+ assert e.total_effort() == 5.25
diff --git a/engagements/tests/test_views.py b/engagements/tests/test_views.py
new file mode 100644
index 0000000..f25eb3a
--- /dev/null
+++ b/engagements/tests/test_views.py
@@ -0,0 +1,91 @@
+import datetime
+from http import HTTPStatus
+
+from django.test import RequestFactory, TestCase
+from django.urls import reverse
+
+from engagements import models, views
+from engagements.utils import populate_database
+
+
+class TestModels(TestCase):
+ @classmethod
+ def setUpTestData(cls):
+ cls.request = RequestFactory() # for use in _ep_request_factory test
+ cls.data = populate_database()
+
+ def test_dscs_for_ep(self):
+ org = self.data["orgs"][0]
+ # we set up an engagement and effort for this org
+ et = models.EngagementType.objects.get(name="INSPECTION")
+ si = self.data["sub_instruments"][0]
+ si2 = self.data["sub_instruments"][2]
+ si3 = self.data["sub_instruments"][3]
+ # si_not = self.data["sub_instruments"][1]
+ engagement = models.Engagement.objects.create(
+ proposed_start_date=datetime.date(2022, 10, 10),
+ proposed_end_date=datetime.date(2022, 10, 10),
+ engagement_type=et,
+ external_party=org,
+ )
+ ef1 = models.EngagementEffort.objects.create(
+ is_planned=True,
+ effort_type="REGULATION",
+ proposed_start_date=datetime.date(2022, 10, 10),
+ proposed_end_date=datetime.date(2022, 10, 10),
+ engagement=engagement,
+ )
+ ef1.sub_instruments.add(si) # DSC 1
+ ef1.sub_instruments.add(si2) # DSC 3
+ ef1.sub_instruments.add(si3) # DSC 4
+ ef1.save()
+ url = reverse("engagements:plan_for_org", kwargs={"orgslug": org.slug})
+ self.client.force_login(self.data["superuser"])
+ response = self.client.get(url)
+ self.assertEqual(response.status_code, HTTPStatus.OK)
+ self.assertTrue(response.context["entity"])
+ self.assertEqual(response.context["entity"].name, org.name)
+ self.assertIn(si, response.context["dscs"])
+ self.assertIn(si2, response.context["dscs"])
+ self.assertIn(si3, response.context["dscs"])
+ self.assertEqual(response.context["dscs"].count(), 3)
+ # self.assertNotIn(si_not, response.context["dscs"])
+
+ def test_dscs_for_ep_request_factory(self):
+ """
+ On the EP page, we expect to see a list of all DSCs related to effort
+ for this organisation.
+
+ Included this here for reference
+ """
+ org = self.data["orgs"][0]
+ url = reverse("engagements:plan_for_org", kwargs={"orgslug": org.slug})
+ request = self.request.get(url)
+ request.user = self.data["superuser"]
+ response = views.engagement_plan_for(request, org.slug)
+ self.assertEqual(response.status_code, HTTPStatus.OK)
+
+
+class TestEngagementEffortView(TestCase):
+ @classmethod
+ def setUpTestData(cls):
+ cls.request = RequestFactory() # for use in _ep_request_factory test
+ cls.data = populate_database()
+
+ def test_get_blank_form(self):
+ url = reverse("engagements:effort_create", kwargs={"eid": 1, "etype": "PLANNING"})
+ self.client.force_login(self.data["superuser"])
+ request = self.request.get(url)
+ request.user = self.data["superuser"]
+ response = views.engagement_effort_create(request, eid=1, etype="PLANNING")
+ self.assertEqual(response.status_code, HTTPStatus.OK)
+
+ # def test_post_data(self):
+ # url = reverse(
+ # "engagements:effort_create", kwargs={"eid": 1, "etype": "PLANNING"}
+ # )
+ # self.client.force_login(self.data["superuser"])
+ # request = self.request.post(url, {"proposed_start_date": "toss"})
+ # request.user = self.data["superuser"]
+ # response = views.engagement_effort_create(request, eid=1, etype="PLANNING")
+ # self.assertEqual(response.status_code, HTTPStatus.OK)