Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit bd2a0c8

Browse files
author
Tim Csitkovics
committedAug 29, 2018
Implement suggested changes
1 parent eb70f3a commit bd2a0c8

File tree

2 files changed

+56
-19
lines changed

2 files changed

+56
-19
lines changed
 

‎example/tests/test_serializers.py

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,38 @@
1-
from collections import namedtuple
1+
from collections import OrderedDict
2+
from unittest.mock import patch
23

34
import pytest
45
from django.test import TestCase
56
from django.urls import reverse
67
from django.utils import timezone
7-
8-
from rest_framework_json_api.serializers import ModelSerializer, ResourceIdentifierObjectSerializer
8+
from rest_framework.request import Request
9+
from rest_framework.test import APIRequestFactory
10+
11+
from rest_framework_json_api.serializers import (
12+
DateField,
13+
ModelSerializer,
14+
ResourceIdentifierObjectSerializer
15+
)
916
from rest_framework_json_api.utils import format_resource_type
1017

1118
from example.models import Author, Blog, Entry
19+
from example.serializers import BlogSerializer
1220

21+
factory = APIRequestFactory()
1322
pytestmark = pytest.mark.django_db
1423

1524

1625
class TestResourceIdentifierObjectSerializer(TestCase):
1726
def setUp(self):
1827
self.blog = Blog.objects.create(name='Some Blog', tagline="It's a blog")
28+
self.now = timezone.now()
29+
1930
self.entry = Entry.objects.create(
2031
blog=self.blog,
2132
headline='headline',
2233
body_text='body_text',
23-
pub_date=timezone.now().date(),
24-
mod_date=timezone.now().date(),
34+
pub_date=self.now.date(),
35+
mod_date=self.now.date(),
2536
n_comments=0,
2637
n_pingbacks=0,
2738
rating=3
@@ -33,30 +44,56 @@ def setUp(self):
3344
)
3445

3546
def test_forward_relationship_not_loaded_when_not_included(self):
36-
MockRequest = namedtuple('Request', ['query_params'])
37-
request_without_includes = MockRequest({})
47+
with patch('example.serializers.BlogSerializer.to_representation') as mock:
48+
class EntrySerializer(ModelSerializer):
49+
blog = BlogSerializer()
3850

39-
class BlogSerializer(ModelSerializer):
40-
class Meta:
41-
model = Blog
42-
fields = '__all__'
51+
class Meta:
52+
model = Entry
53+
fields = '__all__'
4354

44-
def to_representation(self, instance):
45-
raise Exception('to_representation of BlogSerializer was called')
55+
request_without_includes = Request(factory.get('/'))
56+
serializer = EntrySerializer(context={'request': request_without_includes})
57+
serializer.to_representation(self.entry)
4658

59+
mock.assert_not_called()
60+
61+
def test_forward_relationship_optimization_correct_representation(self):
4762
class EntrySerializer(ModelSerializer):
4863
blog = BlogSerializer()
4964

5065
class Meta:
5166
model = Entry
5267
fields = '__all__'
5368

54-
included_serializers = {
55-
'blog': BlogSerializer,
56-
}
57-
69+
request_without_includes = Request(factory.get('/'))
5870
serializer = EntrySerializer(context={'request': request_without_includes})
59-
serializer.to_representation(self.entry)
71+
result = serializer.to_representation(self.entry)
72+
73+
# Remove non deterministic fields
74+
result.pop('created_at')
75+
result.pop('modified_at')
76+
77+
expected = OrderedDict(
78+
[
79+
('id', 1),
80+
('blog', OrderedDict([('type', 'blogs'), ('id', 1)])),
81+
('headline', 'headline'),
82+
('body_text', 'body_text'),
83+
('pub_date', DateField().to_representation(self.now.date())),
84+
('mod_date', DateField().to_representation(self.now.date())),
85+
('n_comments', 0),
86+
('n_pingbacks', 0),
87+
('rating', 3),
88+
('authors',
89+
[
90+
OrderedDict([('type', 'authors'), ('id', '1')]),
91+
OrderedDict([('type', 'authors'), ('id', '2')]),
92+
OrderedDict([('type', 'authors'), ('id', '3')]),
93+
OrderedDict([('type', 'authors'), ('id', '4')]),
94+
OrderedDict([('type', 'authors'), ('id', '5')])])])
95+
96+
self.assertEqual(expected, result)
6097

6198
def test_data_in_correct_format_when_instantiated_with_blog_object(self):
6299
serializer = ResourceIdentifierObjectSerializer(instance=self.blog)

‎rest_framework_json_api/serializers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def to_representation(self, instance):
190190
return ret
191191

192192
def _get_field_representation(self, field, instance):
193-
request = self.context.get('request', None)
193+
request = self.context.get('request')
194194
is_included = field.source in get_included_resources(request)
195195
if not is_included and \
196196
isinstance(field, ModelSerializer) and \

0 commit comments

Comments
 (0)
Please sign in to comment.