summaryrefslogtreecommitdiffstats
path: root/engagements/tests/test_views.py
diff options
context:
space:
mode:
Diffstat (limited to 'engagements/tests/test_views.py')
-rw-r--r--engagements/tests/test_views.py177
1 files changed, 95 insertions, 82 deletions
diff --git a/engagements/tests/test_views.py b/engagements/tests/test_views.py
index f25eb3a..017b96f 100644
--- a/engagements/tests/test_views.py
+++ b/engagements/tests/test_views.py
@@ -1,91 +1,104 @@
import datetime
from http import HTTPStatus
-from django.test import RequestFactory, TestCase
+import pytest
+from django.test import RequestFactory
from django.urls import reverse
from engagements import models, views
+from engagements.models import EngagementStrategy, RegulatoryCycle, Organisation
from engagements.utils import populate_database
+pytestmark = pytest.mark.django_db
-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)
+
+@pytest.fixture
+def test_data():
+ return populate_database()
+
+
+@pytest.fixture
+def request_factory():
+ return RequestFactory()
+
+
+def test_dscs_for_ep(client, test_data, request_factory):
+ org = test_data["orgs"][0]
+ et = models.EngagementType.objects.get(name="INSPECTION")
+ si = test_data["sub_instruments"][0]
+ si2 = test_data["sub_instruments"][2]
+ si3 = test_data["sub_instruments"][3]
+
+ 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, si2, si3)
+
+ url = reverse("engagements:plan_for_org", kwargs={"orgslug": org.slug})
+ client.force_login(test_data["superuser"])
+ response = client.get(url)
+
+ assert response.status_code == HTTPStatus.OK
+ assert response.context["entity"]
+ assert response.context["entity"].name == org.name
+ assert si in response.context["dscs"]
+ assert si2 in response.context["dscs"]
+ assert si3 in response.context["dscs"]
+
+
+def test_get_blank_form(client, test_data, request_factory):
+ url = reverse("engagements:effort_create", kwargs={"eid": 1, "etype": "PLANNING"})
+ client.force_login(test_data["superuser"])
+ request = request_factory.get(url)
+ request.user = test_data["superuser"]
+ response = views.engagement_effort_create(request, eid=1, etype="PLANNING")
+ assert response.status_code == HTTPStatus.OK
+
+
+# def test_get_form_to_create_engagement_strategy(client, request_factory):
+# url = reverse("engagements:es-create")
+# client.force_login(test_data["superuser"])
+# request = request_factory.get(url)
+# request.user = test_data["superuser"]
+# response = views.CreateEngagementStrategy()
+# assert response.status_code == HTTPStatus.OK
+
+
+def test_create_engagement_strategy(client, user, org, regulatory_cycles):
+ # Define the URL for the create view
+ url = reverse("engagements:es-create")
+ client.force_login(user)
+ mod = Organisation.objects.create(name="MOD", is_regulated_entity=False)
+
+ sy = RegulatoryCycle.objects.get(start_date__year="2022")
+ ey = RegulatoryCycle.objects.get(start_date__year="2024")
+
+ # Define sample data for the form
+ data = {
+ "organisation": mod.pk,
+ "start_year": sy.pk, # Use the pk of the regulatory cycle instead of the object itself
+ "end_year": ey.pk,
+ "description": "Example description",
+ "inspector_sign_off": "2022-01-10",
+ "owned_by": user.pk, # Same here
+ "reviewed_by": user.pk,
+ "management_sign_off": "2022-02-10",
+ "status": "DRAFT",
+ }
+
+ # Send a POST request to the view
+ response = client.post(url, data)
+
+ # Check that the response redirects (status code 302) after successful creation
+ assert response.status_code == 302
+ assert EngagementStrategy.objects.count() == 1