PageRenderTime 53ms CodeModel.GetById 27ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/regressiontests/generic_views/list.py

https://code.google.com/p/mango-py/
Python | 164 lines | 138 code | 23 blank | 3 comment | 2 complexity | c860d9342f1af33abbc03e3e22cec2f1 MD5 | raw file
  1from django.core.exceptions import ImproperlyConfigured
  2from django.test import TestCase
  3
  4from regressiontests.generic_views.models import Author, Artist
  5from regressiontests.generic_views.views import CustomPaginator
  6
  7class ListViewTests(TestCase):
  8    fixtures = ['generic-views-test-data.json']
  9    urls = 'regressiontests.generic_views.urls'
 10
 11    def test_items(self):
 12        res = self.client.get('/list/dict/')
 13        self.assertEqual(res.status_code, 200)
 14        self.assertTemplateUsed(res, 'generic_views/list.html')
 15        self.assertEqual(res.context['object_list'][0]['first'], 'John')
 16
 17    def test_queryset(self):
 18        res = self.client.get('/list/authors/')
 19        self.assertEqual(res.status_code, 200)
 20        self.assertTemplateUsed(res, 'generic_views/author_list.html')
 21        self.assertEqual(list(res.context['object_list']), list(Author.objects.all()))
 22        self.assertIs(res.context['author_list'], res.context['object_list'])
 23        self.assertIsNone(res.context['paginator'])
 24        self.assertIsNone(res.context['page_obj'])
 25        self.assertFalse(res.context['is_paginated'])
 26
 27    def test_paginated_queryset(self):
 28        self._make_authors(100)
 29        res = self.client.get('/list/authors/paginated/')
 30        self.assertEqual(res.status_code, 200)
 31        self.assertTemplateUsed(res, 'generic_views/author_list.html')
 32        self.assertEqual(len(res.context['object_list']), 30)
 33        self.assertIs(res.context['author_list'], res.context['object_list'])
 34        self.assertTrue(res.context['is_paginated'])
 35        self.assertEqual(res.context['page_obj'].number, 1)
 36        self.assertEqual(res.context['paginator'].num_pages, 4)
 37        self.assertEqual(res.context['author_list'][0].name, 'Author 00')
 38        self.assertEqual(list(res.context['author_list'])[-1].name, 'Author 29')
 39
 40    def test_paginated_queryset_shortdata(self):
 41        # Test that short datasets ALSO result in a paginated view.
 42        res = self.client.get('/list/authors/paginated/')
 43        self.assertEqual(res.status_code, 200)
 44        self.assertTemplateUsed(res, 'generic_views/author_list.html')
 45        self.assertEqual(list(res.context['object_list']), list(Author.objects.all()))
 46        self.assertIs(res.context['author_list'], res.context['object_list'])
 47        self.assertEqual(res.context['page_obj'].number, 1)
 48        self.assertEqual(res.context['paginator'].num_pages, 1)
 49        self.assertFalse(res.context['is_paginated'])
 50
 51    def test_paginated_get_page_by_query_string(self):
 52        self._make_authors(100)
 53        res = self.client.get('/list/authors/paginated/', {'page': '2'})
 54        self.assertEqual(res.status_code, 200)
 55        self.assertTemplateUsed(res, 'generic_views/author_list.html')
 56        self.assertEqual(len(res.context['object_list']), 30)
 57        self.assertIs(res.context['author_list'], res.context['object_list'])
 58        self.assertEqual(res.context['author_list'][0].name, 'Author 30')
 59        self.assertEqual(res.context['page_obj'].number, 2)
 60
 61    def test_paginated_get_last_page_by_query_string(self):
 62        self._make_authors(100)
 63        res = self.client.get('/list/authors/paginated/', {'page': 'last'})
 64        self.assertEqual(res.status_code, 200)
 65        self.assertEqual(len(res.context['object_list']), 10)
 66        self.assertIs(res.context['author_list'], res.context['object_list'])
 67        self.assertEqual(res.context['author_list'][0].name, 'Author 90')
 68        self.assertEqual(res.context['page_obj'].number, 4)
 69
 70    def test_paginated_get_page_by_urlvar(self):
 71        self._make_authors(100)
 72        res = self.client.get('/list/authors/paginated/3/')
 73        self.assertEqual(res.status_code, 200)
 74        self.assertTemplateUsed(res, 'generic_views/author_list.html')
 75        self.assertEqual(len(res.context['object_list']), 30)
 76        self.assertIs(res.context['author_list'], res.context['object_list'])
 77        self.assertEqual(res.context['author_list'][0].name, 'Author 60')
 78        self.assertEqual(res.context['page_obj'].number, 3)
 79
 80    def test_paginated_page_out_of_range(self):
 81        self._make_authors(100)
 82        res = self.client.get('/list/authors/paginated/42/')
 83        self.assertEqual(res.status_code, 404)
 84
 85    def test_paginated_invalid_page(self):
 86        self._make_authors(100)
 87        res = self.client.get('/list/authors/paginated/?page=frog')
 88        self.assertEqual(res.status_code, 404)
 89
 90    def test_paginated_custom_paginator_class(self):
 91        self._make_authors(7)
 92        res = self.client.get('/list/authors/paginated/custom_class/')
 93        self.assertEqual(res.status_code, 200)
 94        self.assertEqual(res.context['paginator'].num_pages, 1)
 95        # Custom pagination allows for 2 orphans on a page size of 5
 96        self.assertEqual(len(res.context['object_list']), 7)
 97
 98    def test_paginated_custom_paginator_constructor(self):
 99        self._make_authors(7)
