Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #14454 -- converted admin_widgets tests from doctests to unitte…

…sts. 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
  • Loading branch information...
commit 2e8bc8f9862f127b0e69766c7fc7dc92f3156d20 1 parent 65b98c9
Alex Gaynor authored October 15, 2010
101  tests/regressiontests/admin_widgets/models.py
... ...
@@ -1,7 +1,4 @@
1  
-
2  
-from django.conf import settings
3 1
 from django.db import models
4  
-from django.core.files.storage import default_storage
5 2
 from django.contrib.auth.models import User
6 3
 
7 4
 class MyFileField(models.FileField): 
@@ -69,101 +66,3 @@ class CarTire(models.Model):
69 66
     A single car tire. This to test that a user can only select their own cars.
70 67
     """
71 68
     car = models.ForeignKey(Car)
72  
-
73  
-__test__ = {'WIDGETS_TESTS': """
74  
->>> from datetime import datetime
75  
->>> from django.utils.html import escape, conditional_escape
76  
->>> from django.core.files.uploadedfile import SimpleUploadedFile
77  
->>> from django.contrib.admin.widgets import FilteredSelectMultiple, AdminSplitDateTime
78  
->>> from django.contrib.admin.widgets import AdminFileWidget, ForeignKeyRawIdWidget, ManyToManyRawIdWidget
79  
->>> from django.contrib.admin.widgets import RelatedFieldWidgetWrapper
80  
->>> from django.utils.translation import activate, deactivate
81  
->>> from django.conf import settings
82  
-
83  
-
84  
-Calling conditional_escape on the output of widget.render will simulate what
85  
-happens in the template. This is easier than setting up a template and context
86  
-for each test.
87  
-
88  
-Make sure that the Admin widgets render properly, that is, without their extra
89  
-HTML escaped.
90  
-
91  
->>> w = FilteredSelectMultiple('test', False)
92  
->>> print conditional_escape(w.render('test', 'test'))
93  
-<select multiple="multiple" name="test" class="selectfilter">
94  
-</select><script type="text/javascript">addEvent(window, "load", function(e) {SelectFilter.init("id_test", "test", 0, "%(ADMIN_MEDIA_PREFIX)s"); });</script>
95  
-<BLANKLINE>
96  
-
97  
->>> w = FilteredSelectMultiple('test', True)
98  
->>> print conditional_escape(w.render('test', 'test'))
99  
-<select multiple="multiple" name="test" class="selectfilterstacked">
100  
-</select><script type="text/javascript">addEvent(window, "load", function(e) {SelectFilter.init("id_test", "test", 1, "%(ADMIN_MEDIA_PREFIX)s"); });</script>
101  
-<BLANKLINE>
102  
-
103  
->>> w = AdminSplitDateTime()
104  
->>> print conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30)))
105  
-<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>
106  
->>> activate('de-at')
107  
->>> settings.USE_L10N = True
108  
->>> w.is_localized = True
109  
->>> print conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30)))
110  
-<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>
111  
->>> deactivate()
112  
->>> settings.USE_L10N = False
113  
-
114  
->>> band = Band.objects.create(pk=1, name='Linkin Park')
115  
->>> album = band.album_set.create(name='Hybrid Theory', cover_art=r'albums\hybrid_theory.jpg')
116  
-
117  
->>> w = AdminFileWidget()
118  
->>> print conditional_escape(w.render('test', album.cover_art))
119  
-<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>
120  
->>> print conditional_escape(w.render('test', SimpleUploadedFile('test', 'content')))
121  
-<input type="file" name="test" />
122  
-
123  
->>> rel = Album._meta.get_field('band').rel
124  
->>> w = ForeignKeyRawIdWidget(rel)
125  
->>> print conditional_escape(w.render('test', band.pk, attrs={}))
126  
-<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>
127  
-
128  
->>> m1 = Member.objects.create(pk=1, name='Chester')
129  
->>> m2 = Member.objects.create(pk=2, name='Mike')
130  
->>> band.members.add(m1, m2)
131  
-
132  
->>> rel = Band._meta.get_field('members').rel
133  
->>> w = ManyToManyRawIdWidget(rel)
134  
->>> print conditional_escape(w.render('test', [m1.pk, m2.pk], attrs={}))
135  
-<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>
136  
->>> print conditional_escape(w.render('test', [m1.pk]))
137  
-<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>
138  
->>> w._has_changed(None, None)
139  
-False
140  
->>> w._has_changed([], None)
141  
-False
142  
->>> w._has_changed(None, [u'1'])
143  
-True
144  
->>> w._has_changed([1, 2], [u'1', u'2'])
145  
-False
146  
->>> w._has_changed([1, 2], [u'1'])
147  
-True
148  
->>> w._has_changed([1, 2], [u'1', u'3'])
149  
-True
150  
-
151  
-# Check that ForeignKeyRawIdWidget works with fields which aren't related to
152  
-# the model's primary key.
153  
->>> apple = Inventory.objects.create(barcode=86, name='Apple')
154  
->>> pear = Inventory.objects.create(barcode=22, name='Pear')
155  
->>> core = Inventory.objects.create(barcode=87, name='Core', parent=apple)
156  
->>> rel = Inventory._meta.get_field('parent').rel
157  
->>> w = ForeignKeyRawIdWidget(rel)
158  
->>> print w.render('test', core.parent_id, attrs={})
159  
-<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>
160  
-
161  
-# see #9258
162  
->>> hidden = Inventory.objects.create(barcode=93, name='Hidden', hidden=True)
163  
->>> child_of_hidden = Inventory.objects.create(barcode=94, name='Child of hidden', parent=hidden)
164  
->>> print w.render('test', child_of_hidden.parent_id, attrs={})
165  
-<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>
166  
-""" % {
167  
-    'ADMIN_MEDIA_PREFIX': settings.ADMIN_MEDIA_PREFIX,
168  
-    'STORAGE_URL': default_storage.url(''),
169  
-}}
150  tests/regressiontests/admin_widgets/tests.py
... ...
@@ -1,14 +1,25 @@
1 1
 # encoding: utf-8
2 2
 
  3
+from datetime import datetime
  4
+
3 5
 from django import forms
  6
+from django.conf import settings
4 7
 from django.contrib import admin
5 8
 from django.contrib.admin import widgets
  9
+from django.contrib.admin.widgets import FilteredSelectMultiple, AdminSplitDateTime
  10
+from django.contrib.admin.widgets import (AdminFileWidget, ForeignKeyRawIdWidget,
  11
+    ManyToManyRawIdWidget)
  12
+from django.core.files.storage import default_storage
  13
+from django.core.files.uploadedfile import SimpleUploadedFile
6 14
 from django.db.models import DateField
7 15
 from django.test import TestCase as DjangoTestCase
  16
+from django.utils.html import conditional_escape
  17
+from django.utils.translation import activate, deactivate
8 18
 from django.utils.unittest import TestCase
9 19
 
10 20
 import models
11 21
 
  22
+
12 23
 class AdminFormfieldForDBFieldTests(TestCase):
13 24
     """
