Ticket #17818: 17818_test_v1.diff

File 17818_test_v1.diff, 10.2 KB (added by Hiroki Kiyohara, 12 years ago)

Tests for sending messages of edie views.

  • tests/regressiontests/generic_views/edit.py

    diff --git a/tests/regressiontests/generic_views/edit.py b/tests/regressiontests/generic_views/edit.py
    index 0f1eb3c..19ea598 100644
    a b class ModelFormMixinTests(TestCase):  
    3434        form_class = views.AuthorGetQuerySetFormView().get_form_class()
    3535        self.assertEqual(form_class._meta.model, Author)
    3636
     37
     38class SuccessMessageMixinTest(TestCase):
     39    def test_nonSmessage(self):
     40        self.assertRaise(ImproperlyConfigured,
     41                         views.NonMessageSuccessMessageMixin().create_success_message,
     42                         verbase_name='NonMessage'
     43        )
     44    def test_invalid_message(self):
     45        self.assertRaise(TypeError,
     46                         views.InvalidMessageSuccessMessageMixin().create_success_message,
     47                         verbase_name='NonMessage'
     48         )
     49
     50
    3751class CreateViewTests(TestCase):
    3852    urls = 'regressiontests.generic_views.urls'
    3953
    class CreateViewTests(TestCase):  
    5165        self.assertEqual(res.status_code, 302)
    5266        self.assertRedirects(res, 'http://testserver/list/authors/')
    5367        self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
     68        self.assertTrue('The author was created successfully.' in res.cookies['messages'].value)
    5469
    5570    def test_create_invalid(self):
    5671        res = self.client.post('/edit/authors/create/',
    class CreateViewTests(TestCase):  
    5974        self.assertTemplateUsed(res, 'generic_views/author_form.html')
    6075        self.assertEqual(len(res.context['form'].errors), 1)
    6176        self.assertEqual(Author.objects.count(), 0)
     77        self.assertFalse(res.cookies.get('messages', False))
    6278
    6379    def test_create_with_object_url(self):
    6480        res = self.client.post('/edit/artists/create/',
    class CreateViewTests(TestCase):  
    6783        artist = Artist.objects.get(name='Rene Magritte')
    6884        self.assertRedirects(res, 'http://testserver/detail/artist/%d/' % artist.pk)
    6985        self.assertQuerysetEqual(Artist.objects.all(), ['<Artist: Rene Magritte>'])
     86        self.assertTrue('The professional artist was created successfully.' in res.cookies['messages'].value)
    7087
    7188    def test_create_with_redirect(self):
    7289        res = self.client.post('/edit/authors/create/redirect/',
    class CreateViewTests(TestCase):  
    7491        self.assertEqual(res.status_code, 302)
    7592        self.assertRedirects(res, 'http://testserver/edit/authors/create/')
    7693        self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
     94        self.assertTrue('The author was created successfully.' in res.cookies['messages'].value)
    7795
    7896    def test_create_with_interpolated_redirect(self):
    7997        res = self.client.post('/edit/authors/create/interpolate_redirect/',
    class CreateViewTests(TestCase):  
    82100        self.assertEqual(res.status_code, 302)
    83101        pk = Author.objects.all()[0].pk
    84102        self.assertRedirects(res, 'http://testserver/edit/author/%d/update/' % pk)
     103        self.assertTrue('The author was created successfully.' in res.cookies['messages'].value)
    85104
    86105    def test_create_with_special_properties(self):
    87106        res = self.client.get('/edit/authors/create/special/')
    class CreateViewTests(TestCase):  
    97116        obj = Author.objects.get(slug='randall-munroe')
    98117        self.assertRedirects(res, reverse('author_detail', kwargs={'pk': obj.pk}))
    99118        self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
     119        self.assertTrue('The author was created successfully.' in res.cookies['messages'].value)
    100120
    101121    def test_create_without_redirect(self):
    102122        try:
    class CreateViewTests(TestCase):  
    111131                        {'name': 'Randall Munroe', 'slug': 'randall-munroe'})
    112132        self.assertEqual(res.status_code, 302)
    113133        self.assertRedirects(res, 'http://testserver/accounts/login/?next=/edit/authors/create/restricted/')
     134        self.assertFalse(res.cookies.get('messages', False))
    114135
    115136
    116137class UpdateViewTests(TestCase):
    class UpdateViewTests(TestCase):  
    134155        self.assertEqual(res.status_code, 302)
    135156        self.assertRedirects(res, 'http://testserver/list/authors/')
    136157        self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (xkcd)>'])
     158        self.assertTrue('The author was updated successfully.' in res.cookies['messages'].value)
    137159
    138160    @expectedFailure
    139161    def test_update_put(self):
    class UpdateViewTests(TestCase):  
    154176        self.assertEqual(res.status_code, 302)
    155177        self.assertRedirects(res, 'http://testserver/list/authors/')
    156178        self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (author of xkcd)>'])
     179        self.assertTrue('The author was updated successfully.' in res.cookies['messages'].value)
    157180
    158181    def test_update_invalid(self):
    159182        a = Author.objects.create(
    class UpdateViewTests(TestCase):  
    166189        self.assertTemplateUsed(res, 'generic_views/author_form.html')
    167190        self.assertEqual(len(res.context['form'].errors), 1)
    168191        self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
     192        self.assertFalse(res.cookies.get('messages', False))
    169193
    170194    def test_update_with_object_url(self):
    171195        a = Artist.objects.create(name='Rene Magritte')
    class UpdateViewTests(TestCase):  
    174198        self.assertEqual(res.status_code, 302)
    175199        self.assertRedirects(res, 'http://testserver/detail/artist/%d/' % a.pk)
    176200        self.assertQuerysetEqual(Artist.objects.all(), ['<Artist: Rene Magritte>'])
     201        self.assertTrue('The professional artist was updated successfully.' in res.cookies['messages'].value)
    177202
    178203    def test_update_with_redirect(self):
    179204        a = Author.objects.create(
    class UpdateViewTests(TestCase):  
    197222        self.assertEqual(res.status_code, 302)
    198223        pk = Author.objects.all()[0].pk
    199224        self.assertRedirects(res, 'http://testserver/edit/author/%d/update/' % pk)
     225        self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (author of xkcd)>'])
    200226
    201227    def test_update_with_special_properties(self):
    202228        a = Author.objects.create(
    class UpdateViewTests(TestCase):  
    216242        self.assertEqual(res.status_code, 302)
    217243        self.assertRedirects(res, 'http://testserver/detail/author/%d/' % a.pk)
    218244        self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (author of xkcd)>'])
     245        self.assertTrue('The author was updated successfully.' in res.cookies['messages'].value)
    219246
    220247    def test_update_without_redirect(self):
    221248        try:
    class UpdateViewTests(TestCase):  
    226253            res = self.client.post('/edit/author/%d/update/naive/' % a.pk,
    227254                            {'name': 'Randall Munroe (author of xkcd)', 'slug': 'randall-munroe'})
    228255            self.fail('Should raise exception -- No redirect URL provided, and no get_absolute_url provided')
     256            self.assertFalse(res.cookies.get('messages', False))
    229257        except ImproperlyConfigured:
    230258            pass
    231259
    class UpdateViewTests(TestCase):  
    249277        self.assertEqual(res.status_code, 302)
    250278        self.assertRedirects(res, 'http://testserver/list/authors/')
    251279        self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (xkcd)>'])
     280        self.assertTrue('The author was updated successfully.' in res.cookies['messages'].value)
    252281
    253282
    254283class DeleteViewTests(TestCase):
    class DeleteViewTests(TestCase):  
    267296        self.assertEqual(res.status_code, 302)
    268297        self.assertRedirects(res, 'http://testserver/list/authors/')
    269298        self.assertQuerysetEqual(Author.objects.all(), [])
     299        self.assertTrue('The author was deleted.' in res.cookies['messages'].value)
    270300
    271301    def test_delete_by_delete(self):
    272302        # Deletion with browser compatible DELETE method
    class DeleteViewTests(TestCase):  
    275305        self.assertEqual(res.status_code, 302)
    276306        self.assertRedirects(res, 'http://testserver/list/authors/')
    277307        self.assertQuerysetEqual(Author.objects.all(), [])
     308        self.assertTrue('The author was deleted.' in res.cookies['messages'].value)
    278309
    279310    def test_delete_with_redirect(self):
    280311        a = Author.objects.create(**{'name': 'Randall Munroe', 'slug': 'randall-munroe'})
    class DeleteViewTests(TestCase):  
    282313        self.assertEqual(res.status_code, 302)
    283314        self.assertRedirects(res, 'http://testserver/edit/authors/create/')
    284315        self.assertQuerysetEqual(Author.objects.all(), [])
     316        self.assertTrue('The author was deleted.' in res.cookies['messages'].value)
    285317
    286318    def test_delete_with_special_properties(self):
    287319        a = Author.objects.create(**{'name': 'Randall Munroe', 'slug': 'randall-munroe'})
    class DeleteViewTests(TestCase):  
    296328        self.assertEqual(res.status_code, 302)
    297329        self.assertRedirects(res, 'http://testserver/list/authors/')
    298330        self.assertQuerysetEqual(Author.objects.all(), [])
     331        self.assertTrue('The author was deleted.' in res.cookies['messages'].value)
    299332
    300333    def test_delete_without_redirect(self):
    301334        try:
    class DeleteViewTests(TestCase):  
    305338            )
    306339            res = self.client.post('/edit/author/%d/delete/naive/' % a.pk)
    307340            self.fail('Should raise exception -- No redirect URL provided, and no get_absolute_url provided')
     341            self.assertFalse(res.cookies.get('messages'), False)
    308342        except ImproperlyConfigured:
    309343            pass
    310344
  • tests/regressiontests/generic_views/views.py

    diff --git a/tests/regressiontests/generic_views/views.py b/tests/regressiontests/generic_views/views.py
    index 71e78e8..96e2cdd 100644
    a b class AuthorGetQuerySetFormView(generic.edit.ModelFormMixin):  
    186186    def get_queryset(self):
    187187        return Author.objects.all()
    188188
     189
     190class NonMessageSuccessMessageView(generic.edit.SuccessMessageMixin):
     191    pass
     192
     193
     194class InvalidMessageSuccessMessageView(generic.edit.SuccessMessageMixin):
     195    success_message = 0
     196
     197
    189198class BookDetailGetObjectCustomQueryset(BookDetail):
    190199    def get_object(self, queryset=None):
    191200        return super(BookDetailGetObjectCustomQueryset,self).get_object(
Back to Top