test_checks.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. from unittest import mock
  2. from django.conf import settings
  3. from django.contrib.staticfiles.checks import check_finders
  4. from django.contrib.staticfiles.finders import BaseFinder
  5. from django.core.checks import Error
  6. from django.test import SimpleTestCase, override_settings
  7. class FindersCheckTests(SimpleTestCase):
  8. def test_base_finder_check_not_implemented(self):
  9. finder = BaseFinder()
  10. msg = 'subclasses may provide a check() method to verify the finder is configured correctly.'
  11. with self.assertRaisesMessage(NotImplementedError, msg):
  12. finder.check()
  13. def test_check_finders(self):
  14. """check_finders() concatenates all errors."""
  15. error1 = Error('1')
  16. error2 = Error('2')
  17. error3 = Error('3')
  18. def get_finders():
  19. class Finder1(BaseFinder):
  20. def check(self, **kwargs):
  21. return [error1]
  22. class Finder2(BaseFinder):
  23. def check(self, **kwargs):
  24. return []
  25. class Finder3(BaseFinder):
  26. def check(self, **kwargs):
  27. return [error2, error3]
  28. class Finder4(BaseFinder):
  29. pass
  30. return [Finder1(), Finder2(), Finder3(), Finder4()]
  31. with mock.patch('django.contrib.staticfiles.checks.get_finders', get_finders):
  32. errors = check_finders(None)
  33. self.assertEqual(errors, [error1, error2, error3])
  34. def test_no_errors_with_test_settings(self):
  35. self.assertEqual(check_finders(None), [])
  36. @override_settings(STATICFILES_DIRS='a string')
  37. def test_dirs_not_tuple_or_list(self):
  38. self.assertEqual(check_finders(None), [
  39. Error(
  40. 'The STATICFILES_DIRS setting is not a tuple or list.',
  41. hint='Perhaps you forgot a trailing comma?',
  42. id='staticfiles.E001',
  43. )
  44. ])
  45. @override_settings(STATICFILES_DIRS=['/fake/path', settings.STATIC_ROOT])
  46. def test_dirs_contains_static_root(self):
  47. self.assertEqual(check_finders(None), [
  48. Error(
  49. 'The STATICFILES_DIRS setting should not contain the '
  50. 'STATIC_ROOT setting.',
  51. id='staticfiles.E002',
  52. )
  53. ])
  54. @override_settings(STATICFILES_DIRS=[('prefix', settings.STATIC_ROOT)])
  55. def test_dirs_contains_static_root_in_tuple(self):
  56. self.assertEqual(check_finders(None), [
  57. Error(
  58. 'The STATICFILES_DIRS setting should not contain the '
  59. 'STATIC_ROOT setting.',
  60. id='staticfiles.E002',
  61. )
  62. ])