100        res = self.client.get('/list/authors/paginated/custom_constructor/')
101        self.assertEqual(res.status_code, 200)
102        # Custom pagination allows for 2 orphans on a page size of 5
103        self.assertEqual(len(res.context['object_list']), 7)
104
105    def test_paginated_non_queryset(self):
106        res = self.client.get('/list/dict/paginated/')
107        self.assertEqual(res.status_code, 200)
108        self.assertEqual(len(res.context['object_list']), 1)
109
110    def test_verbose_name(self):
111        res = self.client.get('/list/artists/')
112        self.assertEqual(res.status_code, 200)
113        self.assertTemplateUsed(res, 'generic_views/list.html')
114        self.assertEqual(list(res.context['object_list']), list(Artist.objects.all()))
115        self.assertIs(res.context['artist_list'], res.context['object_list'])
116        self.assertIsNone(res.context['paginator'])
117        self.assertIsNone(res.context['page_obj'])
118        self.assertFalse(res.context['is_paginated'])
119
120    def test_allow_empty_false(self):
121        res = self.client.get('/list/authors/notempty/')
122        self.assertEqual(res.status_code, 200)
123        Author.objects.all().delete()
124        res = self.client.get('/list/authors/notempty/')
125        self.assertEqual(res.status_code, 404)
126
127    def test_template_name(self):
128        res = self.client.get('/list/authors/template_name/')
129        self.assertEqual(res.status_code, 200)
130        self.assertEqual(list(res.context['object_list']), list(Author.objects.all()))
131        self.assertIs(res.context['author_list'], res.context['object_list'])
132        self.assertTemplateUsed(res, 'generic_views/list.html')
133
134    def test_template_name_suffix(self):
135        res = self.client.get('/list/authors/template_name_suffix/')
136        self.assertEqual(res.status_code, 200)
137        self.assertEqual(list(res.context['object_list']), list(Author.objects.all()))
138        self.assertIs(res.context['author_list'], res.context['object_list'])
139        self.assertTemplateUsed(res, 'generic_views/author_objects.html')
140
141    def test_context_object_name(self):
142        res = self.client.get('/list/authors/context_object_name/')
143        self.assertEqual(res.status_code, 200)
144        self.assertEqual(list(res.context['object_list']), list(Author.objects.all()))
145        self.assertNotIn('authors', res.context)
146        self.assertIs(res.context['author_list'], res.context['object_list'])
147        self.assertTemplateUsed(res, 'generic_views/author_list.html')
148
149    def test_duplicate_context_object_name(self):
150        res = self.client.get('/list/authors/dupe_context_object_name/')
151        self.assertEqual(res.status_code, 200)
152        self.assertEqual(list(res.context['object_list']), list(Author.objects.all()))
153        self.assertNotIn('authors', res.context)
154        self.assertNotIn('author_list', res.context)
155        self.assertTemplateUsed(res, 'generic_views/author_list.html')
156
157    def test_missing_items(self):
158        self.assertRaises(ImproperlyConfigured, self.client.get, '/list/authors/invalid/')
159
160    def _make_authors(self, n):
161        Author.objects.all().delete()
162        for i in range(n):
163            Author.objects.create(name='Author %02i' % i, slug='a%s' % i)
164