Add failing test for fallback to pk-only optimization

Not sure if this is the right place to put the test or where the situation comes up in the real world when not using ORM hacks (like how we found it), but it *does* test the thing.
This commit is contained in:
Ryan O’Hara 2018-01-15 14:58:38 -08:00
parent 2709de1310
commit fa5ad1d0d9

View File

@ -3,7 +3,7 @@ import uuid
import pytest import pytest
from _pytest.monkeypatch import MonkeyPatch from _pytest.monkeypatch import MonkeyPatch
from django.conf.urls import url from django.conf.urls import url
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist
from django.test import override_settings from django.test import override_settings
from django.utils.datastructures import MultiValueDict from django.utils.datastructures import MultiValueDict
@ -167,6 +167,22 @@ class TestHyperlinkedRelatedField(APISimpleTestCase):
representation = self.field.to_representation(MockObject(pk='')) representation = self.field.to_representation(MockObject(pk=''))
assert representation is None assert representation is None
def test_serialize_empty_relationship_attribute(self):
class TestSerializer(serializers.Serializer):
via_unreachable = serializers.HyperlinkedRelatedField(
source='does_not_exist.unreachable',
view_name='example',
read_only=True,
)
class TestSerializable:
@property
def does_not_exist(self):
raise ObjectDoesNotExist
serializer = TestSerializer(TestSerializable())
assert serializer.data == {'via_unreachable': None}
def test_hyperlinked_related_lookup_exists(self): def test_hyperlinked_related_lookup_exists(self):
instance = self.field.to_internal_value('http://example.org/example/foobar/') instance = self.field.to_internal_value('http://example.org/example/foobar/')
assert instance is self.queryset.items[0] assert instance is self.queryset.items[0]