1
+ from collections import OrderedDict
2
+
1
3
import pytest
2
4
from django .test import TestCase
3
5
from django .urls import reverse
4
6
from django .utils import timezone
5
-
6
- from rest_framework_json_api .serializers import ResourceIdentifierObjectSerializer
7
+ from rest_framework .request import Request
8
+ from rest_framework .test import APIRequestFactory
9
+
10
+ from rest_framework_json_api .serializers import (
11
+ DateField ,
12
+ ModelSerializer ,
13
+ ResourceIdentifierObjectSerializer
14
+ )
7
15
from rest_framework_json_api .utils import format_resource_type
8
16
9
17
from example .models import Author , Blog , Entry
18
+ from example .serializers import BlogSerializer
19
+
20
+ try :
21
+ from unittest import mock
22
+ except ImportError :
23
+ import mock
10
24
25
+ request_factory = APIRequestFactory ()
11
26
pytestmark = pytest .mark .django_db
12
27
13
28
14
29
class TestResourceIdentifierObjectSerializer (TestCase ):
15
30
def setUp (self ):
16
31
self .blog = Blog .objects .create (name = 'Some Blog' , tagline = "It's a blog" )
32
+ now = timezone .now ()
33
+
17
34
self .entry = Entry .objects .create (
18
35
blog = self .blog ,
19
36
headline = 'headline' ,
20
37
body_text = 'body_text' ,
21
- pub_date = timezone . now (),
22
- mod_date = timezone . now (),
38
+ pub_date = now . date (),
39
+ mod_date = now . date (),
23
40
n_comments = 0 ,
24
41
n_pingbacks = 0 ,
25
42
rating = 3
@@ -30,6 +47,59 @@ def setUp(self):
30
47
Author .objects .create (name = name , email = '{}@example.org' .format (name ))
31
48
)
32
49
50
+ def test_forward_relationship_not_loaded_when_not_included (self ):
51
+ to_representation_method = 'example.serializers.BlogSerializer.to_representation'
52
+ with mock .patch (to_representation_method ) as mocked_serializer :
53
+ class EntrySerializer (ModelSerializer ):
54
+ blog = BlogSerializer ()
55
+
56
+ class Meta :
57
+ model = Entry
58
+ fields = '__all__'
59
+
60
+ request_without_includes = Request (request_factory .get ('/' ))
61
+ serializer = EntrySerializer (context = {'request' : request_without_includes })
62
+ serializer .to_representation (self .entry )
63
+
64
+ mocked_serializer .assert_not_called ()
65
+
66
+ def test_forward_relationship_optimization_correct_representation (self ):
67
+ class EntrySerializer (ModelSerializer ):
68
+ blog = BlogSerializer ()
69
+
70
+ class Meta :
71
+ model = Entry
72
+ fields = '__all__'
73
+
74
+ request_without_includes = Request (request_factory .get ('/' ))
75
+ serializer = EntrySerializer (context = {'request' : request_without_includes })
76
+ result = serializer .to_representation (self .entry )
77
+
78
+ # Remove non deterministic fields
79
+ result .pop ('created_at' )
80
+ result .pop ('modified_at' )
81
+
82
+ expected = OrderedDict (
83
+ [
84
+ ('id' , 1 ),
85
+ ('blog' , OrderedDict ([('type' , 'blogs' ), ('id' , 1 )])),
86
+ ('headline' , 'headline' ),
87
+ ('body_text' , 'body_text' ),
88
+ ('pub_date' , DateField ().to_representation (self .entry .pub_date )),
89
+ ('mod_date' , DateField ().to_representation (self .entry .mod_date )),
90
+ ('n_comments' , 0 ),
91
+ ('n_pingbacks' , 0 ),
92
+ ('rating' , 3 ),
93
+ ('authors' ,
94
+ [
95
+ OrderedDict ([('type' , 'authors' ), ('id' , '1' )]),
96
+ OrderedDict ([('type' , 'authors' ), ('id' , '2' )]),
97
+ OrderedDict ([('type' , 'authors' ), ('id' , '3' )]),
98
+ OrderedDict ([('type' , 'authors' ), ('id' , '4' )]),
99
+ OrderedDict ([('type' , 'authors' ), ('id' , '5' )])])])
100
+
101
+ self .assertEqual (expected , result )
102
+
33
103
def test_data_in_correct_format_when_instantiated_with_blog_object (self ):
34
104
serializer = ResourceIdentifierObjectSerializer (instance = self .blog )
35
105
0 commit comments