From c64bb5a27abc47fe0103b5772e37c3c9818820d9 Mon Sep 17 00:00:00 2001 From: MR Lemon Date: Sun, 26 Apr 2020 20:59:47 +0100 Subject: testing new create Address form --- ctrack/organisations/forms.py | 19 ++++++++++++- ctrack/organisations/tests/test_forms.py | 47 ++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 ctrack/organisations/tests/test_forms.py (limited to 'ctrack/organisations') diff --git a/ctrack/organisations/forms.py b/ctrack/organisations/forms.py index 4234a07..d4baf64 100644 --- a/ctrack/organisations/forms.py +++ b/ctrack/organisations/forms.py @@ -3,7 +3,7 @@ from crispy_forms.layout import Layout, Fieldset, ButtonHolder, Submit, Button, from django import forms from django.urls import reverse -from ctrack.organisations.models import Organisation +from ctrack.organisations.models import Organisation, Address class OrganisationCreateForm(forms.ModelForm): @@ -44,3 +44,20 @@ class OrganisationCreateForm(forms.ModelForm): "active": "Is this company an active participant in the NIS compliance regime?", "designation_type": "This is probably defined in the Reguation", } + + +class AddressCreateForm(forms.ModelForm): + class Meta: + model = Address + fields = ('type', 'line1', 'line2', 'line3', 'city', 'county', 'postcode', + 'country', 'other_details') + + def __init__(self, *args, **kwargs): + self.org = kwargs.pop("org") + super().__init__(*args, **kwargs) + + def save(self): + address = super().save(commit=False) + address.organisation = self.org + address.organisation.save() + return address diff --git a/ctrack/organisations/tests/test_forms.py b/ctrack/organisations/tests/test_forms.py new file mode 100644 index 0000000..b742d10 --- /dev/null +++ b/ctrack/organisations/tests/test_forms.py @@ -0,0 +1,47 @@ +import pytest + +from ctrack.organisations.forms import AddressCreateForm +from ctrack.organisations.models import AddressType + +pytestmark = pytest.mark.django_db + + +# https://test-driven-django-development.readthedocs.io/en/latest/05-forms.html +# is instructive + +# Can the form accept an org_id? We need this. +def test_add_new_address_for_organisation_form(org): + AddressCreateForm(org=org) + + +# Will our form raise an exception if the org_id isn't specified? +def test_add_new_address_init_without_org_id(org): + with pytest.raises(KeyError): + AddressCreateForm() + + +def test_add_new_address_with_valid_data(org): + at = AddressType.objects.create(descriptor="Primary Address").pk + form = AddressCreateForm({ + "type": at, + "line1": "10 Bawbags Lane", + "line2": "Awful Area", + "line3": "Chudleigh Meadows", + "city": "Curstan", + "county": "East Suncto", + "postcode": "ET31 3PF", + "country": "UK", + "other_details": "There is nothing great about this place!", + }, org=org) + + +def test_add_new_address_blank_data(org): + form = AddressCreateForm({}, org=org) + assert not form.is_valid() + assert form.errors == { + "type": ["This field is required."], + "line1": ["This field is required."], + "country": ["This field is required."], + "postcode": ["This field is required."], + "city": ["This field is required."], + } -- cgit v1.2.3