Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.2.X] Fixed #14472 -- converted generic_relations tests from doctes…

…ts to unitests. We have always been at war with doctests. Thanks to Gabriel Hurley for the patch. Backport of [14225].

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@14226 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 3c469e95fbd92986563cbc5508bfce0bcd868d47 1 parent f18e271
Alex Gaynor authored October 15, 2010
172  tests/modeltests/generic_relations/models.py
@@ -9,9 +9,10 @@
9 9
 from complete).
10 10
 """
11 11
 
12  
-from django.db import models
13  
-from django.contrib.contenttypes.models import ContentType
14 12
 from django.contrib.contenttypes import generic
  13
+from django.contrib.contenttypes.models import ContentType
  14
+from django.db import models
  15
+
15 16
 
16 17
 class TaggedItem(models.Model):
17 18
     """A tag on an item."""
@@ -77,170 +78,3 @@ class Mineral(models.Model):
77 78
 
78 79
     def __unicode__(self):
79 80
         return self.name
80  
-
81  
-__test__ = {'API_TESTS':"""
82  
-# Create the world in 7 lines of code...
83  
->>> lion = Animal(common_name="Lion", latin_name="Panthera leo")
84  
->>> platypus = Animal(common_name="Platypus", latin_name="Ornithorhynchus anatinus")
85  
->>> eggplant = Vegetable(name="Eggplant", is_yucky=True)
86  
->>> bacon = Vegetable(name="Bacon", is_yucky=False)
87  
->>> quartz = Mineral(name="Quartz", hardness=7)
88  
->>> for o in (platypus, lion, eggplant, bacon, quartz):
89  
-...     o.save()
90  
-
91  
-# Objects with declared GenericRelations can be tagged directly -- the API
92  
-# mimics the many-to-many API.
93  
->>> bacon.tags.create(tag="fatty")
94  
-<TaggedItem: fatty>
95  
->>> bacon.tags.create(tag="salty")
96  
-<TaggedItem: salty>
97  
->>> lion.tags.create(tag="yellow")
98  
-<TaggedItem: yellow>
99  
->>> lion.tags.create(tag="hairy")
100  
-<TaggedItem: hairy>
101  
->>> platypus.tags.create(tag="fatty")
102  
-<TaggedItem: fatty>
103  
-
104  
->>> lion.tags.all()
105  
-[<TaggedItem: hairy>, <TaggedItem: yellow>]
106  
->>> bacon.tags.all()
107  
-[<TaggedItem: fatty>, <TaggedItem: salty>]
108  
-
109  
-# You can easily access the content object like a foreign key.
110  
->>> t = TaggedItem.objects.get(tag="salty")
111  
->>> t.content_object
112  
-<Vegetable: Bacon>
113  
-
114  
-# Recall that the Mineral class doesn't have an explicit GenericRelation
115  
-# defined. That's OK, because you can create TaggedItems explicitly.
116  
->>> tag1 = TaggedItem(content_object=quartz, tag="shiny")
117  
->>> tag2 = TaggedItem(content_object=quartz, tag="clearish")
118  
->>> tag1.save()
119  
->>> tag2.save()
120  
-
121  
-# However, excluding GenericRelations means your lookups have to be a bit more
122  
-# explicit.
123  
->>> from django.contrib.contenttypes.models import ContentType
124  
->>> ctype = ContentType.objects.get_for_model(quartz)
125  
->>> TaggedItem.objects.filter(content_type__pk=ctype.id, object_id=quartz.id)
126  
-[<TaggedItem: clearish>, <TaggedItem: shiny>]
127  
-
128  
-# You can set a generic foreign key in the way you'd expect.
129  
->>> tag1.content_object = platypus
130  
->>> tag1.save()
131  
->>> platypus.tags.all()
132  
-[<TaggedItem: fatty>, <TaggedItem: shiny>]
133  
->>> TaggedItem.objects.filter(content_type__pk=ctype.id, object_id=quartz.id)
134  
-[<TaggedItem: clearish>]
135  
-
136  
-# Queries across generic relations respect the content types. Even though there are two TaggedItems with a tag of "fatty", this query only pulls out the one with the content type related to Animals.
137  
->>> Animal.objects.order_by('common_name')
138  
-[<Animal: Lion>, <Animal: Platypus>]
139  
->>> Animal.objects.filter(tags__tag='fatty')
140  
-[<Animal: Platypus>]
141  
->>> Animal.objects.exclude(tags__tag='fatty')
142  
-[<Animal: Lion>]
143  
-
144  
-# If you delete an object with an explicit Generic relation, the related
145  
-# objects are deleted when the source object is deleted.
146  
-# Original list of tags:
147  
->>> [(t.tag, t.content_type, t.object_id) for t in TaggedItem.objects.all()]
148  
-[(u'clearish', <ContentType: mineral>, 1), (u'fatty', <ContentType: vegetable>, 2), (u'fatty', <ContentType: animal>, 1), (u'hairy', <ContentType: animal>, 2), (u'salty', <ContentType: vegetable>, 2), (u'shiny', <ContentType: animal>, 1), (u'yellow', <ContentType: animal>, 2)]
149  
-
150  
->>> lion.delete()
151  
->>> [(t.tag, t.content_type, t.object_id) for t in TaggedItem.objects.all()]
152  
-[(u'clearish', <ContentType: mineral>, 1), (u'fatty', <ContentType: vegetable>, 2), (u'fatty', <ContentType: animal>, 1), (u'salty', <ContentType: vegetable>, 2), (u'shiny', <ContentType: animal>, 1)]
153  
-
154  
-# If Generic Relation is not explicitly defined, any related objects
155  
-# remain after deletion of the source object.
156  
->>> quartz.delete()
157  
->>> [(t.tag, t.content_type, t.object_id) for t in TaggedItem.objects.all()]
158  
-[(u'clearish', <ContentType: mineral>, 1), (u'fatty', <ContentType: vegetable>, 2), (u'fatty', <ContentType: animal>, 1), (u'salty', <ContentType: vegetable>, 2), (u'shiny', <ContentType: animal>, 1)]
159  
-
160  
-# If you delete a tag, the objects using the tag are unaffected
161  
-# (other than losing a tag)
162  
->>> tag = TaggedItem.objects.get(id=1)
163  
->>> tag.delete()
164  
->>> bacon.tags.all()
165  
-[<TaggedItem: salty>]
166  
->>> [(t.tag, t.content_type, t.object_id) for t in TaggedItem.objects.all()]
167  
-[(u'clearish', <ContentType: mineral>, 1), (u'fatty', <ContentType: animal>, 1), (u'salty', <ContentType: vegetable>, 2), (u'shiny', <ContentType: animal>, 1)]
168  
-
169  
->>> TaggedItem.objects.filter(tag='fatty').delete()
170  
-
171  
->>> ctype = ContentType.objects.get_for_model(lion)
172  
->>> Animal.objects.filter(tags__content_type=ctype)
173  
-[<Animal: Platypus>]
174  
-
175  
-# Simple tests for multiple GenericForeignKeys
176  
-# only uses one model, since the above tests should be sufficient.
177  
->>> tiger, cheetah, bear = Animal(common_name="tiger"), Animal(common_name="cheetah"), Animal(common_name="bear")
178  
->>> for o in [tiger, cheetah, bear]: o.save()
179  
-
180  
-# Create directly
181  
->>> Comparison(first_obj=cheetah, other_obj=tiger, comparative="faster").save()
182  
->>> Comparison(first_obj=tiger, other_obj=cheetah, comparative="cooler").save()
183  
-
184  
-# Create using GenericRelation
185  
->>> tiger.comparisons.create(other_obj=bear, comparative="cooler")
186  
-<Comparison: tiger is cooler than bear>
187  
->>> tiger.comparisons.create(other_obj=cheetah, comparative="stronger")
188  
-<Comparison: tiger is stronger than cheetah>
189  
-
190  
->>> cheetah.comparisons.all()
191  
-[<Comparison: cheetah is faster than tiger>]
192  
-
193  
-# Filtering works
194  
->>> tiger.comparisons.filter(comparative="cooler")
195  
-[<Comparison: tiger is cooler than cheetah>, <Comparison: tiger is cooler than bear>]
196  
-
197  
-# Filtering and deleting works
198  
->>> subjective = ["cooler"]
199  
->>> tiger.comparisons.filter(comparative__in=subjective).delete()
200  
->>> Comparison.objects.all()
201  
-[<Comparison: cheetah is faster than tiger>, <Comparison: tiger is stronger than cheetah>]
202  
-
203  
-# If we delete cheetah, Comparisons with cheetah as 'first_obj' will be deleted
204  
-# since Animal has an explicit GenericRelation to Comparison through first_obj.
205  
-# Comparisons with cheetah as 'other_obj' will not be deleted.
206  
->>> cheetah.delete()
207  
->>> Comparison.objects.all()
208  
-[<Comparison: tiger is stronger than None>]
209  
-
210  
-# GenericForeignKey should work with subclasses (see #8309)
211  
->>> quartz = Mineral.objects.create(name="Quartz", hardness=7)
212  
->>> valuedtag = ValuableTaggedItem(content_object=quartz, tag="shiny", value=10)
213  
->>> valuedtag.save()
214  
->>> valuedtag.content_object
215  
-<Mineral: Quartz>
216  
-
217  
-# GenericInlineFormSet tests ##################################################
218  
-
219  
->>> from django.contrib.contenttypes.generic import generic_inlineformset_factory
220  
-
221  
->>> GenericFormSet = generic_inlineformset_factory(TaggedItem, extra=1)
222  
->>> formset = GenericFormSet()
223  
->>> for form in formset.forms:
224  
-...     print form.as_p()
225  
-<p><label for="id_generic_relations-taggeditem-content_type-object_id-0-tag">Tag:</label> <input id="id_generic_relations-taggeditem-content_type-object_id-0-tag" type="text" name="generic_relations-taggeditem-content_type-object_id-0-tag" maxlength="50" /></p> 
226  
-<p><label for="id_generic_relations-taggeditem-content_type-object_id-0-DELETE">Delete:</label> <input type="checkbox" name="generic_relations-taggeditem-content_type-object_id-0-DELETE" id="id_generic_relations-taggeditem-content_type-object_id-0-DELETE" /><input type="hidden" name="generic_relations-taggeditem-content_type-object_id-0-id" id="id_generic_relations-taggeditem-content_type-object_id-0-id" /></p>
227  
->>> formset = GenericFormSet(instance=Animal())
228  
->>> for form in formset.forms:
229  
-...     print form.as_p()
230  
-<p><label for="id_generic_relations-taggeditem-content_type-object_id-0-tag">Tag:</label> <input id="id_generic_relations-taggeditem-content_type-object_id-0-tag" type="text" name="generic_relations-taggeditem-content_type-object_id-0-tag" maxlength="50" /></p>
231  
-<p><label for="id_generic_relations-taggeditem-content_type-object_id-0-DELETE">Delete:</label> <input type="checkbox" name="generic_relations-taggeditem-content_type-object_id-0-DELETE" id="id_generic_relations-taggeditem-content_type-object_id-0-DELETE" /><input type="hidden" name="generic_relations-taggeditem-content_type-object_id-0-id" id="id_generic_relations-taggeditem-content_type-object_id-0-id" /></p>
232  
-
233  
->>> formset = GenericFormSet(instance=platypus)
234  
->>> for form in formset.forms:
235  
-...     print form.as_p()
236  
-<p><label for="id_generic_relations-taggeditem-content_type-object_id-0-tag">Tag:</label> <input id="id_generic_relations-taggeditem-content_type-object_id-0-tag" type="text" name="generic_relations-taggeditem-content_type-object_id-0-tag" value="shiny" maxlength="50" /></p>
237  
-<p><label for="id_generic_relations-taggeditem-content_type-object_id-0-DELETE">Delete:</label> <input type="checkbox" name="generic_relations-taggeditem-content_type-object_id-0-DELETE" id="id_generic_relations-taggeditem-content_type-object_id-0-DELETE" /><input type="hidden" name="generic_relations-taggeditem-content_type-object_id-0-id" value="..." id="id_generic_relations-taggeditem-content_type-object_id-0-id" /></p>
238  
-<p><label for="id_generic_relations-taggeditem-content_type-object_id-1-tag">Tag:</label> <input id="id_generic_relations-taggeditem-content_type-object_id-1-tag" type="text" name="generic_relations-taggeditem-content_type-object_id-1-tag" maxlength="50" /></p>
239  
-<p><label for="id_generic_relations-taggeditem-content_type-object_id-1-DELETE">Delete:</label> <input type="checkbox" name="generic_relations-taggeditem-content_type-object_id-1-DELETE" id="id_generic_relations-taggeditem-content_type-object_id-1-DELETE" /><input type="hidden" name="generic_relations-taggeditem-content_type-object_id-1-id" id="id_generic_relations-taggeditem-content_type-object_id-1-id" /></p>
240  
-
241  
->>> formset = GenericFormSet(instance=lion, prefix='x')
242  
->>> for form in formset.forms:
243  
-...     print form.as_p()
244  
-<p><label for="id_x-0-tag">Tag:</label> <input id="id_x-0-tag" type="text" name="x-0-tag" maxlength="50" /></p>
245  
-<p><label for="id_x-0-DELETE">Delete:</label> <input type="checkbox" name="x-0-DELETE" id="id_x-0-DELETE" /><input type="hidden" name="x-0-id" id="id_x-0-id" /></p>
246  
-"""}
223  tests/modeltests/generic_relations/tests.py
... ...
@@ -0,0 +1,223 @@
  1
+from django.contrib.contenttypes.generic import generic_inlineformset_factory
  2
+from django.contrib.contenttypes.models import ContentType
  3
+from django.test import TestCase
  4
+
  5
+from models import (TaggedItem, ValuableTaggedItem, Comparison, Animal,
  6
+    Vegetable, Mineral)
  7
+
  8
+
  9
+class GenericRelationsTests(TestCase):
  10
+    def test_generic_relations(self):
  11
+        # Create the world in 7 lines of code...
  12
+        lion = Animal.objects.create(common_name="Lion", latin_name="Panthera leo")
  13
+        platypus = Animal.objects.create(
  14
+            common_name="Platypus", latin_name="Ornithorhynchus anatinus"
  15
+        )
  16
+        eggplant = Vegetable.objects.create(name="Eggplant", is_yucky=True)
  17
+        bacon = Vegetable.objects.create(name="Bacon", is_yucky=False)
  18
+        quartz = Mineral.objects.create(name="Quartz", hardness=7)
  19
+
  20
+        # Objects with declared GenericRelations can be tagged directly -- the
  21
+        # API mimics the many-to-many API.
  22
+        bacon.tags.create(tag="fatty")
  23
+        bacon.tags.create(tag="salty")
  24
+        lion.tags.create(tag="yellow")
  25
+        lion.tags.create(tag="hairy")
  26
+        platypus.tags.create(tag="fatty")
  27
+        self.assertQuerysetEqual(lion.tags.all(), [
  28
+            "<TaggedItem: hairy>",
  29
+            "<TaggedItem: yellow>"
  30
+        ])
  31
+        self.assertQuerysetEqual(bacon.tags.all(), [
  32
+            "<TaggedItem: fatty>",
  33
+            "<TaggedItem: salty>"
  34
+        ])
  35
+
  36
+        # You can easily access the content object like a foreign key.
  37
+        t = TaggedItem.objects.get(tag="salty")
  38
+        self.assertEqual(t.content_object, bacon)
  39
+
  40
+        # Recall that the Mineral class doesn't have an explicit GenericRelation
  41
+        # defined. That's OK, because you can create TaggedItems explicitly.
  42
+        tag1 = TaggedItem.objects.create(content_object=quartz, tag="shiny")
  43
+        tag2 = TaggedItem.objects.create(content_object=quartz, tag="clearish")
  44
+
  45
+        # However, excluding GenericRelations means your lookups have to be a
  46
+        # bit more explicit.
  47
+        ctype = ContentType.objects.get_for_model(quartz)
  48
+        q = TaggedItem.objects.filter(
  49
+            content_type__pk=ctype.id, object_id=quartz.id
  50
+        )
  51
+        self.assertQuerysetEqual(q, [
  52
+            "<TaggedItem: clearish>",
  53
+            "<TaggedItem: shiny>"
  54
+        ])
  55
+
  56
+        # You can set a generic foreign key in the way you'd expect.
  57
+        tag1.content_object = platypus
  58
+        tag1.save()
  59
+        self.assertQuerysetEqual(platypus.tags.all(), [
  60
+            "<TaggedItem: fatty>",
  61
+            "<TaggedItem: shiny>"
  62
+        ])
  63
+        q = TaggedItem.objects.filter(
  64
+            content_type__pk=ctype.id, object_id=quartz.id
  65
+        )
  66
+        self.assertQuerysetEqual(q, ["<TaggedItem: clearish>"])
  67
+
  68
+        # Queries across generic relations respect the content types. Even
  69
+        # though there are two TaggedItems with a tag of "fatty", this query
  70
+        # only pulls out the one with the content type related to Animals.
  71
+        self.assertQuerysetEqual(Animal.objects.order_by('common_name'), [
  72
+            "<Animal: Lion>",
  73
+            "<Animal: Platypus>"
  74
+        ])
  75
+        self.assertQuerysetEqual(Animal.objects.filter(tags__tag='fatty'), [
  76
+            "<Animal: Platypus>"
  77
+        ])
  78
+        self.assertQuerysetEqual(Animal.objects.exclude(tags__tag='fatty'), [
  79
+            "<Animal: Lion>"
  80
+        ])
  81
+
  82
+        # If you delete an object with an explicit Generic relation, the related
  83
+        # objects are deleted when the source object is deleted.
  84
+        # Original list of tags:
  85
+        comp_func = lambda obj: (
  86
+            obj.tag, obj.content_type.model_class(), obj.object_id
  87
+        )
  88
+
  89
+        self.assertQuerysetEqual(TaggedItem.objects.all(), [
  90
+                (u'clearish', Mineral, quartz.pk),
  91
+                (u'fatty', Vegetable, bacon.pk),
  92
+                (u'fatty', Animal, platypus.pk),
  93
+                (u'hairy', Animal, lion.pk),
  94
+                (u'salty', Vegetable, bacon.pk),
  95
+                (u'shiny', Animal, platypus.pk),
  96
+                (u'yellow', Animal, lion.pk)
  97
+            ],
  98
+            comp_func
  99
+        )
  100
+        lion.delete()
  101
+        self.assertQuerysetEqual(TaggedItem.objects.all(), [
  102
+                (u'clearish', Mineral, quartz.pk),
  103
+                (u'fatty', Vegetable, bacon.pk),
  104
+                (u'fatty', Animal, platypus.pk),
  105
+                (u'salty', Vegetable, bacon.pk),
  106
+                (u'shiny', Animal, platypus.pk)
  107
+            ],
  108
+            comp_func
  109
+        )
  110
+
  111
+        # If Generic Relation is not explicitly defined, any related objects
  112
+        # remain after deletion of the source object.
  113
+        quartz_pk = quartz.pk
  114
+        quartz.delete()
  115
+        self.assertQuerysetEqual(TaggedItem.objects.all(), [
  116
+                (u'clearish', Mineral, quartz_pk),
  117
+                (u'fatty', Vegetable, bacon.pk),
  118
+                (u'fatty', Animal, platypus.pk),
  119
+                (u'salty', Vegetable, bacon.pk),
  120
+                (u'shiny', Animal, platypus.pk)
  121
+            ],
  122
+            comp_func
  123
+        )
  124
+        # If you delete a tag, the objects using the tag are unaffected
  125
+        # (other than losing a tag)
  126
+        tag = TaggedItem.objects.get(id=1)
  127
+        tag.delete()
  128
+        self.assertQuerysetEqual(bacon.tags.all(), ["<TaggedItem: salty>"])
  129
+        self.assertQuerysetEqual(TaggedItem.objects.all(), [
  130
+                (u'clearish', Mineral, quartz_pk),
  131
+                (u'fatty', Animal, platypus.pk),
  132
+                (u'salty', Vegetable, bacon.pk),
  133
+                (u'shiny', Animal, platypus.pk)
  134
+            ],
  135
+            comp_func
  136
+        )
  137
+        TaggedItem.objects.filter(tag='fatty').delete()
  138
+        ctype = ContentType.objects.get_for_model(lion)
  139
+        self.assertQuerysetEqual(Animal.objects.filter(tags__content_type=ctype), [
  140
+            "<Animal: Platypus>"
  141
+        ])
  142
+
  143
+
  144
+    def test_multiple_gfk(self):
  145
+        # Simple tests for multiple GenericForeignKeys
  146
+        # only uses one model, since the above tests should be sufficient.
  147
+        tiger = Animal.objects.create(common_name="tiger")
  148
+        cheetah = Animal.objects.create(common_name="cheetah")
  149
+        bear = Animal.objects.create(common_name="bear")
  150
+
  151
+        # Create directly
  152
+        Comparison.objects.create(
  153
+            first_obj=cheetah, other_obj=tiger, comparative="faster"
  154
+        )
  155
+        Comparison.objects.create(
  156
+            first_obj=tiger, other_obj=cheetah, comparative="cooler"
  157
+        )
  158
+
  159
+        # Create using GenericRelation
  160
+        tiger.comparisons.create(other_obj=bear, comparative="cooler")
  161
+        tiger.comparisons.create(other_obj=cheetah, comparative="stronger")
  162
+        self.assertQuerysetEqual(cheetah.comparisons.all(), [
  163
+            "<Comparison: cheetah is faster than tiger>"
  164
+        ])
  165
+
  166
+        # Filtering works
  167
+        self.assertQuerysetEqual(tiger.comparisons.filter(comparative="cooler"), [
  168
+            "<Comparison: tiger is cooler than cheetah>",
  169
+            "<Comparison: tiger is cooler than bear>"
  170
+        ])
  171
+
  172
+        # Filtering and deleting works
  173
+        subjective = ["cooler"]
  174
+        tiger.comparisons.filter(comparative__in=subjective).delete()
  175
+        self.assertQuerysetEqual(Comparison.objects.all(), [
  176
+            "<Comparison: cheetah is faster than tiger>",
  177
+            "<Comparison: tiger is stronger than cheetah>"
  178
+        ])
  179
+
  180
+        # If we delete cheetah, Comparisons with cheetah as 'first_obj' will be
  181
+        # deleted since Animal has an explicit GenericRelation to Comparison
  182
+        # through first_obj. Comparisons with cheetah as 'other_obj' will not
  183
+        # be deleted.
  184
+        cheetah.delete()
  185
+        self.assertQuerysetEqual(Comparison.objects.all(), [
  186
+            "<Comparison: tiger is stronger than None>"
  187
+        ])
  188
+
  189
+    def test_gfk_subclasses(self):
  190
+        # GenericForeignKey should work with subclasses (see #8309)
  191
+        quartz = Mineral.objects.create(name="Quartz", hardness=7)
  192
+        valuedtag = ValuableTaggedItem.objects.create(
  193
+            content_object=quartz, tag="shiny", value=10
  194
+        )
  195
+        self.assertEqual(valuedtag.content_object, quartz)
  196
+
  197
+    def test_generic_inline_formsets(self):
  198
+        GenericFormSet = generic_inlineformset_factory(TaggedItem, extra=1)
  199
+        formset = GenericFormSet()
  200
+        self.assertEqual(u''.join(form.as_p() for form in formset.forms), u"""<p><label for="id_generic_relations-taggeditem-content_type-object_id-0-tag">Tag:</label> <input id="id_generic_relations-taggeditem-content_type-object_id-0-tag" type="text" name="generic_relations-taggeditem-content_type-object_id-0-tag" maxlength="50" /></p>
  201
