diff options
author | Matthew Lemon <y@yulqen.org> | 2024-09-09 12:34:03 +0100 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-09-09 12:39:52 +0100 |
commit | e57e4a03bbe67d3801656ceb5c2f8249dba6456d (patch) | |
tree | acb2b7efb8c4f6e1d6e8f08e3a429f2930137d57 | |
parent | 9da1c87f0dfd5c4519334a50afc83853a32d6a88 (diff) |
Converted to pytest-style test
-rw-r--r-- | engagements/tests/test_models.py | 52 | ||||
-rw-r--r-- | engagements/tests/test_views.py | 123 | ||||
-rw-r--r-- | instruments/tests/test_models.py | 37 |
3 files changed, 94 insertions, 118 deletions
diff --git a/engagements/tests/test_models.py b/engagements/tests/test_models.py index f5afbb8..73738db 100644 --- a/engagements/tests/test_models.py +++ b/engagements/tests/test_models.py @@ -1,30 +1,32 @@ 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 - - @pytest.mark.skip(reason="Not implemented until I get my head round effort calculations") - def test_total_effort_for_engagement(self): - e = self.data["engagements"][0] - assert e.total_effort() == 5.25 +@pytest.fixture +@pytest.mark.django_db +def data(): + return populate_database() + + +@pytest.mark.django_db +def test_check_all_dcs(data): + dscs = data.get("sub_instruments") + assert dscs[0].title == "DSC 1 - Title 1" + + +@pytest.mark.django_db +def test_effort_by_type(data): + e = 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 + + +@pytest.mark.skip(reason="Not implemented until I get my head round effort calculations") +def test_total_effort_for_engagement(data): + e = data["engagements"][0] + assert e.total_effort() == 5.25 diff --git a/engagements/tests/test_views.py b/engagements/tests/test_views.py index f25eb3a..4899c58 100644 --- a/engagements/tests/test_views.py +++ b/engagements/tests/test_views.py @@ -1,91 +1,64 @@ 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.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() +@pytest.fixture +@pytest.mark.django_db +def test_data(): + return 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. +@pytest.fixture +def request_factory(): + return RequestFactory() - 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) +@pytest.mark.django_db +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] -class TestEngagementEffortView(TestCase): - @classmethod - def setUpTestData(cls): - cls.request = RequestFactory() # for use in _ep_request_factory test - cls.data = populate_database() + 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) - 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) + url = reverse("engagements:plan_for_org", kwargs={"orgslug": org.slug}) + client.force_login(test_data["superuser"]) + response = client.get(url) - # 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) + 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"] + +@pytest.mark.django_db +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
\ No newline at end of file diff --git a/instruments/tests/test_models.py b/instruments/tests/test_models.py index 0d7eafa..6346cba 100644 --- a/instruments/tests/test_models.py +++ b/instruments/tests/test_models.py @@ -1,29 +1,30 @@ import datetime import pytest -from django.test import TestCase from engagements import models from engagements.utils import populate_database -class TestModels(TestCase): - @classmethod - def setUpTestData(cls): - cls.data = populate_database() +@pytest.fixture +@pytest.mark.django_db +def test_data(): + return populate_database() - 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_get_hours_of_effort_for_dsc_for_org(self): - org = self.data["orgs"][0] +@pytest.mark.django_db +class TestModels: + def test_check_all_dcs(self, test_data): + dscs = test_data.get("sub_instruments") + assert dscs[0].title == "DSC 1 - Title 1" + + def test_get_hours_of_effort_for_dsc_for_org(self, test_data): + org = test_data["orgs"][0] 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] + si = test_data["sub_instruments"][0] + si2 = test_data["sub_instruments"][2] + si3 = test_data["sub_instruments"][3] + # si_not = test_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), @@ -41,6 +42,6 @@ class TestModels(TestCase): ef1.sub_instruments.add(si2) # DSC 3 ef1.sub_instruments.add(si3) # DSC 4 ef1.save() - self.assertEqual(si.effort_for_org(org), 2) - self.assertEqual(si2.effort_for_org(org), 2) - self.assertEqual(si3.effort_for_org(org), 2) + assert si.effort_for_org(org) == 2 + assert si2.effort_for_org(org) == 2 + assert si3.effort_for_org(org) == 2 |