list.py 9.0 KB

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