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