Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve exception handler tests #4

Merged
merged 2 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rest_framework_verbose_errors/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

VERSION = (0, 0, 5)
VERSION = (0, 0, 6)

__version__ = '.'.join(str(number) for number in VERSION)
61 changes: 48 additions & 13 deletions tests/views_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,78 @@

class ViewsTestCase(unittest.TestCase):
def test_exception_handler_returns_correct_response_if_detail_is_str(self):
exc = APIException('error')
exc = APIException('You are not authenticated')
context = {}
response = exception_handler(exc, context)
expected = {
'errors': [
{
'field': None,
'messages': ['error'],
}
]
'messages': ['You are not authenticated'],
},
],
}
self.assertEqual(expected, response.data)

def test_exception_handler_returns_correct_response_if_detail_is_detail_dict(self):
exc = APIException({'detail': 'error'})
def test_exception_handler_returns_correct_response_if_detail_is_dict_which_contains_detail_key(self):
exc = APIException({'detail': 'Permisssion denied'})
context = {}
response = exception_handler(exc, context)
expected = {
'errors': [
{
'field': None,
'messages': ['error'],
}
]
'messages': ['Permisssion denied'],
},
],
}
self.assertEqual(expected, response.data)

def test_exception_handler_returns_correct_response_if_detail_is_dict_with_list(self):
exc = APIException({'name': ['This field is required', 'This field can not be null']})
def test_exception_handler_returns_correct_response_if_detail_is_list(self):
exc = APIException(['Invalid name', 'Invalid age'])
context = {}
response = exception_handler(exc, context)
expected = {
'errors': [
{
'field': None,
'messages': ['Invalid name', 'Invalid age'],
},
],
}
self.assertEqual(expected, response.data)

def test_exception_handler_returns_correct_response_if_detail_is_tuple(self):
exc = APIException(('Invalid name', 'Invalid age'))
context = {}
response = exception_handler(exc, context)
expected = {
'errors': [
{
'field': None,
'messages': ['Invalid name', 'Invalid age'],
},
],
}
self.assertEqual(expected, response.data)

def test_exception_handler_returns_correct_response_if_detail_is_dict_which_contains_list_value(self):
exc = APIException(
{
'name': [
'This field is required',
'This field can not be null',
],
},
)
context = {}
response = exception_handler(exc, context)
expected = {
'errors': [
{
'field': 'name',
'messages': ['This field is required', 'This field can not be null'],
}
]
},
],
}
self.assertEqual(expected, response.data)