Fixed #14454 -- converted admin_widgets tests from doctests to unittests. We have always been at war with doctests. Thanks to prestontimmons for the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14221 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Alex Gaynor 2010-10-15 02:35:51 +00:00
parent 65b98c9b41
commit 2e8bc8f986
2 changed files with 150 additions and 101 deletions

View File

@ -1,7 +1,4 @@
from django.conf import settings
from django.db import models from django.db import models
from django.core.files.storage import default_storage
from django.contrib.auth.models import User from django.contrib.auth.models import User
class MyFileField(models.FileField): class MyFileField(models.FileField):
@ -69,101 +66,3 @@ class CarTire(models.Model):
A single car tire. This to test that a user can only select their own cars. A single car tire. This to test that a user can only select their own cars.
""" """
car = models.ForeignKey(Car) car = models.ForeignKey(Car)
__test__ = {'WIDGETS_TESTS': """
>>> from datetime import datetime
>>> from django.utils.html import escape, conditional_escape
>>> from django.core.files.uploadedfile import SimpleUploadedFile
>>> from django.contrib.admin.widgets import FilteredSelectMultiple, AdminSplitDateTime
>>> from django.contrib.admin.widgets import AdminFileWidget, ForeignKeyRawIdWidget, ManyToManyRawIdWidget
>>> from django.contrib.admin.widgets import RelatedFieldWidgetWrapper
>>> from django.utils.translation import activate, deactivate
>>> from django.conf import settings
Calling conditional_escape on the output of widget.render will simulate what
happens in the template. This is easier than setting up a template and context
for each test.
Make sure that the Admin widgets render properly, that is, without their extra
HTML escaped.
>>> w = FilteredSelectMultiple('test', False)
>>> print conditional_escape(w.render('test', 'test'))
<select multiple="multiple" name="test" class="selectfilter">
</select><script type="text/javascript">addEvent(window, "load", function(e) {SelectFilter.init("id_test", "test", 0, "%(ADMIN_MEDIA_PREFIX)s"); });</script>
<BLANKLINE>
>>> w = FilteredSelectMultiple('test', True)
>>> print conditional_escape(w.render('test', 'test'))
<select multiple="multiple" name="test" class="selectfilterstacked">
</select><script type="text/javascript">addEvent(window, "load", function(e) {SelectFilter.init("id_test", "test", 1, "%(ADMIN_MEDIA_PREFIX)s"); });</script>
<BLANKLINE>
>>> w = AdminSplitDateTime()
>>> print conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30)))
<p class="datetime">Date: <input value="2007-12-01" type="text" class="vDateField" name="test_0" size="10" /><br />Time: <input value="09:30:00" type="text" class="vTimeField" name="test_1" size="8" /></p>
>>> activate('de-at')
>>> settings.USE_L10N = True
>>> w.is_localized = True
>>> print conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30)))
<p class="datetime">Datum: <input value="01.12.2007" type="text" class="vDateField" name="test_0" size="10" /><br />Zeit: <input value="09:30:00" type="text" class="vTimeField" name="test_1" size="8" /></p>
>>> deactivate()
>>> settings.USE_L10N = False
>>> band = Band.objects.create(pk=1, name='Linkin Park')
>>> album = band.album_set.create(name='Hybrid Theory', cover_art=r'albums\hybrid_theory.jpg')
>>> w = AdminFileWidget()
>>> print conditional_escape(w.render('test', album.cover_art))
<p class="file-upload">Currently: <a target="_blank" href="%(STORAGE_URL)salbums/hybrid_theory.jpg">albums\hybrid_theory.jpg</a> <span class="clearable-file-input"><input type="checkbox" name="test-clear" id="test-clear_id" /> <label for="test-clear_id">Clear</label></span><br />Change: <input type="file" name="test" /></p>
>>> print conditional_escape(w.render('test', SimpleUploadedFile('test', 'content')))
<input type="file" name="test" />
>>> rel = Album._meta.get_field('band').rel
>>> w = ForeignKeyRawIdWidget(rel)
>>> print conditional_escape(w.render('test', band.pk, attrs={}))
<input type="text" name="test" value="1" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/band/?t=id" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>&nbsp;<strong>Linkin Park</strong>
>>> m1 = Member.objects.create(pk=1, name='Chester')
>>> m2 = Member.objects.create(pk=2, name='Mike')
>>> band.members.add(m1, m2)
>>> rel = Band._meta.get_field('members').rel
>>> w = ManyToManyRawIdWidget(rel)
>>> print conditional_escape(w.render('test', [m1.pk, m2.pk], attrs={}))
<input type="text" name="test" value="1,2" class="vManyToManyRawIdAdminField" /><a href="../../../admin_widgets/member/" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>
>>> print conditional_escape(w.render('test', [m1.pk]))
<input type="text" name="test" value="1" class="vManyToManyRawIdAdminField" /><a href="../../../admin_widgets/member/" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>
>>> w._has_changed(None, None)
False
>>> w._has_changed([], None)
False
>>> w._has_changed(None, [u'1'])
True
>>> w._has_changed([1, 2], [u'1', u'2'])
False
>>> w._has_changed([1, 2], [u'1'])
True
>>> w._has_changed([1, 2], [u'1', u'3'])
True
# Check that ForeignKeyRawIdWidget works with fields which aren't related to
# the model's primary key.
>>> apple = Inventory.objects.create(barcode=86, name='Apple')
>>> pear = Inventory.objects.create(barcode=22, name='Pear')
>>> core = Inventory.objects.create(barcode=87, name='Core', parent=apple)
>>> rel = Inventory._meta.get_field('parent').rel
>>> w = ForeignKeyRawIdWidget(rel)
>>> print w.render('test', core.parent_id, attrs={})
<input type="text" name="test" value="86" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/inventory/?t=barcode" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>&nbsp;<strong>Apple</strong>
# see #9258
>>> hidden = Inventory.objects.create(barcode=93, name='Hidden', hidden=True)
>>> child_of_hidden = Inventory.objects.create(barcode=94, name='Child of hidden', parent=hidden)
>>> print w.render('test', child_of_hidden.parent_id, attrs={})
<input type="text" name="test" value="93" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/inventory/?t=barcode" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>&nbsp;<strong>Hidden</strong>
""" % {
'ADMIN_MEDIA_PREFIX': settings.ADMIN_MEDIA_PREFIX,
'STORAGE_URL': default_storage.url(''),
}}

