diff options
author | Matthew Lemon <y@yulqen.org> | 2024-09-10 15:36:13 +0100 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-09-10 15:36:13 +0100 |
commit | 15520388a725dd9763e80ad617f31aea3b2ddb67 (patch) | |
tree | d28670b8abfc405f6e3541805e59b588735696f3 | |
parent | b87f2e3410cf3445a4eeb1ca16d04cc7de71c330 (diff) |
wip: testing the view containing the ES create form
-rw-r--r-- | engagements/templates/engagements/engagement_strategy_form.html | 17 | ||||
-rw-r--r-- | engagements/tests/test_views.py | 51 | ||||
-rw-r--r-- | engagements/urls.py | 7 | ||||
-rw-r--r-- | engagements/views.py | 17 |
4 files changed, 84 insertions, 8 deletions
diff --git a/engagements/templates/engagements/engagement_strategy_form.html b/engagements/templates/engagements/engagement_strategy_form.html new file mode 100644 index 0000000..6b2f26c --- /dev/null +++ b/engagements/templates/engagements/engagement_strategy_form.html @@ -0,0 +1,17 @@ +{% extends "core/base.html" %}} + +{% block content %} + +<h1>Create Engagement Strategy</h1> + +<div class="row w-full"> + <div> + <form target="{% url "engagements:es-create" %}" method="post"> + {% csrf_token %} + {{ form.as_p }} + <button type="submit">Create</button> + </form> + </div> +</div> + +{% endblock content %} diff --git a/engagements/tests/test_views.py b/engagements/tests/test_views.py index 233ac7e..7a65ea0 100644 --- a/engagements/tests/test_views.py +++ b/engagements/tests/test_views.py @@ -6,6 +6,7 @@ from django.test import RequestFactory from django.urls import reverse from engagements import models, views +from engagements.models import EngagementStrategy, RegulatoryCycle from engagements.utils import populate_database pytestmark = pytest.mark.django_db @@ -63,3 +64,53 @@ def test_get_blank_form(client, test_data, request_factory): 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) + + sy = RegulatoryCycle.objects.get(start_date__year="2022") + ey = RegulatoryCycle.objects.get(start_date__year="2024") + + # Define sample data for the form + data = { + "organisation": org, + "start_year": sy, + "end_year": ey, + "description": "Example description", + "inspector_sign_off": "2022-01-10", + "owned_by": user, + "reviewed_by": user, + "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 == 200 + + # Check that a new EngagementStrategy object was created in the database + assert EngagementStrategy.objects.filter(organisation__name="MOD").exists() + + +# def test_create_view_uses_correct_form(self): +# # Define the URL for the create view +# url = reverse("engagement_strategy_create") +# +# # Send a GET request to the view +# response = self.client.get(url) +# +# # Check that the response uses the correct form class +# self.assertIsInstance(response.context["form"], EngagementStrategyCreateForm) diff --git a/engagements/urls.py b/engagements/urls.py index ef61f84..a199b34 100644 --- a/engagements/urls.py +++ b/engagements/urls.py @@ -13,9 +13,10 @@ urlpatterns = [ name="regulatedentities", ), path("edit/<int:eid>", views.engagement_edit, name="edit"), - path('effort/<int:effort_id>/detail/', views.effort_detail, name='effort_detail'), + path("effort/<int:effort_id>/detail/", views.effort_detail, name="effort_detail"), path("create/<slug:slug>/", views.engagement_create, name="create"), path("create/<slug:slug>/<str:reg>", views.engagement_create, name="create"), + path("es-create/", views.CreateEngagementStrategy.as_view(), name="es-create"), path( "effort/create/<int:eid>/<str:etype>", views.engagement_effort_create, @@ -23,8 +24,6 @@ urlpatterns = [ ), ] -htmx_urls = [ - path("htmx-effort-planned/<int:effid>", views.htmx_effort_planned, name="htmx-effort-planned") -] +htmx_urls = [path("htmx-effort-planned/<int:effid>", views.htmx_effort_planned, name="htmx-effort-planned")] urlpatterns = urlpatterns + htmx_urls diff --git a/engagements/views.py b/engagements/views.py index e66d679..2257a98 100644 --- a/engagements/views.py +++ b/engagements/views.py @@ -4,7 +4,8 @@ from django.db.models import Q from django.http import HttpResponse from django.shortcuts import get_object_or_404, redirect, render from django.template.loader import render_to_string -from django.views.generic import ListView +from django.urls import reverse_lazy +from django.views.generic import CreateView, ListView from instruments.models import SubInstrument @@ -14,13 +15,14 @@ from .forms import ( EngagementEffortRegulationCreateForm, EngagementEffortReportingCreateForm, EngagementEffortTravelCreateForm, + EngagementStrategyCreateForm, ) -from .models import Engagement, EngagementEffort, EngagementType, Organisation +from .models import Engagement, EngagementEffort, EngagementStrategy, EngagementType, Organisation def effort_detail(request, effort_id): effort = get_object_or_404(EngagementEffort, id=effort_id) - html = render_to_string('engagements/snippets/effort_detail.html', {'effort': effort}) + html = render_to_string("engagements/snippets/effort_detail.html", {"effort": effort}) return HttpResponse(html) @@ -48,7 +50,7 @@ def htmx_effort_planned(request, effid): else: effort.is_planned = True effort.save() - return render(request, "engagements/snippets/effort_summary_panel.html", {"e" : effort}) + return render(request, "engagements/snippets/effort_summary_panel.html", {"e": effort}) @login_required @@ -194,3 +196,10 @@ def engagement_create(request, slug, reg=None): "engagements/engagement_form.html", {"form": form, "title": "Add New Engagement"}, ) + + +class CreateEngagementStrategy(LoginRequiredMixin, CreateView): + model = EngagementStrategy + form_class = EngagementStrategyCreateForm + template_name = "engagements/engagement_strategy_form.html" + success_url = reverse_lazy("engagements:home") |