1
- from collections import namedtuple
1
+ from collections import OrderedDict
2
+ from unittest .mock import patch
2
3
3
4
import pytest
4
5
from django .test import TestCase
5
6
from django .urls import reverse
6
7
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
+ )
9
16
from rest_framework_json_api .utils import format_resource_type
10
17
11
18
from example .models import Author , Blog , Entry
19
+ from example .serializers import BlogSerializer
12
20
21
+ factory = APIRequestFactory ()
13
22
pytestmark = pytest .mark .django_db
14
23
15
24
16
25
class TestResourceIdentifierObjectSerializer (TestCase ):
17
26
def setUp (self ):
18
27
self .blog = Blog .objects .create (name = 'Some Blog' , tagline = "It's a blog" )
28
+ self .now = timezone .now ()
29
+
19
30
self .entry = Entry .objects .create (
20
31
blog = self .blog ,
21
32
headline = 'headline' ,
22
33
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 (),
25
36
n_comments = 0 ,
26
37
n_pingbacks = 0 ,
27
38
rating = 3
@@ -33,30 +44,56 @@ def setUp(self):
33
44
)
34
45
35
46
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 ()
38
50
39
- class BlogSerializer (ModelSerializer ):
40
- class Meta :
41
- model = Blog
42
- fields = '__all__'
51
+ class Meta :
52
+ model = Entry
53
+ fields = '__all__'
43
54
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 )
46
58
59
+ mock .assert_not_called ()
60
+
61
+ def test_forward_relationship_optimization_correct_representation (self ):
47
62
class EntrySerializer (ModelSerializer ):
48
63
blog = BlogSerializer ()
49
64
50
65
class Meta :
51
66
model = Entry
52
67
fields = '__all__'
53
68
54
- included_serializers = {
55
- 'blog' : BlogSerializer ,
56
- }
57
-
69
+ request_without_includes = Request (factory .get ('/' ))
58
70
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 )
60
97
61
98
def test_data_in_correct_format_when_instantiated_with_blog_object (self ):
62
99
serializer = ResourceIdentifierObjectSerializer (instance = self .blog )
0 commit comments