django-rest-framework/tests/test_exceptions.py
Nicolas Delaby 97d848413e Fix support of get_full_details() for Throttled exceptions (#4627)
Since `str` objects are immutable, appending to existing `str` creates
in fact a new `str` instance.

Thus `ErrorDetail.detail.code` attribute is lost after `str` concatenation operation.
2016-11-01 10:38:56 +00:00

60 lines
1.7 KiB
Python

from __future__ import unicode_literals
from django.test import TestCase
from django.utils import six
from django.utils.translation import ugettext_lazy as _
from rest_framework.exceptions import (
ErrorDetail, Throttled, _get_error_details
)
class ExceptionTestCase(TestCase):
def test_get_error_details(self):
example = "string"
lazy_example = _(example)
self.assertEqual(
_get_error_details(lazy_example),
example
)
assert isinstance(
_get_error_details(lazy_example),
ErrorDetail
)
self.assertEqual(
_get_error_details({'nested': lazy_example})['nested'],
example
)
assert isinstance(
_get_error_details({'nested': lazy_example})['nested'],
ErrorDetail
)
self.assertEqual(
_get_error_details([[lazy_example]])[0][0],
example
)
assert isinstance(
_get_error_details([[lazy_example]])[0][0],
ErrorDetail
)
def test_get_full_details_with_throttling(self):
exception = Throttled()
assert exception.get_full_details() == {
'message': 'Request was throttled.', 'code': 'throttled'}
exception = Throttled(wait=2)
assert exception.get_full_details() == {
'message': 'Request was throttled. Expected available in {} seconds.'.format(2 if six.PY3 else 2.),
'code': 'throttled'}
exception = Throttled(wait=2, detail='Slow down!')
assert exception.get_full_details() == {
'message': 'Slow down! Expected available in {} seconds.'.format(2 if six.PY3 else 2.),
'code': 'throttled'}