14 25
     Tests for correct behavior of ModelAdmin.formfield_for_dbfield
@@ -105,6 +116,7 @@ def testChoicesWithRadioFields(self):
105 116
     def testInheritance(self):
106 117
         self.assertFormfield(models.Album, 'backside_art', widgets.AdminFileWidget)
107 118
 
  119
+
108 120
 class AdminFormfieldForDBFieldWithRequestTests(DjangoTestCase):
109 121
     fixtures = ["admin-widgets-users.xml"]
110 122
 
@@ -117,6 +129,7 @@ def testFilterChoicesByRequestUser(self):
117 129
         self.assert_("BMW M3" not in response.content)
118 130
         self.assert_("Volkswagon Passat" in response.content)
119 131
 
  132
+
120 133
 class AdminForeignKeyWidgetChangeList(DjangoTestCase):
121 134
     fixtures = ["admin-widgets-users.xml"]
122 135
     admin_root = '/widget_admin'
@@ -131,6 +144,7 @@ def test_changelist_foreignkey(self):
131 144
         response = self.client.get('%s/admin_widgets/car/' % self.admin_root)
132 145
         self.failUnless('%s/auth/user/add/' % self.admin_root in response.content)
133 146
 
  147
+
134 148
 class AdminForeignKeyRawIdWidget(DjangoTestCase):
135 149
     fixtures = ["admin-widgets-users.xml"]
136 150
     admin_root = '/widget_admin'
@@ -164,3 +178,139 @@ def test_invalid_target_id(self):
164 178
 
165 179
             self.assertContains(response,
166 180
                 'Select a valid choice. That choice is not one of the available choices.')
  181
+
  182
+
  183
+class FilteredSelectMultipleWidgetTest(TestCase):
  184
+    def test_render(self):
  185
+        w = FilteredSelectMultiple('test', False)
  186
+        self.assertEqual(
  187
+            conditional_escape(w.render('test', 'test')),
  188
+            '<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}
  189
+        )
  190
+
  191
+    def test_stacked_render(self):
  192
+        w = FilteredSelectMultiple('test', True)
  193
+        self.assertEqual(
  194
+            conditional_escape(w.render('test', 'test')),
  195
+            '<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}
  196
+        )
  197
+
  198
+
  199
+class AdminSplitDateTimeWidgetTest(TestCase):
  200
+    def test_render(self):
  201
+        w = AdminSplitDateTime()
  202
+        self.assertEqual(
  203
+            conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30))),
  204
+            '<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>',
  205
+        )
  206
+
  207
+    def test_localization(self):
  208
+        w = AdminSplitDateTime()
  209
