Test wrapped TypeError/ValueError handling

This commit is contained in:
Ryan P Kilby 2018-06-21 20:58:58 -04:00 committed by Carlton Gibson
parent 7c834f25e4
commit ecadfc3220

View File

@ -206,6 +206,27 @@ class TestHyperlinkedRelatedField(APISimpleTestCase):
with pytest.raises(TypeError): with pytest.raises(TypeError):
field.to_internal_value('http://example.org/example/doesnotexist/') field.to_internal_value('http://example.org/example/doesnotexist/')
def hyperlinked_related_queryset_error(self, exc_type):
class QuerySet:
def get(self, *args, **kwargs):
raise exc_type
field = serializers.HyperlinkedRelatedField(
view_name='example',
lookup_field='name',
queryset=QuerySet(),
)
with pytest.raises(serializers.ValidationError) as excinfo:
field.to_internal_value('http://example.org/example/doesnotexist/')
msg = excinfo.value.detail[0]
assert msg == 'Invalid hyperlink - Object does not exist.'
def test_hyperlinked_related_queryset_type_error(self):
self.hyperlinked_related_queryset_error(TypeError)
def test_hyperlinked_related_queryset_value_error(self):
self.hyperlinked_related_queryset_error(ValueError)
class TestHyperlinkedIdentityField(APISimpleTestCase): class TestHyperlinkedIdentityField(APISimpleTestCase):
def setUp(self): def setUp(self):