Skip to content
Snippets Groups Projects
Commit 4d308029 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

validation to visit dates added

parent 29187621
No related branches found
No related tags found
No related merge requests found
...@@ -151,3 +151,8 @@ class VisitAddForm(ModelForm): ...@@ -151,3 +151,8 @@ class VisitAddForm(ModelForm):
class Meta: class Meta:
model = Visit model = Visit
exclude = ['is_finished'] exclude = ['is_finished']
def clean(self):
if (self.cleaned_data['datetime_begin']>=self.cleaned_data['datetime_end']):
self.add_error('datetime_begin', "Start date must be before end date")
self.add_error('datetime_end', "Start date must be before end date")
from django.test import TestCase
from web.forms import SubjectAddForm
from web.forms import VisitAddForm
from web.models import Subject
from web.models import Visit
class SubjectAddFormTests(TestCase):
def setUp(self):
subject_data = {'first_name': 'name',
'last_name': 'name',
'status' : Subject.STATUS_CHOICES_NOT_CONTACTED,
'sex' : Subject.SEX_CHOICES_MALE,
'country' : 'Luxembourg',
}
self.subject = SubjectAddForm(data=subject_data).save()
self.sample_data = {'datetime_begin': "2017-01-01",
'datetime_end': "2017-02-02",
'visit_type': Visit.TYPE_CHOICES_OTHER,
'subject' : self.subject.id
}
def test_validation(self):
form = VisitAddForm(data=self.sample_data)
self.assertTrue(form.is_valid())
def test_invalid_validation(self):
self.sample_data['datetime_begin'] = "2017-02-02"
self.sample_data['datetime_end'] = "2017-01-01"
form = VisitAddForm(data=self.sample_data)
validation_status = form.is_valid()
self.assertFalse(validation_status)
self.assertTrue("datetime_begin" in form.errors)
self.assertTrue("datetime_end" in form.errors)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment