summaryrefslogtreecommitdiffstats
path: root/engagements/tests/test_forms.py
blob: deb0a29662ec8c161fdab565f7816981d3afcca3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import pytest
from django.forms import DateInput, Select, SelectMultiple

from engagements.forms import EngagementCreateForm, EngagementEffortCreateForm, EngagementStrategyCreateForm
from engagements.models import EngagementType, RegulatoryCycle

pytestmark = pytest.mark.django_db


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],
        }
    )
    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 == "YYYY-MM-DD"
    assert form.fields["proposed_end_date"].help_text == "YYYY-MM-DD"


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(engagement_type):
    initial_data = {"engagement_type": EngagementType.objects.all()}
    form = EngagementCreateForm(initial=initial_data)
    assert form.fields["engagement_type"].queryset.first().name == initial_data["engagement_type"].first().name


def test_form_engagement_type_queryset_without_initial():
    form = EngagementCreateForm()
    assert list(form.fields["engagement_type"].queryset) == list(EngagementType.objects.all())


def test_create_engagement_strategy_form(org, user, regulatory_cycles):
    sy = RegulatoryCycle.objects.get(start_date="2022-01-01")
    ey = RegulatoryCycle.objects.get(start_date="2024-01-01")
    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",
    }
    form = EngagementStrategyCreateForm(data=form_data)
    assert form.is_valid()