test_InlineChangeList.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # -*- coding: utf-8 -*-
  2. from distutils.version import StrictVersion
  3. from unittest import mock
  4. import django
  5. from django.contrib import admin
  6. from django.contrib.auth.models import User
  7. from django.http import HttpRequest
  8. from django.test import TestCase
  9. from django_changelist_inline.admin import (
  10. ChangelistInlineModelAdmin,
  11. InlineChangeList,
  12. )
  13. from testing.models import RelatedThing, Thing
  14. from testing.factories import ThingFactory
  15. class RelatedThingInlineModelAdmin(ChangelistInlineModelAdmin):
  16. pass
  17. class InlineChangeListTestCase(TestCase):
  18. def setUp(self):
  19. self.fake_user = mock.Mock(spec=User)
  20. self.fake_user.has_perm.return_value = True
  21. self.fake_request = mock.Mock(spec=HttpRequest)
  22. self.fake_request.GET = {}
  23. self.fake_request.resolver_match = None
  24. self.fake_request.user = self.fake_user
  25. thing = ThingFactory()
  26. self.model_admin = RelatedThingInlineModelAdmin(
  27. thing, RelatedThing, admin.site,
  28. )
  29. def _get_change_list_args(self):
  30. result = [
  31. self.fake_request, Thing, ['pk'], ['pk'], [], None, None, None, 5,
  32. 5, None, self.model_admin, None,
  33. ]
  34. django_version = StrictVersion(django.__version__)
  35. if django_version.version[0] >= 4:
  36. result.append(None) # Account for the new `search_help_text` arg
  37. return result
  38. def test_init(self):
  39. # When
  40. change_list = InlineChangeList(*self._get_change_list_args())
  41. # Then
  42. self.assertIsNone(change_list.formset)
  43. self.assertEqual(
  44. change_list.add_url, '/admin/testing/relatedthing/add/',
  45. )
  46. self.assertEqual(
  47. change_list.show_all_url, '/admin/testing/relatedthing/',
  48. )
  49. self.assertIsNone(change_list.toolbar_links)
  50. def test_get_filters_params(self):
  51. # Given
  52. change_list = InlineChangeList(*self._get_change_list_args())
  53. # When
  54. result = change_list.get_filters_params(params={'q': 'spam'})
  55. # Then
  56. self.assertEqual(result, {})
  57. def test_has_toolbar_False(self):
  58. # Given
  59. change_list = InlineChangeList(*self._get_change_list_args())
  60. change_list.add_url = None
  61. change_list.show_all_url = None
  62. change_list.toolbar_links = None
  63. # Then
  64. self.assertFalse(change_list.has_toolbar)
  65. def test_has_toolbar_True_with_add_url(self):
  66. # Given
  67. change_list = InlineChangeList(*self._get_change_list_args())
  68. change_list.add_url = 'add_url'
  69. change_list.show_all_url = None
  70. change_list.toolbar_links = None
  71. # Then
  72. self.assertTrue(change_list.has_toolbar)
  73. def test_has_toolbar_True_with_show_all_url(self):
  74. # Given
  75. change_list = InlineChangeList(*self._get_change_list_args())
  76. change_list.add_url = None
  77. change_list.show_all_url = 'show_all_url'
  78. change_list.toolbar_links = None
  79. # Then
  80. self.assertTrue(change_list.has_toolbar)
  81. def test_has_toolbar_True_with_toolbar_links(self):
  82. # Given
  83. change_list = InlineChangeList(*self._get_change_list_args())
  84. change_list.add_url = None
  85. change_list.show_all_url = None
  86. change_list.toolbar_links = 'toolbar_links'
  87. # Then
  88. self.assertTrue(change_list.has_toolbar)