mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-06 21:40:13 +03:00
adds test for raising uncaught exc in serializer
Serializers should certainly except `ValidationError`s and respond with the appropriate `400 Bad Request`. This commit adds a test to check that it remains possible to raise another exception - e.g. deriving `APIException` - that escapes the serializer and is handled elsewhere - or not, as the case may be. It turns out that this behaviour is already supported, but it wasn't clear that it was (or was by-design) from reading the existing tests; while it _seemed_ to be the case from the source, it's not as obvious as it would be if there's an explicit test.
This commit is contained in:
parent
e7eccac6df
commit
a2898d71c0
|
@ -61,6 +61,19 @@ class TestSerializer:
|
|||
with pytest.raises(AssertionError):
|
||||
serializer.save()
|
||||
|
||||
def test_non_validation_error_escapes(self):
|
||||
from rest_framework.exceptions import APIException
|
||||
|
||||
class CustomException(APIException):
|
||||
@classmethod
|
||||
def throw(self, *a, **kw):
|
||||
raise self
|
||||
|
||||
serializer = self.Serializer(data={'char': 'abc', 'integer': 123})
|
||||
serializer.validate_char = CustomException.throw
|
||||
with pytest.raises(CustomException):
|
||||
serializer.is_valid()
|
||||
|
||||
|
||||
class TestValidateMethod:
|
||||
def test_non_field_error_validate_method(self):
|
||||
|
|
Loading…
Reference in New Issue
Block a user