From 05ba2a6b4f8594e68bbe4409424fcbaaa39bdeae Mon Sep 17 00:00:00 2001 From: abhiabhi94 <13880786+abhiabhi94@users.noreply.github.com> Date: Sat, 28 Nov 2020 06:03:00 +0530 Subject: [PATCH] chore: Add tests for exceptions module --- comment/tests/test_exceptions.py | 35 ++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 comment/tests/test_exceptions.py diff --git a/comment/tests/test_exceptions.py b/comment/tests/test_exceptions.py new file mode 100644 index 0000000..0786a53 --- /dev/null +++ b/comment/tests/test_exceptions.py @@ -0,0 +1,35 @@ +import sys +from unittest.mock import patch + +from rest_framework import status + +from comment.exceptions import CommentBadRequest +from comment.tests.base import TestCase +from comment.messages import ExceptionError + + +class CommentExceptionTest(TestCase): + _default_detail = ExceptionError.BAD_REQUEST + + def test_can_create_custom_error_without_params(self): + exception = CommentBadRequest() + + self.assertEqual(exception.status_code, status.HTTP_400_BAD_REQUEST) + self.assertEqual(exception.detail, self._default_detail) + + def test_create_custom_error_with_params(self): + detail = 'not found' + exception = CommentBadRequest(detail=detail, status_code=404) + + self.assertEqual(exception.status_code, status.HTTP_404_NOT_FOUND) + self.assertEqual(exception.detail, detail) + + def test_create_custom_error_without_drf_installed(self): + with patch.dict(sys.modules, {'rest_framework.exceptions': None}): + from importlib import reload + reload(sys.modules['comment.exceptions']) + from comment.exceptions import CommentBadRequest + exception = CommentBadRequest() + + self.assertEqual(exception.status_code, status.HTTP_400_BAD_REQUEST) + self.assertEqual(exception.detail, self._default_detail)