test_ChangelistInlineModelAdmin.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # -*- coding: utf-8 -*-
  2. from unittest import mock
  3. from django.contrib import admin
  4. from django.http import HttpRequest
  5. from django.test import TestCase
  6. from django_changelist_inline import ChangelistInlineModelAdmin
  7. from django_changelist_inline.admin import InlineChangeList
  8. from testing.factories import ThingFactory
  9. from testing.models import RelatedThing
  10. class ChangelistInlineModelAdminTestCase(TestCase):
  11. def setUp(self):
  12. self.thing = ThingFactory()
  13. self.fake_request = mock.Mock(spec=HttpRequest)
  14. def test_init(self):
  15. # When
  16. model_admin = ChangelistInlineModelAdmin(
  17. self.thing, RelatedThing, admin.site,
  18. )
  19. # Then
  20. self.assertEqual(model_admin.parent_instance, self.thing)
  21. self.assertEqual(model_admin.list_editable, ())
  22. self.assertEqual(model_admin.list_filter, ())
  23. self.assertEqual(model_admin.search_fields, ())
  24. self.assertIsNone(model_admin.date_hierarchy)
  25. self.assertEqual(model_admin.sortable_by, ())
  26. # Account for `search_help_text` attr added in Django 4.0
  27. if hasattr(model_admin, 'search_help_text'):
  28. self.assertIsNone(model_admin.search_help_text)
  29. @mock.patch('django.contrib.admin.ModelAdmin.get_actions')
  30. def test_get_actions(self, mock_get_actions):
  31. # Given
  32. mock_get_actions.return_value = []
  33. model_admin = ChangelistInlineModelAdmin(
  34. self.thing, RelatedThing, admin.site,
  35. )
  36. # When
  37. result = model_admin.get_actions(self.fake_request)
  38. # Then
  39. self.assertEqual(result, [])
  40. self.assertFalse(mock_get_actions.called)
  41. def test_get_changelist(self):
  42. # Given
  43. model_admin = ChangelistInlineModelAdmin(
  44. self.thing, RelatedThing, admin.site,
  45. )
  46. # When
  47. result = model_admin.get_changelist(self.fake_request)
  48. # Then
  49. self.assertEqual(result, InlineChangeList)
  50. def test_title(self):
  51. # Given
  52. model_admin = ChangelistInlineModelAdmin(
  53. self.thing, RelatedThing, admin.site,
  54. )
  55. # Then
  56. self.assertEqual(
  57. model_admin.title, RelatedThing._meta.verbose_name_plural,
  58. )
  59. def test_no_results_message(self):
  60. # Given
  61. model_admin = ChangelistInlineModelAdmin(
  62. self.thing, RelatedThing, admin.site,
  63. )
  64. # Then
  65. self.assertEqual(
  66. model_admin.no_results_message,
  67. f'0 {RelatedThing._meta.verbose_name_plural}',
  68. )
  69. def test_get_add_url_no_add_permission(self):
  70. # Given
  71. model_admin = ChangelistInlineModelAdmin(
  72. self.thing, RelatedThing, admin.site,
  73. )
  74. with mock.patch.object(model_admin, 'has_add_permission') as mock_has_add_permission:
  75. mock_has_add_permission.return_value = False
  76. # Then
  77. self.assertIsNone(model_admin.get_add_url(self.fake_request))
  78. mock_has_add_permission.assert_called_with(self.fake_request)
  79. def test_get_add_url(self):
  80. # Given
  81. model_admin = ChangelistInlineModelAdmin(
  82. self.thing, RelatedThing, admin.site,
  83. )
  84. with mock.patch.object(model_admin, 'has_add_permission') as mock_has_add_permission:
  85. mock_has_add_permission.return_value = True
  86. # Then
  87. self.assertEqual(
  88. model_admin.get_add_url(self.fake_request),
  89. '/admin/testing/relatedthing/add/',
  90. )
  91. def test_get_show_all_url_no_view_permission(self):
  92. # Given
  93. model_admin = ChangelistInlineModelAdmin(
  94. self.thing, RelatedThing, admin.site,
  95. )
  96. with mock.patch.object(model_admin, 'has_view_permission') as mock_has_view_permission:
  97. mock_has_view_permission.return_value = False
  98. # Then
  99. self.assertIsNone(model_admin.get_show_all_url(self.fake_request))
  100. mock_has_view_permission.assert_called_with(
  101. self.fake_request, obj=None,
  102. )
  103. def test_get_show_all_url(self):
  104. # Given
  105. model_admin = ChangelistInlineModelAdmin(
  106. self.thing, RelatedThing, admin.site,
  107. )
  108. with mock.patch.object(model_admin, 'has_view_permission') as mock_has_view_permission:
  109. mock_has_view_permission.return_value = True
  110. # Then
  111. self.assertEqual(
  112. model_admin.get_show_all_url(self.fake_request),
  113. '/admin/testing/relatedthing/',
  114. )
  115. def test_get_toolbar_links(self):
  116. # Given
  117. model_admin = ChangelistInlineModelAdmin(
  118. self.thing, RelatedThing, admin.site,
  119. )
  120. # Then
  121. self.assertIsNone(model_admin.get_toolbar_links(self.fake_request))