diff --git a/tests/test_relations.py b/tests/test_relations.py index c2a8adaf2..3c76f009e 100644 --- a/tests/test_relations.py +++ b/tests/test_relations.py @@ -206,6 +206,27 @@ class TestHyperlinkedRelatedField(APISimpleTestCase): with pytest.raises(TypeError): 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): def setUp(self):