22
22
except ImportError :
23
23
import mock
24
24
25
- factory = APIRequestFactory ()
25
+ request_factory = APIRequestFactory ()
26
26
pytestmark = pytest .mark .django_db
27
27
28
28
29
29
class TestResourceIdentifierObjectSerializer (TestCase ):
30
30
def setUp (self ):
31
31
self .blog = Blog .objects .create (name = 'Some Blog' , tagline = "It's a blog" )
32
- self . now = timezone .now ()
32
+ now = timezone .now ()
33
33
34
34
self .entry = Entry .objects .create (
35
35
blog = self .blog ,
36
36
headline = 'headline' ,
37
37
body_text = 'body_text' ,
38
- pub_date = self . now .date (),
39
- mod_date = self . now .date (),
38
+ pub_date = now .date (),
39
+ mod_date = now .date (),
40
40
n_comments = 0 ,
41
41
n_pingbacks = 0 ,
42
42
rating = 3
@@ -48,15 +48,16 @@ def setUp(self):
48
48
)
49
49
50
50
def test_forward_relationship_not_loaded_when_not_included (self ):
51
- with mock .patch ('example.serializers.BlogSerializer.to_representation' ) as mocked_serializer :
51
+ to_representation_method = 'example.serializers.BlogSerializer.to_representation'
52
+ with mock .patch (to_representation_method ) as mocked_serializer :
52
53
class EntrySerializer (ModelSerializer ):
53
54
blog = BlogSerializer ()
54
55
55
56
class Meta :
56
57
model = Entry
57
58
fields = '__all__'
58
59
59
- request_without_includes = Request (factory .get ('/' ))
60
+ request_without_includes = Request (request_factory .get ('/' ))
60
61
serializer = EntrySerializer (context = {'request' : request_without_includes })
61
62
serializer .to_representation (self .entry )
62
63
@@ -70,7 +71,7 @@ class Meta:
70
71
model = Entry
71
72
fields = '__all__'
72
73
73
- request_without_includes = Request (factory .get ('/' ))
74
+ request_without_includes = Request (request_factory .get ('/' ))
74
75
serializer = EntrySerializer (context = {'request' : request_without_includes })
75
76
result = serializer .to_representation (self .entry )
76
77
@@ -84,8 +85,8 @@ class Meta:
84
85
('blog' , OrderedDict ([('type' , 'blogs' ), ('id' , 1 )])),
85
86
('headline' , 'headline' ),
86
87
('body_text' , 'body_text' ),
87
- ('pub_date' , DateField ().to_representation (self .now . date () )),
88
- ('mod_date' , DateField ().to_representation (self .now . date () )),
88
+ ('pub_date' , DateField ().to_representation (self .entry . pub_date )),
89
+ ('mod_date' , DateField ().to_representation (self .entry . mod_date )),
89
90
('n_comments' , 0 ),
90
91
('n_pingbacks' , 0 ),
91
92
('rating' , 3 ),
0 commit comments