test_exceptionhandlers.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. from django.core import exceptions as django_exceptions
  2. from django.test import TestCase
  3. from misago.core import exceptionhandler
  4. INVALID_EXCEPTIONS = [
  5. django_exceptions.ObjectDoesNotExist,
  6. django_exceptions.ViewDoesNotExist,
  7. TypeError,
  8. ValueError,
  9. KeyError,
  10. ]
  11. class IsMisagoExceptionTests(TestCase):
  12. def test_is_misago_exception_true_for_handled_exceptions(self):
  13. """exceptionhandler.is_misago_exception recognizes handled exceptions"""
  14. for exception in exceptionhandler.HANDLED_EXCEPTIONS:
  15. self.assertTrue(exceptionhandler.is_misago_exception(exception()))
  16. def test_is_misago_exception_false_for_not_handled_exceptions(self):
  17. """exceptionhandler.is_misago_exception fails to recognize other exceptions"""
  18. for exception in INVALID_EXCEPTIONS:
  19. self.assertFalse(exceptionhandler.is_misago_exception(exception()))
  20. class GetExceptionHandlerTests(TestCase):
  21. def test_exception_handlers_list(self):
  22. """HANDLED_EXCEPTIONS length matches that of EXCEPTION_HANDLERS"""
  23. self.assertEqual(
  24. len(exceptionhandler.HANDLED_EXCEPTIONS), len(exceptionhandler.EXCEPTION_HANDLERS)
  25. )
  26. def test_get_exception_handler_for_handled_exceptions(self):
  27. """Exception handler has correct handler for every Misago exception"""
  28. for exception in exceptionhandler.HANDLED_EXCEPTIONS:
  29. exceptionhandler.get_exception_handler(exception())
  30. def test_get_exception_handler_for_non_handled_exceptio(self):
  31. """Exception handler has no handler for non-supported exception"""
  32. for exception in INVALID_EXCEPTIONS:
  33. with self.assertRaises(ValueError):
  34. exceptionhandler.get_exception_handler(exception())