summaryrefslogblamecommitdiffstats
path: root/engagements/tests/test_forms.py
blob: deb0a29662ec8c161fdab565f7816981d3afcca3 (plain) (tree)
1
2
3
4
5
6
7
8
9

                                                          
 

                                                                                                            
 
                                  
 
 








                                                      
         






































                                                         

                                                                       











































                                                                           
                                                        

                                                                    
                                                                                                               




                                                                                              

















                                                                       
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()