So I'm writing a test to check for the occurrence of Validation Errors in my Form.
In doing so I'm getting the following error:
======================================================================
FAIL: test_start_date_before_end_date_errors (reports.tests.ScheduleValidation)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/jwe/piesup2/reports/tests.py", line 61, in test_start_date_before_end_date_errors
self.assertFormError(response, 'form', None, 'End Date Must Be Greater Than Start Date')
File "/home/jwe/piesup2/venv/lib/python3.4/site-packages/django/test/testcases.py", line 467, in assertFormError
" response" % form)
AssertionError: The form 'form' was not used to render the response
----------------------------------------------------------------------
The validation error I'm specifically checking for is raised in models.py inside the clean() method
class Schedule(models.Model)
...
...
def clean(self):
if self.start_date and self.end_date:
# Ensure that the end_date occurs after the start_date
if self.end_date <= self.start_date:
err = "End Date Must Be Greater Than Start Date"
raise ValidationError(err)
The Generic CreateView I am testing looks as follows:
class ScheduleCreate(SuccessMessageMixin, FetchURLMixin, CreateView):
model = Schedule
form_class = ScheduleCreateForm
template_name_suffix = '_create_form'
I'm able to render the errors in the view's template as non_field_errors
as so:
My test looks as follows:
class ScheduleValidation(RequiresLogin, TestCase):
def setUp(self):
self._client = client_fixture.create()[0]
# Setup some example sample data for a Schedule
self.data = {
'client': self._client.id,
'schedule_date': today,
'billing_period': 'Q',
'schedule_type': 'SE',
}
def test_start_date_before_end_date_errors(self):
self.data['start_date'] = today
self.data['end_date'] = yesterday
response = self.client.post('reports:schedule-create', self.data, follow=True)
# Check if redirects back to the page
with self.assertTemplateUsed('schedule_create_form.html'):
self.assertFormError(response, 'form', None, 'End Date Must Be Greater Than Start Date')
I'm setting the field type of None in order to access non_field_errors as detailed by the documentation here
What I've Tried Already
Inside my template for the view, I'm able to reference the form using , including any non_field_errors. Which means it is being passed to the template properly
I can check the context data inside the view itself.
def get_context_data(self, **kwargs):
context = super(ScheduleCreate, self).get_context_data(**kwargs)
assert False
return context
From the breakpoint here I'm able to log the contents of the context variable with Werkzeug in the browser, which shows that 'form' is actually passed to the template
[console ready]
>>> context
{'form': <ScheduleCreateForm bound=True, valid=False, fields=(client;schedule_date;start_date;end_date;billing_period;schedule_type)>, 'view': <reports.views.ScheduleCreate object at 0x7f256eb6c908>}
Which begs the question why am I getting this error, and how would I fix this?
Aucun commentaire:
Enregistrer un commentaire