Ticket #15274: tests_for_get_object.patch

File tests_for_get_object.patch, 3.8 KB (added by Sergey N. Belinsky <sergeybe@…>, 14 years ago)

Tests for UpdateView and get_object method

  • tests/regressiontests/generic_views/views.py

     
    114114    success_url = '/list/authors/'
    115115
    116116
     117class OneAuthorUpdate(generic.UpdateView):
     118
     119    def get_object(self):
     120        return Author.objects.get(pk=1)
     121
     122
     123class OneAuthorWithRedirectUpdate(generic.UpdateView):
     124    success_url = '/list/authors/'
     125
     126    def get_object(self):
     127        return Author.objects.get(pk=1)
     128
     129
    117130class SpecializedAuthorUpdate(generic.UpdateView):
    118131    model = Author
    119132    form_class = AuthorForm
  • tests/regressiontests/generic_views/edit.py

     
    201201        except ImproperlyConfigured:
    202202            pass
    203203
     204    def test_update_get_object(self):
     205        a = Author.objects.create(
     206            name='Randall Munroe',
     207            slug='randall-munroe',
     208        )
     209        res = self.client.get('/edit/author/update/')
     210        self.assertEqual(res.status_code, 200)
     211        self.assertTrue(isinstance(res.context['form'], forms.ModelForm))
     212        self.assertEqual(res.context['object'], Author.objects.get(pk=a.pk))
     213        self.assertEqual(res.context['author'], Author.objects.get(pk=a.pk))
     214        self.assertTemplateUsed(res, 'generic_views/author_form.html')
     215
     216        # Modification with both POST and PUT (browser compatible)
     217        res = self.client.post('/edit/author/update/' % a.pk,
     218                        {'name': 'Randall Munroe (xkcd)', 'slug': 'randall-munroe'})
     219        self.assertEqual(res.status_code, 302)
     220        self.assertRedirects(res, 'http://testserver/list/authors/')
     221        self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (xkcd)>'])
     222
     223    def test_update_get_object_with_redirect(self):
     224        a = Author.objects.create(
     225            name='Randall Munroe',
     226            slug='randall-munroe',
     227        )
     228        res = self.client.get('/edit/author/update/redirect')
     229        self.assertEqual(res.status_code, 200)
     230        self.assertTrue(isinstance(res.context['form'], forms.ModelForm))
     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_form.html')
     234
     235        # Modification with both POST and PUT (browser compatible)
     236        res = self.client.post('/edit/author/update/redirect' % a.pk,
     237                        {'name': 'Randall Munroe (xkcd)', 'slug': 'randall-munroe'})
     238        self.assertEqual(res.status_code, 302)
     239        self.assertRedirects(res, 'http://testserver/list/authors/')
     240        self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (xkcd)>'])
     241
    204242class DeleteViewTests(TestCase):
    205243    urls = 'regressiontests.generic_views.urls'
    206244
  • tests/regressiontests/generic_views/urls.py

     
    7272        views.NaiveAuthorUpdate.as_view(success_url='/edit/author/%(id)d/update/')),
    7373    (r'^edit/author/(?P<pk>\d+)/update/$',
    7474        views.AuthorUpdate.as_view()),
     75    (r'^edit/author/update/$',
     76        views.OneAuthorUpdate.as_view()),
     77    (r'^edit/author/update/redirect$',
     78        views.OneAuthorWithRedirectUpdate.as_view()),
    7579    (r'^edit/author/(?P<pk>\d+)/update/special/$',
    7680        views.SpecializedAuthorUpdate.as_view()),
    7781    (r'^edit/author/(?P<pk>\d+)/delete/naive/$',
Back to Top