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