2016-02-07 19:26:37 +03:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
from django.test import TestCase
|
2016-11-01 13:38:56 +03:00
|
|
|
from django.utils import six
|
2016-02-07 19:26:37 +03:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
2016-11-01 13:38:56 +03:00
|
|
|
from rest_framework.exceptions import (
|
|
|
|
ErrorDetail, Throttled, _get_error_details
|
|
|
|
)
|
2016-02-07 19:26:37 +03:00
|
|
|
|
|
|
|
|
|
|
|
class ExceptionTestCase(TestCase):
|
|
|
|
|
2016-10-11 12:25:21 +03:00
|
|
|
def test_get_error_details(self):
|
|
|
|
|
|
|
|
example = "string"
|
|
|
|
lazy_example = _(example)
|
|
|
|
|
2016-11-30 14:12:01 +03:00
|
|
|
assert _get_error_details(lazy_example) == example
|
|
|
|
|
2016-10-11 12:25:21 +03:00
|
|
|
assert isinstance(
|
|
|
|
_get_error_details(lazy_example),
|
|
|
|
ErrorDetail
|
|
|
|
)
|
|
|
|
|
2016-11-30 14:12:01 +03:00
|
|
|
assert _get_error_details({'nested': lazy_example})['nested'] == example
|
|
|
|
|
2016-10-11 12:25:21 +03:00
|
|
|
assert isinstance(
|
|
|
|
_get_error_details({'nested': lazy_example})['nested'],
|
|
|
|
ErrorDetail
|
|
|
|
)
|
|
|
|
|
2016-11-30 14:12:01 +03:00
|
|
|
assert _get_error_details([[lazy_example]])[0][0] == example
|
|
|
|
|
2016-10-11 12:25:21 +03:00
|
|
|
assert isinstance(
|
|
|
|
_get_error_details([[lazy_example]])[0][0],
|
|
|
|
ErrorDetail
|
|
|
|
)
|
2016-11-01 13:38:56 +03:00
|
|
|
|
|
|
|
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'}
|