PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/modeltests/generic_relations/tests.py

https://code.google.com/p/mango-py/
Python | 223 lines | 172 code | 21 blank | 30 comment | 0 complexity | 3e5432211f47199797bdcc202380a6aa MD5 | raw file
Possible License(s): BSD-3-Clause
  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. from models import (TaggedItem, ValuableTaggedItem, Comparison, Animal,
  5. Vegetable, Mineral)
  6. class GenericRelationsTests(TestCase):
  7. def test_generic_relations(self):
  8. # Create the world in 7 lines of code...
  9. lion = Animal.objects.create(common_name="Lion", latin_name="Panthera leo")
  10. platypus = Animal.objects.create(
  11. common_name="Platypus", latin_name="Ornithorhynchus anatinus"
  12. )
  13. eggplant = Vegetable.objects.create(name="Eggplant", is_yucky=True)
  14. bacon = Vegetable.objects.create(name="Bacon", is_yucky=False)
  15. quartz = Mineral.objects.create(name="Quartz", hardness=7)
  16. # Objects with declared GenericRelations can be tagged directly -- the
  17. # API mimics the many-to-many API.
  18. bacon.tags.create(tag="fatty")
  19. bacon.tags.create(tag="salty")
  20. lion.tags.create(tag="yellow")
  21. lion.tags.create(tag="hairy")
  22. platypus.tags.create(tag="fatty")
  23. self.assertQuerysetEqual(lion.tags.all(), [
  24. "<TaggedItem: hairy>",
  25. "<TaggedItem: yellow>"
  26. ])
  27. self.assertQuerysetEqual(bacon.tags.all(), [
  28. "<TaggedItem: fatty>",
  29. "<TaggedItem: salty>"
  30. ])
  31. # You can easily access the content object like a foreign key.
  32. t = TaggedItem.objects.get(tag="salty")
  33. self.assertEqual(t.content_object, bacon)
  34. # Recall that the Mineral class doesn't have an explicit GenericRelation
  35. # defined. That's OK, because you can create TaggedItems explicitly.
  36. tag1 = TaggedItem.objects.create(content_object=quartz, tag="shiny")
  37. tag2 = TaggedItem.objects.create(content_object=quartz, tag="clearish")
  38. # However, excluding GenericRelations means your lookups have to be a
  39. # bit more explicit.
  40. ctype = ContentType.objects.get_for_model(quartz)
  41. q = TaggedItem.objects.filter(
  42. content_type__pk=ctype.id, object_id=quartz.id
  43. )
  44. self.assertQuerysetEqual(q, [
  45. "<TaggedItem: clearish>",
  46. "<TaggedItem: shiny>"
  47. ])
  48. # You can set a generic foreign key in the way you'd expect.
  49. tag1.content_object = platypus
  50. tag1.save()
  51. self.assertQuerysetEqual(platypus.tags.all(), [
  52. "<TaggedItem: fatty>",
  53. "<TaggedItem: shiny>"
  54. ])
  55. q = TaggedItem.objects.filter(
  56. content_type__pk=ctype.id, object_id=quartz.id
  57. )
  58. self.assertQuerysetEqual(q, ["<TaggedItem: clearish>"])
  59. # Queries across generic relations respect the content types. Even
  60. # though there are two TaggedItems with a tag of "fatty", this query
  61. # only pulls out the one with the content type related to Animals.
  62. self.assertQuerysetEqual(Animal.objects.order_by('common_name'), [
  63. "<Animal: Lion>",
  64. "<Animal: Platypus>"
  65. ])
  66. self.assertQuerysetEqual(Animal.objects.filter(tags__tag='fatty'), [
  67. "<Animal: Platypus>"
  68. ])
  69. self.assertQuerysetEqual(Animal.objects.exclude(tags__tag='fatty'), [
  70. "<Animal: Lion>"
  71. ])
  72. # If you delete an object with an explicit Generic relation, the related
  73. # objects are deleted when the source object is deleted.
  74. # Original list of tags:
  75. comp_func = lambda obj: (
  76. obj.tag, obj.content_type.model_class(), obj.object_id
  77. )
  78. self.assertQuerysetEqual(TaggedItem.objects.all(), [
  79. (u'clearish', Mineral, quartz.pk),
  80. (u'fatty', Animal, platypus.pk),
  81. (u'fatty', Vegetable, bacon.pk),
  82. (u'hairy', Animal, lion.pk),
  83. (u'salty', Vegetable, bacon.pk),
  84. (u'shiny', Animal, platypus.pk),
  85. (u'yellow', Animal, lion.pk)
  86. ],
  87. comp_func
  88. )
  89. lion.delete()
  90. self.assertQuerysetEqual(TaggedItem.objects.all(), [
  91. (u'clearish', Mineral, quartz.pk),
  92. (u'fatty', Animal, platypus.pk),
  93. (u'fatty', Vegetable, bacon.pk),
  94. (u'salty', Vegetable, bacon.pk),
  95. (u'shiny', Animal, platypus.pk)
  96. ],
  97. comp_func
  98. )
  99. # If Generic Relation is not explicitly defined, any related objects
  100. # remain after deletion of the source object.
  101. quartz_pk = quartz.pk
  102. quartz.delete()
  103. self.assertQuerysetEqual(TaggedItem.objects.all(), [
  104. (u'clearish', Mineral, quartz_pk),
  105. (u'fatty', Animal, platypus.pk),
  106. (u'fatty', Vegetable, bacon.pk),
  107. (u'salty', Vegetable, bacon.pk),
  108. (u'shiny', Animal, platypus.pk)
  109. ],
  110. comp_func
  111. )
  112. # If you delete a tag, the objects using the tag are unaffected
  113. # (other than losing a tag)
  114. tag = TaggedItem.objects.order_by("id")[0]
  115. tag.delete()
  116. self.assertQuerysetEqual(bacon.tags.all(), ["<TaggedItem: salty>"])
  117. self.assertQuerysetEqual(TaggedItem.objects.all(), [
  118. (u'clearish', Mineral, quartz_pk),
  119. (u'fatty', Animal, platypus.pk),
  120. (u'salty', Vegetable, bacon.pk),
  121. (u'shiny', Animal, platypus.pk)
  122. ],
  123. comp_func
  124. )
  125. TaggedItem.objects.filter(tag='fatty').delete()
  126. ctype = ContentType.objects.get_for_model(lion)
  127. self.assertQuerysetEqual(Animal.objects.filter(tags__content_type=ctype), [
  128. "<Animal: Platypus>"
  129. ])
  130. def test_multiple_gfk(self):
  131. # Simple tests for multiple GenericForeignKeys
  132. # only uses one model, since the above tests should be sufficient.
  133. tiger = Animal.objects.create(common_name="tiger")
  134. cheetah = Animal.objects.create(common_name="cheetah")
  135. bear = Animal.objects.create(common_name="bear")
  136. # Create directly
  137. Comparison.objects.create(
  138. first_obj=cheetah, other_obj=tiger, comparative="faster"
  139. )
  140. Comparison.objects.create(
  141. first_obj=tiger, other_obj=cheetah, comparative="cooler"
  142. )
  143. # Create using GenericRelation
  144. tiger.comparisons.create(other_obj=bear, comparative="cooler")
  145. tiger.comparisons.create(other_obj=cheetah, comparative="stronger")
  146. self.assertQuerysetEqual(cheetah.comparisons.all(), [
  147. "<Comparison: cheetah is faster than tiger>"
  148. ])
  149. # Filtering works
  150. self.assertQuerysetEqual(tiger.comparisons.filter(comparative="cooler"), [
  151. "<Comparison: tiger is cooler than cheetah>",
  152. "<Comparison: tiger is cooler than bear>"
  153. ])
  154. # Filtering and deleting works
  155. subjective = ["cooler"]
  156. tiger.comparisons.filter(comparative__in=subjective).delete()
  157. self.assertQuerysetEqual(Comparison.objects.all(), [
  158. "<Comparison: cheetah is faster than tiger>",
  159. "<Comparison: tiger is stronger than cheetah>"
  160. ])
  161. # If we delete cheetah, Comparisons with cheetah as 'first_obj' will be
  162. # deleted since Animal has an explicit GenericRelation to Comparison
  163. # through first_obj. Comparisons with cheetah as 'other_obj' will not
  164. # be deleted.
  165. cheetah.delete()
  166. self.assertQuerysetEqual(Comparison.objects.all(), [
  167. "<Comparison: tiger is stronger than None>"
  168. ])
  169. def test_gfk_subclasses(self):
  170. # GenericForeignKey should work with subclasses (see #8309)
  171. quartz = Mineral.objects.create(name="Quartz", hardness=7)
  172. valuedtag = ValuableTaggedItem.objects.create(
  173. content_object=quartz, tag="shiny", value=10
  174. )
  175. self.assertEqual(valuedtag.content_object, quartz)
  176. def test_generic_inline_formsets(self):
  177. GenericFormSet = generic_inlineformset_factory(TaggedItem, extra=1)
  178. formset = GenericFormSet()
  179. 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>
  180. <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>""")
  181. formset = GenericFormSet(instance=Animal())
  182. 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>
  183. <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>""")
  184. platypus = Animal.objects.create(
  185. common_name="Platypus", latin_name="Ornithorhynchus anatinus"
  186. )
  187. platypus.tags.create(tag="shiny")
  188. GenericFormSet = generic_inlineformset_factory(TaggedItem, extra=1)
  189. formset = GenericFormSet(instance=platypus)
  190. tagged_item_id = TaggedItem.objects.get(
  191. tag='shiny', object_id=platypus.id
  192. ).id
  193. 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>
  194. <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>
  195. <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)
  196. lion = Animal.objects.create(common_name="Lion", latin_name="Panthera leo")
  197. formset = GenericFormSet(instance=lion, prefix='x')
  198. 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>
  199. <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>""")