/tests/regressiontests/views/tests/generic/create_update.py

https://code.google.com/p/mango-py/ · Python · 211 lines · 137 code · 26 blank · 48 comment · 4 complexity · 2cc40d377b01387e416e0ba244840c23 MD5 · raw file

  1. import datetime
  2. from django.test import TestCase
  3. from django.core.exceptions import ImproperlyConfigured
  4. from regressiontests.views.models import Article, UrlArticle
  5. class CreateObjectTest(TestCase):
  6. fixtures = ['testdata.json']
  7. def test_login_required_view(self):
  8. """
  9. Verifies that an unauthenticated user attempting to access a
  10. login_required view gets redirected to the login page and that
  11. an authenticated user is let through.
  12. """
  13. view_url = '/views/create_update/member/create/article/'
  14. response = self.client.get(view_url)
  15. self.assertRedirects(response, '/accounts/login/?next=%s' % view_url)
  16. # Now login and try again.
  17. login = self.client.login(username='testclient', password='password')
  18. self.assertTrue(login, 'Could not log in')
  19. response = self.client.get(view_url)
  20. self.assertEqual(response.status_code, 200)
  21. self.assertTemplateUsed(response, 'views/article_form.html')
  22. def test_create_article_display_page(self):
  23. """
  24. Ensures the generic view returned the page and contains a form.
  25. """
  26. view_url = '/views/create_update/create/article/'
  27. response = self.client.get(view_url)
  28. self.assertEqual(response.status_code, 200)
  29. self.assertTemplateUsed(response, 'views/article_form.html')
  30. if not response.context.get('form'):
  31. self.fail('No form found in the response.')
  32. def test_create_article_with_errors(self):
  33. """
  34. POSTs a form that contains validation errors.
  35. """
  36. view_url = '/views/create_update/create/article/'
  37. num_articles = Article.objects.count()
  38. response = self.client.post(view_url, {
  39. 'title': 'My First Article',
  40. })
  41. self.assertFormError(response, 'form', 'slug', [u'This field is required.'])
  42. self.assertTemplateUsed(response, 'views/article_form.html')
  43. self.assertEqual(num_articles, Article.objects.count(),
  44. "Number of Articles should not have changed.")
  45. def test_create_custom_save_article(self):
  46. """
  47. Creates a new article using a custom form class with a save method
  48. that alters the slug entered.
  49. """
  50. view_url = '/views/create_update/create_custom/article/'
  51. response = self.client.post(view_url, {
  52. 'title': 'Test Article',
  53. 'slug': 'this-should-get-replaced',
  54. 'author': 1,
  55. 'date_created': datetime.datetime(2007, 6, 25),
  56. })
  57. self.assertRedirects(response,
  58. '/views/create_update/view/article/some-other-slug/',
  59. target_status_code=404)
  60. class UpdateDeleteObjectTest(TestCase):
  61. fixtures = ['testdata.json']
  62. def test_update_object_form_display(self):
  63. """
  64. Verifies that the form was created properly and with initial values.
  65. """
  66. response = self.client.get('/views/create_update/update/article/old_article/')
  67. self.assertTemplateUsed(response, 'views/article_form.html')
  68. self.assertEqual(unicode(response.context['form']['title']),
  69. u'<input id="id_title" type="text" name="title" value="Old Article" maxlength="100" />')
  70. def test_update_object(self):
  71. """
  72. Verifies the updating of an Article.
  73. """
  74. response = self.client.post('/views/create_update/update/article/old_article/', {
  75. 'title': 'Another Article',
  76. 'slug': 'another-article-slug',
  77. 'author': 1,
  78. 'date_created': datetime.datetime(2007, 6, 25),
  79. })
  80. article = Article.objects.get(pk=1)
  81. self.assertEqual(article.title, "Another Article")
  82. def test_delete_object_confirm(self):
  83. """
  84. Verifies the confirm deletion page is displayed using a GET.
  85. """
  86. response = self.client.get('/views/create_update/delete/article/old_article/')
  87. self.assertTemplateUsed(response, 'views/article_confirm_delete.html')
  88. def test_delete_object(self):
  89. """
  90. Verifies the object actually gets deleted on a POST.
  91. """
  92. view_url = '/views/create_update/delete/article/old_article/'
  93. response = self.client.post(view_url)
  94. try:
  95. Article.objects.get(slug='old_article')
  96. except Article.DoesNotExist:
  97. pass
  98. else:
  99. self.fail('Object was not deleted.')
  100. class PostSaveRedirectTests(TestCase):
  101. """
  102. Verifies that the views redirect to the correct locations depending on
  103. if a post_save_redirect was passed and a get_absolute_url method exists
  104. on the Model.
  105. """
  106. fixtures = ['testdata.json']
  107. article_model = Article
  108. create_url = '/views/create_update/create/article/'
  109. update_url = '/views/create_update/update/article/old_article/'
  110. delete_url = '/views/create_update/delete/article/old_article/'
  111. create_redirect = '/views/create_update/view/article/my-first-article/'
  112. update_redirect = '/views/create_update/view/article/another-article-slug/'
  113. delete_redirect = '/views/create_update/'
  114. def test_create_article(self):
  115. num_articles = self.article_model.objects.count()
  116. response = self.client.post(self.create_url, {
  117. 'title': 'My First Article',
  118. 'slug': 'my-first-article',
  119. 'author': '1',
  120. 'date_created': datetime.datetime(2007, 6, 25),
  121. })
  122. self.assertRedirects(response, self.create_redirect,
  123. target_status_code=404)
  124. self.assertEqual(num_articles + 1, self.article_model.objects.count(),
  125. "A new Article should have been created.")
  126. def test_update_article(self):
  127. num_articles = self.article_model.objects.count()
  128. response = self.client.post(self.update_url, {
  129. 'title': 'Another Article',
  130. 'slug': 'another-article-slug',
  131. 'author': 1,
  132. 'date_created': datetime.datetime(2007, 6, 25),
  133. })
  134. self.assertRedirects(response, self.update_redirect,
  135. target_status_code=404)
  136. self.assertEqual(num_articles, self.article_model.objects.count(),
  137. "A new Article should not have been created.")
  138. def test_delete_article(self):
  139. num_articles = self.article_model.objects.count()
  140. response = self.client.post(self.delete_url)
  141. self.assertRedirects(response, self.delete_redirect,
  142. target_status_code=404)
  143. self.assertEqual(num_articles - 1, self.article_model.objects.count(),
  144. "An Article should have been deleted.")
  145. class NoPostSaveNoAbsoluteUrl(PostSaveRedirectTests):
  146. """
  147. Tests that when no post_save_redirect is passed and no get_absolute_url
  148. method exists on the Model that the view raises an ImproperlyConfigured
  149. error.
  150. """
  151. create_url = '/views/create_update/no_redirect/create/article/'
  152. update_url = '/views/create_update/no_redirect/update/article/old_article/'
  153. def test_create_article(self):
  154. self.assertRaises(ImproperlyConfigured,
  155. super(NoPostSaveNoAbsoluteUrl, self).test_create_article)
  156. def test_update_article(self):
  157. self.assertRaises(ImproperlyConfigured,
  158. super(NoPostSaveNoAbsoluteUrl, self).test_update_article)
  159. def test_delete_article(self):
  160. """
  161. The delete_object view requires a post_delete_redirect, so skip testing
  162. here.
  163. """
  164. pass
  165. class AbsoluteUrlNoPostSave(PostSaveRedirectTests):
  166. """
  167. Tests that the views redirect to the Model's get_absolute_url when no
  168. post_save_redirect is passed.
  169. """
  170. # Article model with get_absolute_url method.
  171. article_model = UrlArticle
  172. create_url = '/views/create_update/no_url/create/article/'
  173. update_url = '/views/create_update/no_url/update/article/old_article/'
  174. create_redirect = '/urlarticles/my-first-article/'
  175. update_redirect = '/urlarticles/another-article-slug/'
  176. def test_delete_article(self):
  177. """
  178. The delete_object view requires a post_delete_redirect, so skip testing
  179. here.
  180. """
  181. pass