diff options
author | Matthew Lemon <lemon@matthewlemon.com> | 2020-10-10 17:02:15 +0100 |
---|---|---|
committer | Matthew Lemon <lemon@matthewlemon.com> | 2020-10-10 17:02:15 +0100 |
commit | c7e1faecabf81cbabf0241a226624c8ed562c98c (patch) | |
tree | b3daa89cdcdcc2c518d8177a572413acd093d56a /ctrack | |
parent | d4431a7c4a31b05356fd1efc9e5cc91dda58ad4b (diff) |
started to test forms! love testing forms
Diffstat (limited to 'ctrack')
-rw-r--r-- | ctrack/register/forms.py | 36 | ||||
-rw-r--r-- | ctrack/register/models.py | 2 | ||||
-rw-r--r-- | ctrack/register/tests/test_forms.py | 37 |
3 files changed, 70 insertions, 5 deletions
diff --git a/ctrack/register/forms.py b/ctrack/register/forms.py index 3f13d33..16c82f0 100644 --- a/ctrack/register/forms.py +++ b/ctrack/register/forms.py @@ -6,7 +6,29 @@ from django.urls import reverse from ctrack.caf.models import CAF from ctrack.organisations.models import Person, Organisation -from ctrack.register.models import EngagementEvent, EngagementType +from ctrack.register.models import EngagementEvent, EngagementType, MeetingEvent + + +class AddMeetingForm(forms.ModelForm): + class Meta: + model = MeetingEvent + fields = [ + "type_descriptor", + "short_description", + "datetime", + "comments", + "location", + ] + + def __init__(self, *args, **kwargs): + self.user = kwargs.pop("user") + super().__init__(*args, **kwargs) + + def save(self): + form = super().save(commit=False) + form.user = self.user + form.save() + return form class EngagementEventCreateForm(forms.ModelForm): @@ -16,8 +38,12 @@ class EngagementEventCreateForm(forms.ModelForm): org = CAF.objects.get(pk=caf).organisation cancel_redirect = reverse("caf:detail", args=[caf]) self.fields["related_caf"].initial = caf - self.fields["participants"].queryset = Person.objects.filter(organisation__pk=org.pk) - self.fields["type"].queryset = EngagementType.objects.all().order_by("descriptor") + self.fields["participants"].queryset = Person.objects.filter( + organisation__pk=org.pk + ) + self.fields["type"].queryset = EngagementType.objects.all().order_by( + "descriptor" + ) self.helper = FormHelper(self) self.helper.layout = Layout( Field("type"), @@ -48,7 +74,9 @@ class EngagementEventCreateForm(forms.ModelForm): selectable_people = Person.objects.filter(organisation__slug=org_slug) self.fields["participants"].queryset = selectable_people self.fields["participants"].initial = selectable_people.first() - self.fields["type"].queryset = EngagementType.objects.all().order_by("descriptor") + self.fields["type"].queryset = EngagementType.objects.all().order_by( + "descriptor" + ) self.fields["related_caf"].queryset = org.caf_set.all() self.fields["related_caf"].label = "Related CAFs" self.helper = FormHelper(self) diff --git a/ctrack/register/models.py b/ctrack/register/models.py index 949c8d0..7e6d46e 100644 --- a/ctrack/register/models.py +++ b/ctrack/register/models.py @@ -65,7 +65,7 @@ class EngagementEventBase(AuditableEventBase): class MeetingEventMixin(models.Model): participants = models.ManyToManyField(Person, blank=False, null=False) - location = models.CharField(max_length=100, blank=True, null=True) + location = models.CharField(max_length=100, blank=False) class Meta: abstract = True diff --git a/ctrack/register/tests/test_forms.py b/ctrack/register/tests/test_forms.py new file mode 100644 index 0000000..0383fa3 --- /dev/null +++ b/ctrack/register/tests/test_forms.py @@ -0,0 +1,37 @@ +import pytest + +from ..forms import AddMeetingForm + +pytestmark = pytest.mark.django_db + + +def test_init(user): + """Here we test that we can pass in the user value from the view. + We don't want that to be field in the form. + """ + form = AddMeetingForm({ + "type_descriptor": "Meeting", # Must be Meeting as that is in the choices param + "short_description": "Test short description", + "datetime": "2010-10-10T13:00", + "comments": "Test Comments", + "location": "Transient Moabs" + }, + user=user, + ) + assert form.is_valid() + + +def test_blank_data(user): + """Missing location and datetime fields which are required.""" + form = AddMeetingForm({ + "type_descriptor": "Meeting", + "short_description": "Test short description", + "comments": "Test Comments", + }, + user=user, + ) + assert form.is_valid() is False + assert form.errors == { + "location": ["This field is required."], + "datetime": ["This field is required."] + } |