diff options
author | Matthew Lemon <y@yulqen.org> | 2024-09-09 14:55:38 +0100 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-09-09 14:55:38 +0100 |
commit | 09b099e467df64ac8ce44bbe2d1f80cd1c00a126 (patch) | |
tree | b4f4110337cde4d0586c3152d42ce9b5133faf53 /engagements/tests/test_forms.py | |
parent | e57e4a03bbe67d3801656ceb5c2f8249dba6456d (diff) |
Basic validation on start and end dates for Engagement
- form validation when end date is before the start date
Diffstat (limited to 'engagements/tests/test_forms.py')
-rw-r--r-- | engagements/tests/test_forms.py | 148 |
1 files changed, 108 insertions, 40 deletions
diff --git a/engagements/tests/test_forms.py b/engagements/tests/test_forms.py index b6aab9a..3c213c5 100644 --- a/engagements/tests/test_forms.py +++ b/engagements/tests/test_forms.py @@ -1,45 +1,113 @@ -from django.test import TestCase +import pytest +from django.forms import DateInput, Select, SelectMultiple +from engagements.forms import EngagementCreateForm from engagements.forms import EngagementEffortCreateForm -from engagements.models import Engagement, EngagementType, Organisation -from myuser.models import TeamUser +from engagements.models import EngagementType +pytestmark = pytest.mark.django_db -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"), +def test_basic_validation(engagement, user): + form = EngagementEffortCreateForm( + data={ + "is_planned": True, + "proposed_start_date": "2022-10-10 10:00", + "proposed_end_date": "2022-10-10 12:00", + "engagement": engagement, + "effort_type": "PLANNING", + "officers": [user], } - 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"]) + ) + assert not form.errors + + +def test_basic_validation_on_bad_entry(engagement, user): + form = EngagementEffortCreateForm( + data={ + "is_planned": True, + "proposed_start_date": "20240-10-10 10:00", + "proposed_end_date": "2022-10-10 12:00", + "engagement": engagement, + "effort_type": "bobbins", + "officers": [user], + "sub_instruments": [""], + } + ) + assert "effort_type" in form.errors + assert "proposed_start_date" in form.errors + + +def test_form_fields(): + form = EngagementCreateForm() + expected_fields = [ + "proposed_start_date", + "proposed_end_date", + "engagement_type", + "external_party", + "officers", + ] + assert list(form.fields.keys()) == expected_fields + + +def test_form_labels(): + form = EngagementCreateForm() + assert form.fields["officers"].label == "Inspectors" + + +def test_form_help_texts(): + form = EngagementCreateForm() + assert form.fields["proposed_start_date"].help_text == "<small><em>YYYY-MM-DD</em></small>" + assert form.fields["proposed_end_date"].help_text == "<small><em>YYYY-MM-DD</em></small>" + + +def test_form_widgets(): + form = EngagementCreateForm() + assert isinstance(form.fields["proposed_start_date"].widget, DateInput) + assert isinstance(form.fields["proposed_end_date"].widget, DateInput) + assert isinstance(form.fields["engagement_type"].widget, Select) + assert isinstance(form.fields["officers"].widget, SelectMultiple) + + +def test_form_valid_data(engagement_type, external_party, user1, user2): + form_data = { + "proposed_start_date": "2023-01-01", + "proposed_end_date": "2023-01-31", + "engagement_type": engagement_type.id, + "external_party": external_party.id, + "officers": [user1.id, user2.id], + } + form = EngagementCreateForm(data=form_data) + assert form.is_valid() + + +def test_form_invalid_dates(engagement_type, external_party, user1): + form_data = { + "proposed_start_date": "2023-01-31", + "proposed_end_date": "2023-01-01", + "engagement_type": engagement_type.id, + "external_party": external_party.id, + "officers": [user1.id], + } + form = EngagementCreateForm(data=form_data) + assert not form.is_valid() + + +def test_form_missing_required_fields(): + form_data = { + "proposed_start_date": "2023-01-01", + } + form = EngagementCreateForm(data=form_data) + assert not form.is_valid() + assert "engagement_type" in form.errors + assert "external_party" in form.errors + + +def test_form_engagement_type_queryset(): + initial_data = {"engagement_type": EngagementType.objects.all()} + form = EngagementCreateForm(initial=initial_data) + assert form.fields["engagement_type"].queryset == initial_data["engagement_type"] + + +def test_form_engagement_type_queryset_without_initial(): + form = EngagementCreateForm() + assert list(form.fields["engagement_type"].queryset) == list(EngagementType.objects.all()) |