From ecadfc322018b8d57e583be9a93de934ad5a983a Mon Sep 17 00:00:00 2001 From: Ryan P Kilby Date: Thu, 21 Jun 2018 20:58:58 -0400 Subject: [PATCH] Test wrapped TypeError/ValueError handling --- tests/test_relations.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) 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):