View File

@ -1,14 +1,25 @@
# encoding: utf-8 # encoding: utf-8
from datetime import datetime
from django import forms from django import forms
from django.conf import settings
from django.contrib import admin from django.contrib import admin
from django.contrib.admin import widgets from django.contrib.admin import widgets
from django.contrib.admin.widgets import FilteredSelectMultiple, AdminSplitDateTime
from django.contrib.admin.widgets import (AdminFileWidget, ForeignKeyRawIdWidget,
ManyToManyRawIdWidget)
from django.core.files.storage import default_storage
from django.core.files.uploadedfile import SimpleUploadedFile
from django.db.models import DateField from django.db.models import DateField
from django.test import TestCase as DjangoTestCase from django.test import TestCase as DjangoTestCase
from django.utils.html import conditional_escape
from django.utils.translation import activate, deactivate
from django.utils.unittest import TestCase from django.utils.unittest import TestCase
import models import models
class AdminFormfieldForDBFieldTests(TestCase): class AdminFormfieldForDBFieldTests(TestCase):
""" """
Tests for correct behavior of ModelAdmin.formfield_for_dbfield Tests for correct behavior of ModelAdmin.formfield_for_dbfield
@ -105,6 +116,7 @@ class AdminFormfieldForDBFieldTests(TestCase):
def testInheritance(self): def testInheritance(self):
self.assertFormfield(models.Album, 'backside_art', widgets.AdminFileWidget) self.assertFormfield(models.Album, 'backside_art', widgets.AdminFileWidget)
class AdminFormfieldForDBFieldWithRequestTests(DjangoTestCase): class AdminFormfieldForDBFieldWithRequestTests(DjangoTestCase):
fixtures = ["admin-widgets-users.xml"] fixtures = ["admin-widgets-users.xml"]
@ -117,6 +129,7 @@ class AdminFormfieldForDBFieldWithRequestTests(DjangoTestCase):
self.assert_("BMW M3" not in response.content) self.assert_("BMW M3" not in response.content)
self.assert_("Volkswagon Passat" in response.content) self.assert_("Volkswagon Passat" in response.content)
class AdminForeignKeyWidgetChangeList(DjangoTestCase): class AdminForeignKeyWidgetChangeList(DjangoTestCase):
fixtures = ["admin-widgets-users.xml"] fixtures = ["admin-widgets-users.xml"]
admin_root = '/widget_admin' admin_root = '/widget_admin'
@ -131,6 +144,7 @@ class AdminForeignKeyWidgetChangeList(DjangoTestCase):
response = self.client.get('%s/admin_widgets/car/' % self.admin_root) response = self.client.get('%s/admin_widgets/car/' % self.admin_root)
self.failUnless('%s/auth/user/add/' % self.admin_root in response.content) self.failUnless('%s/auth/user/add/' % self.admin_root in response.content)
class AdminForeignKeyRawIdWidget(DjangoTestCase): class AdminForeignKeyRawIdWidget(DjangoTestCase):
fixtures = ["admin-widgets-users.xml"] fixtures = ["admin-widgets-users.xml"]
admin_root = '/widget_admin' admin_root = '/widget_admin'
@ -164,3 +178,139 @@ class AdminForeignKeyRawIdWidget(DjangoTestCase):
self.assertContains(response, self.assertContains(response,
'Select a valid choice. That choice is not one of the available choices.') 'Select a valid choice. That choice is not one of the available choices.')
class FilteredSelectMultipleWidgetTest(TestCase):
def test_render(self):
w = FilteredSelectMultiple('test', False)
self.assertEqual(
conditional_escape(w.render('test', 'test')),
'<select multiple="multiple" name="test" class="selectfilter">\n</select><script type="text/javascript">addEvent(window, "load", function(e) {SelectFilter.init("id_test", "test", 0, "%(ADMIN_MEDIA_PREFIX)s"); });</script>\n' % {"ADMIN_MEDIA_PREFIX": settings.ADMIN_MEDIA_PREFIX}
)
def test_stacked_render(self):
w = FilteredSelectMultiple('test', True)
self.assertEqual(
conditional_escape(w.render('test', 'test')),
'<select multiple="multiple" name="test" class="selectfilterstacked">\n</select><script type="text/javascript">addEvent(window, "load", function(e) {SelectFilter.init("id_test", "test", 1, "%(ADMIN_MEDIA_PREFIX)s"); });</script>\n' % {"ADMIN_MEDIA_PREFIX": settings.ADMIN_MEDIA_PREFIX}
)
class AdminSplitDateTimeWidgetTest(TestCase):
def test_render(self):
w = AdminSplitDateTime()
self.assertEqual(
conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30))),
'<p class="datetime">Date: <input value="2007-12-01" type="text" class="vDateField" name="test_0" size="10" /><br />Time: <input value="09:30:00" type="text" class="vTimeField" name="test_1" size="8" /></p>',
)
def test_localization(self):
w = AdminSplitDateTime()
activate('de-at')
old_USE_L10N = settings.USE_L10N
settings.USE_L10N = True
w.is_localized = True
self.assertEqual(
conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30))),
'<p class="datetime">Datum: <input value="01.12.2007" type="text" class="vDateField" name="test_0" size="10" /><br />Zeit: <input value="09:30:00" type="text" class="vTimeField" name="test_1" size="8" /></p>',
)
deactivate()
settings.USE_L10N = old_USE_L10N
class AdminFileWidgetTest(DjangoTestCase):
def test_render(self):
band = models.Band.objects.create(name='Linkin Park')
album = band.album_set.create(
name='Hybrid Theory', cover_art=r'albums\hybrid_theory.jpg'
)
w = AdminFileWidget()
self.assertEqual(
conditional_escape(w.render('test', album.cover_art)),
'<p class="file-upload">Currently: <a target="_blank" href="%(STORAGE_URL)salbums/hybrid_theory.jpg">albums\hybrid_theory.jpg</a> <span class="clearable-file-input"><input type="checkbox" name="test-clear" id="test-clear_id" /> <label for="test-clear_id">Clear</label></span><br />Change: <input type="file" name="test" /></p>' % { 'STORAGE_URL': default_storage.url('') },
)
self.assertEqual(
conditional_escape(w.render('test', SimpleUploadedFile('test', 'content'))),
'<input type="file" name="test" />',
)
class ForeignKeyRawIdWidgetTest(DjangoTestCase):
def test_render(self):
band = models.Band.objects.create(name='Linkin Park')
band.album_set.create(
name='Hybrid Theory', cover_art=r'albums\hybrid_theory.jpg'
)
rel = models.Album._meta.get_field('band').rel
w = ForeignKeyRawIdWidget(rel)
self.assertEqual(
conditional_escape(w.render('test', band.pk, attrs={})),
'<input type="text" name="test" value="%(bandpk)s" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/band/?t=id" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>&nbsp;<strong>Linkin Park</strong>' % {"ADMIN_MEDIA_PREFIX": settings.ADMIN_MEDIA_PREFIX, "bandpk": band.pk},
)
def test_relations_to_non_primary_key(self):
# Check that ForeignKeyRawIdWidget works with fields which aren't
# related to the model's primary key.
apple = models.Inventory.objects.create(barcode=86, name='Apple')
models.Inventory.objects.create(barcode=22, name='Pear')
core = models.Inventory.objects.create(
barcode=87, name='Core', parent=apple
)
rel = models.Inventory._meta.get_field('parent').rel
w = ForeignKeyRawIdWidget(rel)
self.assertEqual(
w.render('test', core.parent_id, attrs={}),
'<input type="text" name="test" value="86" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/inventory/?t=barcode" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>&nbsp;<strong>Apple</strong>' % {"ADMIN_MEDIA_PREFIX": settings.ADMIN_MEDIA_PREFIX},
)
def test_proper_manager_for_label_lookup(self):
# see #9258
rel = models.Inventory._meta.get_field('parent').rel
w = ForeignKeyRawIdWidget(rel)
hidden = models.Inventory.objects.create(
barcode=93, name='Hidden', hidden=True
)
child_of_hidden = models.Inventory.objects.create(
barcode=94, name='Child of hidden', parent=hidden
)
self.assertEqual(
w.render('test', child_of_hidden.parent_id, attrs={}),
'<input type="text" name="test" value="93" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/inventory/?t=barcode" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>&nbsp;<strong>Hidden</strong>' % {"ADMIN_MEDIA_PREFIX": settings.ADMIN_MEDIA_PREFIX},
)
class ManyToManyRawIdWidgetTest(DjangoTestCase):
def test_render(self):
band = models.Band.objects.create(name='Linkin Park')
band.album_set.create(
name='Hybrid Theory', cover_art=r'albums\hybrid_theory.jpg'
)
m1 = models.Member.objects.create(name='Chester')
m2 = models.Member.objects.create(name='Mike')
band.members.add(m1, m2)
rel = models.Band._meta.get_field('members').rel
w = ManyToManyRawIdWidget(rel)
self.assertEqual(
conditional_escape(w.render('test', [m1.pk, m2.pk], attrs={})),
'<input type="text" name="test" value="%(m1pk)s,%(m2pk)s" class="vManyToManyRawIdAdminField" /><a href="../../../admin_widgets/member/" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>' % {"ADMIN_MEDIA_PREFIX": settings.ADMIN_MEDIA_PREFIX, "m1pk": m1.pk, "m2pk": m2.pk},
)
self.assertEqual(
conditional_escape(w.render('test', [m1.pk])),
'<input type="text" name="test" value="%(m1pk)s" class="vManyToManyRawIdAdminField" /><a href="../../../admin_widgets/member/" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>' % {"ADMIN_MEDIA_PREFIX": settings.ADMIN_MEDIA_PREFIX, "m1pk": m1.pk},
)
self.assertEqual(w._has_changed(None, None), False)
self.assertEqual(w._has_changed([], None), False)
self.assertEqual(w._has_changed(None, [u'1']), True)
self.assertEqual(w._has_changed([1, 2], [u'1', u'2']), False)
self.assertEqual(w._has_changed([1, 2], [u'1']), True)
self.assertEqual(w._has_changed([1, 2], [u'1', u'3']), True)