[Django] #27266: assertFormError fails when trying to check a custom validation in an Admin form

35 views
Skip to first unread message

Django

unread,
Sep 23, 2016, 6:22:11 PM9/23/16
to django-...@googlegroups.com
#27266: assertFormError fails when trying to check a custom validation in an Admin
form
-------------------------------------+-------------------------------------
Reporter: Diego Andrés | Owner: nobody
Sanabria Martín |
Type: Bug | Status: new
Component: contrib.admin | Version: master
Severity: Normal | Keywords: unittest, admin,
| modelform, adminform, test
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
When using the assertFormError in a unittest to check a custome validation
in an admin form the assertion fails because apparently the form in the
admin view behaviours slightly different from a normal view.

Create a project 'what' with an app 'why' (I used 1.8.4 but it was tested
with master branch):

django-admin startproject what
cd what
python manage.py startapp why

Use this files to check the bug:

what/settings.py
{{{
...
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'why',
)
...
}}}

why/models.py
{{{
from django.db import models

# Create your models here.
class WhyMe(models.Model):
name = models.CharField(max_length=255)
}}}

why/admin.py
{{{
from django.contrib import admin
from django.core.exceptions import ValidationError
from django.forms import ModelForm

from why.models import WhyMe

class WhyMeAdminForm(ModelForm):
def clean_name(self):
name = self.cleaned_data['name']
if name.startswith('xxx'):
raise ValidationError('don\'t use xxx!', code='invalid')
return name

@admin.register(WhyMe)
class WhyMeAdmin(admin.ModelAdmin):
form = WhyMeAdminForm
}}}

why/tests.py
{{{
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.test import TestCase

class WhyMeAdminTest(TestCase):
def setUp(self):
self.user = User.objects.create_superuser(username='chuck',
email='ch...@internet.com', password='no')
self.client.login(username='chuck', password='no')

def test_custome_validation(self):
url = reverse('admin:why_whyme_add')
data = {
'name': 'xxxDiegueus9'
}
response = self.client.post(url, data, follow=True)

self.assertEqual(response.status_code, 200)
self.assertFormError(response, 'adminform', 'name', ['don\'t use
xxx!'])
}}}

Finally run the tests and the result would be something like:
{{{
Creating test database for alias 'default'...
E
======================================================================
ERROR: test_custome_validation (why.tests.WhyMeAdminTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/diegueus9/.virtualenvs/django1.8/what/why/tests.py", line
18, in test_custome_validation
self.assertFormError(response, 'adminform', 'name', ['don\'t use
xxx!'])
File "/Users/diegueus9/.virtualenvs/django1.8/lib/python2.7/site-
packages/Django-1.11.dev20160923192832-py2.7.egg/django/test/testcases.py",
line 428, in assertFormError
if field in context[form].errors:
AttributeError: 'AdminForm' object has no attribute 'errors'

----------------------------------------------------------------------
Ran 1 test in 0.771s

FAILED (errors=1)
Destroying test database for alias 'default'...
}}}

In the attached file I show that the validation works fine in the admin.
I did a little of debug using ipdb and noticed that the errors are in
'AdminForm.form.errors', perhaps it should be added a property to the
AdminForm?
This also fails using django 1.8.x, 1.9.x, 1.10.x

--
Ticket URL: <https://code.djangoproject.com/ticket/27266>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 23, 2016, 6:23:03 PM9/23/16
to django-...@googlegroups.com
#27266: assertFormError fails when trying to check a custom validation in an Admin
form
-------------------------------------------------+-------------------------
Reporter: Diego Andrés Sanabria Martín | Owner: nobody

Type: Bug | Status: new
Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: unittest, admin, modelform, | Triage Stage:
adminform, test | Unreviewed

Has patch: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------------------+-------------------------
Changes (by Diego Andrés Sanabria Martín):

* Attachment "Screen Shot 2016-09-23 at 17.22.19.png" added.

Custom validation in admin

Django

unread,
Sep 26, 2016, 12:40:22 PM9/26/16
to django-...@googlegroups.com
#27266: assertFormError fails when trying to check a custom validation in an Admin
form
-------------------------------------+-------------------------------------
Reporter: Diego Andrés | Owner: nobody
Sanabria Martín |
Type: Bug | Status: new
Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: unittest, admin, | Triage Stage: Accepted
modelform, adminform, test |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* needs_better_patch: => 0
* has_patch: 0 => 1
* stage: Unreviewed => Accepted
* needs_tests: => 0
* needs_docs: => 0


Comment:

I'd rather not have those form and formset wrappers in the first place,
but as long as we have them, it seems like a sensible idea. Can you review
my [https://github.com/django/django/pull/7296 PR]?

--
Ticket URL: <https://code.djangoproject.com/ticket/27266#comment:1>

Django

unread,
Sep 26, 2016, 3:01:07 PM9/26/16
to django-...@googlegroups.com
#27266: assertFormError fails when trying to check a custom validation in an Admin
form
-------------------------------------+-------------------------------------
Reporter: Diego Andrés | Owner: nobody
Sanabria Martín |
Type: Bug | Status: new
Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: unittest, admin, | Triage Stage: Accepted
modelform, adminform, test |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Diego Andrés Sanabria Martín):

Tim, it looks good to me, I just wrote a comment:

" just wonder if self.formset.non_form_errors ==
self.form.non_form_errors."

--
Ticket URL: <https://code.djangoproject.com/ticket/27266#comment:2>

Django

unread,
Sep 27, 2016, 9:56:33 AM9/27/16
to django-...@googlegroups.com
#27266: assertFormError fails when trying to check a custom validation in an Admin
form
-------------------------------------+-------------------------------------
Reporter: Diego Andrés | Owner: nobody
Sanabria Martín |
Type: Bug | Status: closed
Component: contrib.admin | Version: master
Severity: Normal | Resolution: fixed

Keywords: unittest, admin, | Triage Stage: Accepted
modelform, adminform, test |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by GitHub <noreply@…>):

* status: new => closed
* resolution: => fixed


Comment:

In [changeset:"6f3c78dbe6f17997aa9d115d041bbb0318061ba7" 6f3c78d]:
{{{
#!CommitTicketReference repository=""
revision="6f3c78dbe6f17997aa9d115d041bbb0318061ba7"
Fixed #27266 -- Allowed using assertFormError()/assertFormsetError() in
admin forms and formsets.

Thanks Diego Andrés Sanabria Martín for the report and review.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/27266#comment:3>

Reply all
Reply to author
Forward
0 new messages