test_exceptionhandler.py 2.0 KB

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