PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 2ms app.codeStats 0ms

/tests/regressiontests/generic_views/edit.py

https://code.google.com/p/mango-py/
Python | 280 lines | 240 code | 36 blank | 4 comment | 6 complexity | 2b4ea969b877dad3b3b09a2a5c8a91cd MD5 | raw file
Possible License(s): BSD-3-Clause
  1. from django.core.exceptions import ImproperlyConfigured
  2. from django.core.urlresolvers import reverse
  3. from django import forms
  4. from django.test import TestCase
  5. from django.utils.unittest import expectedFailure
  6. from regressiontests.generic_views.models import Artist, Author
  7. from regressiontests.generic_views import views
  8. class ModelFormMixinTests(TestCase):
  9. def test_get_form(self):
  10. form_class = views.AuthorGetQuerySetFormView().get_form_class()
  11. self.assertEqual(form_class.Meta.model, Author)
  12. class CreateViewTests(TestCase):
  13. urls = 'regressiontests.generic_views.urls'
  14. def test_create(self):
  15. res = self.client.get('/edit/authors/create/')
  16. self.assertEqual(res.status_code, 200)
  17. self.assertTrue(isinstance(res.context['form'], forms.ModelForm))
  18. self.assertFalse('object' in res.context)
  19. self.assertFalse('author' in res.context)
  20. self.assertTemplateUsed(res, 'generic_views/author_form.html')
  21. res = self.client.post('/edit/authors/create/',
  22. {'name': 'Randall Munroe', 'slug': 'randall-munroe'})
  23. self.assertEqual(res.status_code, 302)
  24. self.assertRedirects(res, 'http://testserver/list/authors/')
  25. self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
  26. def test_create_invalid(self):
  27. res = self.client.post('/edit/authors/create/',
  28. {'name': 'A' * 101, 'slug': 'randall-munroe'})
  29. self.assertEqual(res.status_code, 200)
  30. self.assertTemplateUsed(res, 'generic_views/author_form.html')
  31. self.assertEqual(len(res.context['form'].errors), 1)
  32. self.assertEqual(Author.objects.count(), 0)
  33. def test_create_with_object_url(self):
  34. res = self.client.post('/edit/artists/create/',
  35. {'name': 'Rene Magritte'})
  36. self.assertEqual(res.status_code, 302)
  37. artist = Artist.objects.get(name='Rene Magritte')
  38. self.assertRedirects(res, 'http://testserver/detail/artist/%d/' % artist.pk)
  39. self.assertQuerysetEqual(Artist.objects.all(), ['<Artist: Rene Magritte>'])
  40. def test_create_with_redirect(self):
  41. res = self.client.post('/edit/authors/create/redirect/',
  42. {'name': 'Randall Munroe', 'slug': 'randall-munroe'})
  43. self.assertEqual(res.status_code, 302)
  44. self.assertRedirects(res, 'http://testserver/edit/authors/create/')
  45. self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
  46. def test_create_with_interpolated_redirect(self):
  47. res = self.client.post('/edit/authors/create/interpolate_redirect/',
  48. {'name': 'Randall Munroe', 'slug': 'randall-munroe'})
  49. self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
  50. self.assertEqual(res.status_code, 302)
  51. pk = Author.objects.all()[0].pk
  52. self.assertRedirects(res, 'http://testserver/edit/author/%d/update/' % pk)
  53. def test_create_with_special_properties(self):
  54. res = self.client.get('/edit/authors/create/special/')
  55. self.assertEqual(res.status_code, 200)
  56. self.assertTrue(isinstance(res.context['form'], views.AuthorForm))
  57. self.assertFalse('object' in res.context)
  58. self.assertFalse('author' in res.context)
  59. self.assertTemplateUsed(res, 'generic_views/form.html')
  60. res = self.client.post('/edit/authors/create/special/',
  61. {'name': 'Randall Munroe', 'slug': 'randall-munroe'})
  62. self.assertEqual(res.status_code, 302)
  63. obj = Author.objects.get(slug='randall-munroe')
  64. self.assertRedirects(res, reverse('author_detail', kwargs={'pk': obj.pk}))
  65. self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
  66. def test_create_without_redirect(self):
  67. try:
  68. res = self.client.post('/edit/authors/create/naive/',
  69. {'name': 'Randall Munroe', 'slug': 'randall-munroe'})
  70. self.fail('Should raise exception -- No redirect URL provided, and no get_absolute_url provided')
  71. except ImproperlyConfigured:
  72. pass
  73. def test_create_restricted(self):
  74. res = self.client.post('/edit/authors/create/restricted/',
  75. {'name': 'Randall Munroe', 'slug': 'randall-munroe'})
  76. self.assertEqual(res.status_code, 302)
  77. self.assertRedirects(res, 'http://testserver/accounts/login/?next=/edit/authors/create/restricted/')
  78. class UpdateViewTests(TestCase):
  79. urls = 'regressiontests.generic_views.urls'
  80. def test_update_post(self):
  81. a = Author.objects.create(
  82. name='Randall Munroe',
  83. slug='randall-munroe',
  84. )
  85. res = self.client.get('/edit/author/%d/update/' % a.pk)
  86. self.assertEqual(res.status_code, 200)
  87. self.assertTrue(isinstance(res.context['form'], forms.ModelForm))
  88. self.assertEqual(res.context['object'], Author.objects.get(pk=a.pk))
  89. self.assertEqual(res.context['author'], Author.objects.get(pk=a.pk))
  90. self.assertTemplateUsed(res, 'generic_views/author_form.html')
  91. # Modification with both POST and PUT (browser compatible)
  92. res = self.client.post('/edit/author/%d/update/' % a.pk,
  93. {'name': 'Randall Munroe (xkcd)', 'slug': 'randall-munroe'})
  94. self.assertEqual(res.status_code, 302)
  95. self.assertRedirects(res, 'http://testserver/list/authors/')
  96. self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (xkcd)>'])
  97. @expectedFailure
  98. def test_update_put(self):
  99. a = Author.objects.create(
  100. name='Randall Munroe',
  101. slug='randall-munroe',
  102. )
  103. res = self.client.get('/edit/author/%d/update/' % a.pk)
  104. self.assertEqual(res.status_code, 200)
  105. self.assertTemplateUsed(res, 'generic_views/author_form.html')
  106. res = self.client.put('/edit/author/%d/update/' % a.pk,
  107. {'name': 'Randall Munroe (author of xkcd)', 'slug': 'randall-munroe'})
  108. self.assertEqual(res.status_code, 302)
  109. self.assertRedirects(res, 'http://testserver/list/authors/')
  110. self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (author of xkcd)>'])
  111. def test_update_invalid(self):
  112. a = Author.objects.create(
  113. name='Randall Munroe',
  114. slug='randall-munroe',
  115. )
  116. res = self.client.post('/edit/author/%d/update/' % a.pk,
  117. {'name': 'A' * 101, 'slug': 'randall-munroe'})
  118. self.assertEqual(res.status_code, 200)
  119. self.assertTemplateUsed(res, 'generic_views/author_form.html')
  120. self.assertEqual(len(res.context['form'].errors), 1)
  121. self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
  122. def test_update_with_object_url(self):
  123. a = Artist.objects.create(name='Rene Magritte')
  124. res = self.client.post('/edit/artists/%d/update/' % a.pk,
  125. {'name': 'Rene Magritte'})
  126. self.assertEqual(res.status_code, 302)
  127. self.assertRedirects(res, 'http://testserver/detail/artist/%d/' % a.pk)
  128. self.assertQuerysetEqual(Artist.objects.all(), ['<Artist: Rene Magritte>'])
  129. def test_update_with_redirect(self):
  130. a = Author.objects.create(
  131. name='Randall Munroe',
  132. slug='randall-munroe',
  133. )
  134. res = self.client.post('/edit/author/%d/update/redirect/' % a.pk,
  135. {'name': 'Randall Munroe (author of xkcd)', 'slug': 'randall-munroe'})
  136. self.assertEqual(res.status_code, 302)
  137. self.assertRedirects(res, 'http://testserver/edit/authors/create/')
  138. self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (author of xkcd)>'])
  139. def test_update_with_interpolated_redirect(self):
  140. a = Author.objects.create(
  141. name='Randall Munroe',
  142. slug='randall-munroe',
  143. )
  144. res = self.client.post('/edit/author/%d/update/interpolate_redirect/' % a.pk,
  145. {'name': 'Randall Munroe (author of xkcd)', 'slug': 'randall-munroe'})
  146. self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (author of xkcd)>'])
  147. self.assertEqual(res.status_code, 302)
  148. pk = Author.objects.all()[0].pk
  149. self.assertRedirects(res, 'http://testserver/edit/author/%d/update/' % pk)
  150. def test_update_with_special_properties(self):
  151. a = Author.objects.create(
  152. name='Randall Munroe',
  153. slug='randall-munroe',
  154. )
  155. res = self.client.get('/edit/author/%d/update/special/' % a.pk)
  156. self.assertEqual(res.status_code, 200)
  157. self.assertTrue(isinstance(res.context['form'], views.AuthorForm))
  158. self.assertEqual(res.context['object'], Author.objects.get(pk=a.pk))
  159. self.assertEqual(res.context['thingy'], Author.objects.get(pk=a.pk))
  160. self.assertFalse('author' in res.context)
  161. self.assertTemplateUsed(res, 'generic_views/form.html')
  162. res = self.client.post('/edit/author/%d/update/special/' % a.pk,
  163. {'name': 'Randall Munroe (author of xkcd)', 'slug': 'randall-munroe'})
  164. self.assertEqual(res.status_code, 302)
  165. self.assertRedirects(res, 'http://testserver/detail/author/%d/' % a.pk)
  166. self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (author of xkcd)>'])
  167. def test_update_without_redirect(self):
  168. try:
  169. a = Author.objects.create(
  170. name='Randall Munroe',
  171. slug='randall-munroe',
  172. )
  173. res = self.client.post('/edit/author/%d/update/naive/' % a.pk,
  174. {'name': 'Randall Munroe (author of xkcd)', 'slug': 'randall-munroe'})
  175. self.fail('Should raise exception -- No redirect URL provided, and no get_absolute_url provided')
  176. except ImproperlyConfigured:
  177. pass
  178. def test_update_get_object(self):
  179. a = Author.objects.create(
  180. pk=1,
  181. name='Randall Munroe',
  182. slug='randall-munroe',
  183. )
  184. res = self.client.get('/edit/author/update/')
  185. self.assertEqual(res.status_code, 200)
  186. self.assertTrue(isinstance(res.context['form'], forms.ModelForm))
  187. self.assertEqual(res.context['object'], Author.objects.get(pk=a.pk))
  188. self.assertEqual(res.context['author'], Author.objects.get(pk=a.pk))
  189. self.assertTemplateUsed(res, 'generic_views/author_form.html')
  190. # Modification with both POST and PUT (browser compatible)
  191. res = self.client.post('/edit/author/update/',
  192. {'name': 'Randall Munroe (xkcd)', 'slug': 'randall-munroe'})
  193. self.assertEqual(res.status_code, 302)
  194. self.assertRedirects(res, 'http://testserver/list/authors/')
  195. self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (xkcd)>'])
  196. class DeleteViewTests(TestCase):
  197. urls = 'regressiontests.generic_views.urls'
  198. def test_delete_by_post(self):
  199. a = Author.objects.create(**{'name': 'Randall Munroe', 'slug': 'randall-munroe'})
  200. res = self.client.get('/edit/author/%d/delete/' % a.pk)
  201. self.assertEqual(res.status_code, 200)
  202. self.assertEqual(res.context['object'], Author.objects.get(pk=a.pk))
  203. self.assertEqual(res.context['author'], Author.objects.get(pk=a.pk))
  204. self.assertTemplateUsed(res, 'generic_views/author_confirm_delete.html')
  205. # Deletion with POST
  206. res = self.client.post('/edit/author/%d/delete/' % a.pk)
  207. self.assertEqual(res.status_code, 302)
  208. self.assertRedirects(res, 'http://testserver/list/authors/')
  209. self.assertQuerysetEqual(Author.objects.all(), [])
  210. def test_delete_by_delete(self):
  211. # Deletion with browser compatible DELETE method
  212. a = Author.objects.create(**{'name': 'Randall Munroe', 'slug': 'randall-munroe'})
  213. res = self.client.delete('/edit/author/%d/delete/' % a.pk)
  214. self.assertEqual(res.status_code, 302)
  215. self.assertRedirects(res, 'http://testserver/list/authors/')
  216. self.assertQuerysetEqual(Author.objects.all(), [])
  217. def test_delete_with_redirect(self):
  218. a = Author.objects.create(**{'name': 'Randall Munroe', 'slug': 'randall-munroe'})
  219. res = self.client.post('/edit/author/%d/delete/redirect/' % a.pk)
  220. self.assertEqual(res.status_code, 302)
  221. self.assertRedirects(res, 'http://testserver/edit/authors/create/')
  222. self.assertQuerysetEqual(Author.objects.all(), [])
  223. def test_delete_with_special_properties(self):
  224. a = Author.objects.create(**{'name': 'Randall Munroe', 'slug': 'randall-munroe'})
  225. res = self.client.get('/edit/author/%d/delete/special/' % a.pk)
  226. self.assertEqual(res.status_code, 200)
  227. self.assertEqual(res.context['object'], Author.objects.get(pk=a.pk))
  228. self.assertEqual(res.context['thingy'], Author.objects.get(pk=a.pk))
  229. self.assertFalse('author' in res.context)
  230. self.assertTemplateUsed(res, 'generic_views/confirm_delete.html')
  231. res = self.client.post('/edit/author/%d/delete/special/' % a.pk)
  232. self.assertEqual(res.status_code, 302)
  233. self.assertRedirects(res, 'http://testserver/list/authors/')
  234. self.assertQuerysetEqual(Author.objects.all(), [])
  235. def test_delete_without_redirect(self):
  236. try:
  237. a = Author.objects.create(
  238. name='Randall Munroe',
  239. slug='randall-munroe',
  240. )
  241. res = self.client.post('/edit/author/%d/delete/naive/' % a.pk)
  242. self.fail('Should raise exception -- No redirect URL provided, and no get_absolute_url provided')
  243. except ImproperlyConfigured:
  244. pass