PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/regressiontests/generic_inline_admin/tests.py

https://code.google.com/p/mango-py/
Python | 216 lines | 182 code | 12 blank | 22 comment | 0 complexity | 76d14895471d2ea0ec7c307e7eb065df MD5 | raw file
Possible License(s): BSD-3-Clause
  1. # coding: utf-8
  2. from django.conf import settings
  3. from django.contrib.contenttypes.generic import generic_inlineformset_factory
  4. from django.test import TestCase
  5. # local test models
  6. from models import Episode, EpisodeExtra, EpisodeMaxNum, EpisodeExclude, \
  7. Media, EpisodePermanent, MediaPermanentInline, Category
  8. class GenericAdminViewTest(TestCase):
  9. fixtures = ['users.xml']
  10. def setUp(self):
  11. # set TEMPLATE_DEBUG to True to ensure {% include %} will raise
  12. # exceptions since that is how inlines are rendered and #9498 will
  13. # bubble up if it is an issue.
  14. self.original_template_debug = settings.TEMPLATE_DEBUG
  15. settings.TEMPLATE_DEBUG = True
  16. self.client.login(username='super', password='secret')
  17. # Can't load content via a fixture (since the GenericForeignKey
  18. # relies on content type IDs, which will vary depending on what
  19. # other tests have been run), thus we do it here.
  20. e = Episode.objects.create(name='This Week in Django')
  21. self.episode_pk = e.pk
  22. m = Media(content_object=e, url='http://example.com/podcast.mp3')
  23. m.save()
  24. self.mp3_media_pk = m.pk
  25. m = Media(content_object=e, url='http://example.com/logo.png')
  26. m.save()
  27. self.png_media_pk = m.pk
  28. def tearDown(self):
  29. self.client.logout()
  30. settings.TEMPLATE_DEBUG = self.original_template_debug
  31. def testBasicAddGet(self):
  32. """
  33. A smoke test to ensure GET on the add_view works.
  34. """
  35. response = self.client.get('/generic_inline_admin/admin/generic_inline_admin/episode/add/')
  36. self.assertEqual(response.status_code, 200)
  37. def testBasicEditGet(self):
  38. """
  39. A smoke test to ensure GET on the change_view works.
  40. """
  41. response = self.client.get('/generic_inline_admin/admin/generic_inline_admin/episode/%d/' % self.episode_pk)
  42. self.assertEqual(response.status_code, 200)
  43. def testBasicAddPost(self):
  44. """
  45. A smoke test to ensure POST on add_view works.
  46. """
  47. post_data = {
  48. "name": u"This Week in Django",
  49. # inline data
  50. "generic_inline_admin-media-content_type-object_id-TOTAL_FORMS": u"1",
  51. "generic_inline_admin-media-content_type-object_id-INITIAL_FORMS": u"0",
  52. "generic_inline_admin-media-content_type-object_id-MAX_NUM_FORMS": u"0",
  53. }
  54. response = self.client.post('/generic_inline_admin/admin/generic_inline_admin/episode/add/', post_data)
  55. self.assertEqual(response.status_code, 302) # redirect somewhere
  56. def testBasicEditPost(self):
  57. """
  58. A smoke test to ensure POST on edit_view works.
  59. """
  60. post_data = {
  61. "name": u"This Week in Django",
  62. # inline data
  63. "generic_inline_admin-media-content_type-object_id-TOTAL_FORMS": u"3",
  64. "generic_inline_admin-media-content_type-object_id-INITIAL_FORMS": u"2",
  65. "generic_inline_admin-media-content_type-object_id-MAX_NUM_FORMS": u"0",
  66. "generic_inline_admin-media-content_type-object_id-0-id": u"%d" % self.mp3_media_pk,
  67. "generic_inline_admin-media-content_type-object_id-0-url": u"http://example.com/podcast.mp3",
  68. "generic_inline_admin-media-content_type-object_id-1-id": u"%d" % self.png_media_pk,
  69. "generic_inline_admin-media-content_type-object_id-1-url": u"http://example.com/logo.png",
  70. "generic_inline_admin-media-content_type-object_id-2-id": u"",
  71. "generic_inline_admin-media-content_type-object_id-2-url": u"",
  72. }
  73. url = '/generic_inline_admin/admin/generic_inline_admin/episode/%d/' % self.episode_pk
  74. response = self.client.post(url, post_data)
  75. self.assertEqual(response.status_code, 302) # redirect somewhere
  76. def testGenericInlineFormset(self):
  77. EpisodeMediaFormSet = generic_inlineformset_factory(Media, can_delete=False, extra=3)
  78. e = Episode.objects.get(name='This Week in Django')
  79. # Works with no queryset
  80. formset = EpisodeMediaFormSet(instance=e)
  81. self.assertEqual(len(formset.forms), 5)
  82. self.assertEqual(formset.forms[0].as_p(), '<p><label for="id_generic_inline_admin-media-content_type-object_id-0-url">Url:</label> <input id="id_generic_inline_admin-media-content_type-object_id-0-url" type="text" name="generic_inline_admin-media-content_type-object_id-0-url" value="http://example.com/podcast.mp3" maxlength="200" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-0-id" value="%s" id="id_generic_inline_admin-media-content_type-object_id-0-id" /></p>' % self.mp3_media_pk)
  83. self.assertEqual(formset.forms[1].as_p(), '<p><label for="id_generic_inline_admin-media-content_type-object_id-1-url">Url:</label> <input id="id_generic_inline_admin-media-content_type-object_id-1-url" type="text" name="generic_inline_admin-media-content_type-object_id-1-url" value="http://example.com/logo.png" maxlength="200" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-1-id" value="%s" id="id_generic_inline_admin-media-content_type-object_id-1-id" /></p>' % self.png_media_pk)
  84. self.assertEqual(formset.forms[2].as_p(), '<p><label for="id_generic_inline_admin-media-content_type-object_id-2-url">Url:</label> <input id="id_generic_inline_admin-media-content_type-object_id-2-url" type="text" name="generic_inline_admin-media-content_type-object_id-2-url" maxlength="200" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-2-id" id="id_generic_inline_admin-media-content_type-object_id-2-id" /></p>')
  85. # A queryset can be used to alter display ordering
  86. formset = EpisodeMediaFormSet(instance=e, queryset=Media.objects.order_by('url'))
  87. self.assertEqual(len(formset.forms), 5)
  88. self.assertEqual(formset.forms[0].as_p(), '<p><label for="id_generic_inline_admin-media-content_type-object_id-0-url">Url:</label> <input id="id_generic_inline_admin-media-content_type-object_id-0-url" type="text" name="generic_inline_admin-media-content_type-object_id-0-url" value="http://example.com/logo.png" maxlength="200" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-0-id" value="%s" id="id_generic_inline_admin-media-content_type-object_id-0-id" /></p>' % self.png_media_pk)
  89. self.assertEqual(formset.forms[1].as_p(), '<p><label for="id_generic_inline_admin-media-content_type-object_id-1-url">Url:</label> <input id="id_generic_inline_admin-media-content_type-object_id-1-url" type="text" name="generic_inline_admin-media-content_type-object_id-1-url" value="http://example.com/podcast.mp3" maxlength="200" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-1-id" value="%s" id="id_generic_inline_admin-media-content_type-object_id-1-id" /></p>' % self.mp3_media_pk)
  90. self.assertEqual(formset.forms[2].as_p(), '<p><label for="id_generic_inline_admin-media-content_type-object_id-2-url">Url:</label> <input id="id_generic_inline_admin-media-content_type-object_id-2-url" type="text" name="generic_inline_admin-media-content_type-object_id-2-url" maxlength="200" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-2-id" id="id_generic_inline_admin-media-content_type-object_id-2-id" /></p>')
  91. # Works with a queryset that omits items
  92. formset = EpisodeMediaFormSet(instance=e, queryset=Media.objects.filter(url__endswith=".png"))
  93. self.assertEqual(len(formset.forms), 4)
  94. self.assertEqual(formset.forms[0].as_p(), '<p><label for="id_generic_inline_admin-media-content_type-object_id-0-url">Url:</label> <input id="id_generic_inline_admin-media-content_type-object_id-0-url" type="text" name="generic_inline_admin-media-content_type-object_id-0-url" value="http://example.com/logo.png" maxlength="200" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-0-id" value="%s" id="id_generic_inline_admin-media-content_type-object_id-0-id" /></p>' % self.png_media_pk)
  95. self.assertEqual(formset.forms[1].as_p(), '<p><label for="id_generic_inline_admin-media-content_type-object_id-1-url">Url:</label> <input id="id_generic_inline_admin-media-content_type-object_id-1-url" type="text" name="generic_inline_admin-media-content_type-object_id-1-url" maxlength="200" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-1-id" id="id_generic_inline_admin-media-content_type-object_id-1-id" /></p>')
  96. def testGenericInlineFormsetFactory(self):
  97. # Regression test for #10522.
  98. inline_formset = generic_inlineformset_factory(Media,
  99. exclude=('url',))
  100. # Regression test for #12340.
  101. e = Episode.objects.get(name='This Week in Django')
  102. formset = inline_formset(instance=e)
  103. self.assertTrue(formset.get_queryset().ordered)
  104. class GenericInlineAdminParametersTest(TestCase):
  105. fixtures = ['users.xml']
  106. def setUp(self):
  107. self.client.login(username='super', password='secret')
  108. def tearDown(self):
  109. self.client.logout()
  110. def _create_object(self, model):
  111. """
  112. Create a model with an attached Media object via GFK. We can't
  113. load content via a fixture (since the GenericForeignKey relies on
  114. content type IDs, which will vary depending on what other tests
  115. have been run), thus we do it here.
  116. """
  117. e = model.objects.create(name='This Week in Django')
  118. Media.objects.create(content_object=e, url='http://example.com/podcast.mp3')
  119. return e
  120. def testNoParam(self):
  121. """
  122. With one initial form, extra (default) at 3, there should be 4 forms.
  123. """
  124. e = self._create_object(Episode)
  125. response = self.client.get('/generic_inline_admin/admin/generic_inline_admin/episode/%s/' % e.pk)
  126. formset = response.context['inline_admin_formsets'][0].formset
  127. self.assertEqual(formset.total_form_count(), 4)
  128. self.assertEqual(formset.initial_form_count(), 1)
  129. def testExtraParam(self):
  130. """
  131. With extra=0, there should be one form.
  132. """
  133. e = self._create_object(EpisodeExtra)
  134. response = self.client.get('/generic_inline_admin/admin/generic_inline_admin/episodeextra/%s/' % e.pk)
  135. formset = response.context['inline_admin_formsets'][0].formset
  136. self.assertEqual(formset.total_form_count(), 1)
  137. self.assertEqual(formset.initial_form_count(), 1)
  138. def testMaxNumParam(self):
  139. """
  140. With extra=5 and max_num=2, there should be only 2 forms.
  141. """
  142. e = self._create_object(EpisodeMaxNum)
  143. inline_form_data = '<input type="hidden" name="generic_inline_admin-media-content_type-object_id-TOTAL_FORMS" value="2" id="id_generic_inline_admin-media-content_type-object_id-TOTAL_FORMS" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-INITIAL_FORMS" value="1" id="id_generic_inline_admin-media-content_type-object_id-INITIAL_FORMS" />'
  144. response = self.client.get('/generic_inline_admin/admin/generic_inline_admin/episodemaxnum/%s/' % e.pk)
  145. formset = response.context['inline_admin_formsets'][0].formset
  146. self.assertEqual(formset.total_form_count(), 2)
  147. self.assertEqual(formset.initial_form_count(), 1)
  148. def testExcludeParam(self):
  149. """
  150. Generic inline formsets should respect include.
  151. """
  152. e = self._create_object(EpisodeExclude)
  153. response = self.client.get('/generic_inline_admin/admin/generic_inline_admin/episodeexclude/%s/' % e.pk)
  154. formset = response.context['inline_admin_formsets'][0].formset
  155. self.assertFalse('url' in formset.forms[0], 'The formset has excluded "url" field.')
  156. class GenericInlineAdminWithUniqueTogetherTest(TestCase):
  157. fixtures = ['users.xml']
  158. def setUp(self):
  159. self.client.login(username='super', password='secret')
  160. def tearDown(self):
  161. self.client.logout()
  162. def testAdd(self):
  163. category_id = Category.objects.create(name='male').pk
  164. post_data = {
  165. "name": u"John Doe",
  166. # inline data
  167. "generic_inline_admin-phonenumber-content_type-object_id-TOTAL_FORMS": u"1",
  168. "generic_inline_admin-phonenumber-content_type-object_id-INITIAL_FORMS": u"0",
  169. "generic_inline_admin-phonenumber-content_type-object_id-MAX_NUM_FORMS": u"0",
  170. "generic_inline_admin-phonenumber-content_type-object_id-0-id": "",
  171. "generic_inline_admin-phonenumber-content_type-object_id-0-phone_number": "555-555-5555",
  172. "generic_inline_admin-phonenumber-content_type-object_id-0-category": "%s" % category_id,
  173. }
  174. response = self.client.get('/generic_inline_admin/admin/generic_inline_admin/contact/add/')
  175. self.assertEqual(response.status_code, 200)
  176. response = self.client.post('/generic_inline_admin/admin/generic_inline_admin/contact/add/', post_data)
  177. self.assertEqual(response.status_code, 302) # redirect somewhere
  178. class NoInlineDeletionTest(TestCase):
  179. def test_no_deletion(self):
  180. fake_site = object()
  181. inline = MediaPermanentInline(EpisodePermanent, fake_site)
  182. fake_request = object()
  183. formset = inline.get_formset(fake_request)
  184. self.assertFalse(formset.can_delete)