Skip to content

Commit bada2f5

Browse files
author
Tim Csitkovics
committed
Include test case
1 parent fc1e822 commit bada2f5

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

example/tests/test_serializers.py

+35-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
from collections import namedtuple
2+
13
import pytest
24
from django.test import TestCase
35
from django.urls import reverse
46
from django.utils import timezone
57

6-
from rest_framework_json_api.serializers import ResourceIdentifierObjectSerializer
8+
from rest_framework_json_api.serializers import ResourceIdentifierObjectSerializer, ModelSerializer
79
from rest_framework_json_api.utils import format_resource_type
810

911
from example.models import Author, Blog, Entry
@@ -18,8 +20,8 @@ def setUp(self):
1820
blog=self.blog,
1921
headline='headline',
2022
body_text='body_text',
21-
pub_date=timezone.now(),
22-
mod_date=timezone.now(),
23+
pub_date=timezone.now().date(),
24+
mod_date=timezone.now().date(),
2325
n_comments=0,
2426
n_pingbacks=0,
2527
rating=3
@@ -30,6 +32,36 @@ def setUp(self):
3032
Author.objects.create(name=name, email='{}@example.org'.format(name))
3133
)
3234

35+
def test_forward_relationship_not_loaded_when_not_included(self):
36+
MockRequest = namedtuple('Request', ['query_params'])
37+
request_without_includes = MockRequest({})
38+
to_representation_was_called = False
39+
40+
class BlogSerializer(ModelSerializer):
41+
class Meta:
42+
model = Blog
43+
fields = '__all__'
44+
45+
def to_representation(self, instance):
46+
nonlocal to_representation_was_called
47+
to_representation_was_called = True
48+
return super().to_representation(instance)
49+
50+
class EntrySerializer(ModelSerializer):
51+
blog = BlogSerializer()
52+
53+
class Meta:
54+
model = Entry
55+
fields = '__all__'
56+
57+
included_serializers = {
58+
'blog': BlogSerializer,
59+
}
60+
61+
serializer = EntrySerializer(context={'request': request_without_includes})
62+
serializer.to_representation(self.entry)
63+
self.assertFalse(to_representation_was_called)
64+
3365
def test_data_in_correct_format_when_instantiated_with_blog_object(self):
3466
serializer = ResourceIdentifierObjectSerializer(instance=self.blog)
3567

rest_framework_json_api/serializers.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -182,17 +182,16 @@ def to_representation(self, instance):
182182

183183
for field in readable_fields:
184184
try:
185-
context = getattr(self, '_context', {})
186-
request = context.get('request', None)
187-
is_included = field.source in get_included_resources(request)
185+
request = self.context.get('request', None)
186+
is_included = field.source in get_included_resources(request, self)
188187
if not is_included \
189188
and isinstance(field, ModelSerializer) \
190189
and hasattr(instance, field.source + "_id"):
191190
attribute = getattr(instance, field.source + "_id")
192191
if attribute is None:
193192
ret[field.field_name] = None
194193
continue
195-
resource_type = get_resource_type_from_instance(field)
194+
resource_type = get_resource_type_from_serializer(field)
196195
if resource_type:
197196
ret[field.field_name] = OrderedDict([("type", resource_type),
198197
("id", attribute)])

0 commit comments

Comments
 (0)