test_exceptionhandler.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. """
  14. exceptionhandler.is_misago_exception recognizes handled exceptions
  15. """
  16. for exception in exceptionhandler.HANDLED_EXCEPTIONS:
  17. try:
  18. raise exception()
  19. except exception as e:
  20. self.assertTrue(exceptionhandler.is_misago_exception(e))
  21. def test_is_misago_exception_false_for_not_handled_exceptions(self):
  22. """
  23. exceptionhandler.is_misago_exception fails to recognize other
  24. exceptions
  25. """
  26. for exception in INVALID_EXCEPTIONS:
  27. try:
  28. raise exception()
  29. except exception as e:
  30. self.assertFalse(exceptionhandler.is_misago_exception(e))
  31. class GetExceptionHandlerTests(TestCase):
  32. def test_exception_handlers_list(self):
  33. """HANDLED_EXCEPTIONS length matches that of EXCEPTION_HANDLERS"""
  34. self.assertEqual(len(exceptionhandler.HANDLED_EXCEPTIONS),
  35. len(exceptionhandler.EXCEPTION_HANDLERS))
  36. def test_get_exception_handler_for_handled_exceptions(self):
  37. """Exception handler has correct handler for every Misago exception"""
  38. for exception in exceptionhandler.HANDLED_EXCEPTIONS:
  39. exceptionhandler.get_exception_handler(exception())
  40. def test_get_exception_handler_for_non_handled_exceptio(self):
  41. """Exception handler has no handler for non-supported exception"""
  42. for exception in INVALID_EXCEPTIONS:
  43. with self.assertRaises(ValueError):
  44. exceptionhandler.get_exception_handler(exception())