+
  210
+        activate('de-at')
  211
+        old_USE_L10N = settings.USE_L10N
  212
+        settings.USE_L10N = True
  213
+        w.is_localized = True
  214
+        self.assertEqual(
  215
+            conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30))),
  216
+            '<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>',
  217
+        )
  218
+        deactivate()
  219
+        settings.USE_L10N = old_USE_L10N
  220
+
  221
+
  222
+class AdminFileWidgetTest(DjangoTestCase):
  223
+    def test_render(self):
  224
+        band = models.Band.objects.create(name='Linkin Park')
  225
+        album = band.album_set.create(
  226
+            name='Hybrid Theory', cover_art=r'albums\hybrid_theory.jpg'
  227
+        )
  228
+
  229
+        w = AdminFileWidget()
  230
+        self.assertEqual(
  231
+            conditional_escape(w.render('test', album.cover_art)),
  232
+            '<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('') },
  233
+        )
  234
+
  235
+        self.assertEqual(
  236
+            conditional_escape(w.render('test', SimpleUploadedFile('test', 'content'))),
  237
+            '<input type="file" name="test" />',
  238
+        )
  239
+
  240
+
  241
+class ForeignKeyRawIdWidgetTest(DjangoTestCase):
  242
+    def test_render(self):
  243
+        band = models.Band.objects.create(name='Linkin Park')
  244
+        band.album_set.create(
  245
+            name='Hybrid Theory', cover_art=r'albums\hybrid_theory.jpg'
  246
+        )
  247
+        rel = models.Album._meta.get_field('band').rel
  248
+
  249
+        w = ForeignKeyRawIdWidget(rel)
  250
+        self.assertEqual(
  251
+            conditional_escape(w.render('test', band.pk, attrs={})),
  252
+            '<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},
  253
+        )
  254
+
  255
+    def test_relations_to_non_primary_key(self):
  256
+        # Check that ForeignKeyRawIdWidget works with fields which aren't
  257
+        # related to the model's primary key.
  258
+        apple = models.Inventory.objects.create(barcode=86, name='Apple')
  259
+        models.Inventory.objects.create(barcode=22, name='Pear')
  260
+        core = models.Inventory.objects.create(
  261
+            barcode=87, name='Core', parent=apple
  262
+        )
  263
+        rel = models.Inventory._meta.get_field('parent').rel
  264
+        w = ForeignKeyRawIdWidget(rel)
  265
+        self.assertEqual(
  266
+            w.render('test', core.parent_id, attrs={}),
  267
+            '<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},
  268
+        )
  269
+
  270
+
  271
+    def test_proper_manager_for_label_lookup(self):
  272
+        # see #9258
  273
+        rel = models.Inventory._meta.get_field('parent').rel
  274
+        w = ForeignKeyRawIdWidget(rel)
  275
+
  276
+        hidden = models.Inventory.objects.create(
  277
+            barcode=93, name='Hidden', hidden=True
  278
+        )
  279
+        child_of_hidden = models.Inventory.objects.create(
  280
+            barcode=94, name='Child of hidden', parent=hidden
  281
+        )
  282
+        self.assertEqual(
  283
+            w.render('test', child_of_hidden.parent_id, attrs={}),
  284
+            '<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},
  285
+        )
  286
+
  287
+
  288
+class ManyToManyRawIdWidgetTest(DjangoTestCase):
  289
+    def test_render(self):
  290
+        band = models.Band.objects.create(name='Linkin Park')
  291
+        band.album_set.create(
  292
+            name='Hybrid Theory', cover_art=r'albums\hybrid_theory.jpg'
  293
+        )
  294
+
  295
+        m1 = models.Member.objects.create(name='Chester')
  296
+        m2 = models.Member.objects.create(name='Mike')
  297
+        band.members.add(m1, m2)
  298
+        rel = models.Band._meta.get_field('members').rel
  299
+
  300
+        w = ManyToManyRawIdWidget(rel)
  301
+        self.assertEqual(
  302
+            conditional_escape(w.render('test', [m1.pk, m2.pk], attrs={})),
  303
+            '<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},
  304
+        )
  305
+
  306
+        self.assertEqual(
  307
+            conditional_escape(w.render('test', [m1.pk])),
  308
+            '<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},
  309
+        )
  310
+
  311
+        self.assertEqual(w._has_changed(None, None), False)
  312
+        self.assertEqual(w._has_changed([], None), False)
  313
+        self.assertEqual(w._has_changed(None, [u'1']), True)
  314
+        self.assertEqual(w._has_changed([1, 2], [u'1', u'2']), False)
  315
+        self.assertEqual(w._has_changed([1, 2], [u'1']), True)
  316
+        self.assertEqual(w._has_changed([1, 2], [u'1', u'3']), True)

0 notes on commit 2e8bc8f

Please sign in to comment.
Something went wrong with that request. Please try again.