+<p><label for="id_generic_relations-taggeditem-content_type-object_id-0-DELETE">Delete:</label> <input type="checkbox" name="generic_relations-taggeditem-content_type-object_id-0-DELETE" id="id_generic_relations-taggeditem-content_type-object_id-0-DELETE" /><input type="hidden" name="generic_relations-taggeditem-content_type-object_id-0-id" id="id_generic_relations-taggeditem-content_type-object_id-0-id" /></p>""")
  202
+
  203
+        formset = GenericFormSet(instance=Animal())
  204
+        self.assertEqual(u''.join(form.as_p() for form in formset.forms), u"""<p><label for="id_generic_relations-taggeditem-content_type-object_id-0-tag">Tag:</label> <input id="id_generic_relations-taggeditem-content_type-object_id-0-tag" type="text" name="generic_relations-taggeditem-content_type-object_id-0-tag" maxlength="50" /></p>
  205
+<p><label for="id_generic_relations-taggeditem-content_type-object_id-0-DELETE">Delete:</label> <input type="checkbox" name="generic_relations-taggeditem-content_type-object_id-0-DELETE" id="id_generic_relations-taggeditem-content_type-object_id-0-DELETE" /><input type="hidden" name="generic_relations-taggeditem-content_type-object_id-0-id" id="id_generic_relations-taggeditem-content_type-object_id-0-id" /></p>""")
  206
+
  207
+        platypus = Animal.objects.create(
  208
+            common_name="Platypus", latin_name="Ornithorhynchus anatinus"
  209
+        )
  210
+        platypus.tags.create(tag="shiny")
  211
+        GenericFormSet = generic_inlineformset_factory(TaggedItem, extra=1)
  212
+        formset = GenericFormSet(instance=platypus)
  213
+        tagged_item_id = TaggedItem.objects.get(
  214
+            tag='shiny', object_id=platypus.id
  215
+        ).id
  216
+        self.assertEqual(u''.join(form.as_p() for form in formset.forms), u"""<p><label for="id_generic_relations-taggeditem-content_type-object_id-0-tag">Tag:</label> <input id="id_generic_relations-taggeditem-content_type-object_id-0-tag" type="text" name="generic_relations-taggeditem-content_type-object_id-0-tag" value="shiny" maxlength="50" /></p>
  217
