Skip to content

Commit

Permalink
chore: Add tests for exceptions module
Browse files Browse the repository at this point in the history
  • Loading branch information
abhiabhi94 authored and Radi85 committed Dec 5, 2020
1 parent 30b62e3 commit 05ba2a6
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions comment/tests/test_exceptions.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 05ba2a6

Please sign in to comment.