/tests/regressiontests/generic_views/list.py

https://code.google.com/p/mango-py/ · Python · 164 lines · 138 code · 23 blank · 3 comment · 1 complexity · c860d9342f1af33abbc03e3e22cec2f1 MD5 · raw file

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