+<p><label for="id_generic_relations-taggeditem-content_type-object_id-0-DELETE">Delete:</label> <input type="checkbox" name="generic_relations-taggeditem-content_type-object_id-0-DELETE" id="id_generic_relations-taggeditem-content_type-object_id-0-DELETE" /><input type="hidden" name="generic_relations-taggeditem-content_type-object_id-0-id" value="%s" id="id_generic_relations-taggeditem-content_type-object_id-0-id" /></p><p><label for="id_generic_relations-taggeditem-content_type-object_id-1-tag">Tag:</label> <input id="id_generic_relations-taggeditem-content_type-object_id-1-tag" type="text" name="generic_relations-taggeditem-content_type-object_id-1-tag" maxlength="50" /></p>
  218
+<p><label for="id_generic_relations-taggeditem-content_type-object_id-1-DELETE">Delete:</label> <input type="checkbox" name="generic_relations-taggeditem-content_type-object_id-1-DELETE" id="id_generic_relations-taggeditem-content_type-object_id-1-DELETE" /><input type="hidden" name="generic_relations-taggeditem-content_type-object_id-1-id" id="id_generic_relations-taggeditem-content_type-object_id-1-id" /></p>""" % tagged_item_id)
  219
+
  220
+        lion = Animal.objects.create(common_name="Lion", latin_name="Panthera leo")
  221
+        formset = GenericFormSet(instance=lion, prefix='x')
  222
+        self.assertEqual(u''.join(form.as_p() for form in formset.forms), u"""<p><label for="id_x-0-tag">Tag:</label> <input id="id_x-0-tag" type="text" name="x-0-tag" maxlength="50" /></p>
  223
+<p><label for="id_x-0-DELETE">Delete:</label> <input type="checkbox" name="x-0-DELETE" id="id_x-0-DELETE" /><input type="hidden" name="x-0-id" id="id_x-0-id" /></p>""")

0 notes on commit 3c469e9

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