forked from django-json-api/django-rest-framework-json-api
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_serializers.py
113 lines (86 loc) · 3.91 KB
/
test_serializers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
fromdjango.core.urlresolversimportreverse
fromdjango.testimportTestCase
fromdjango.utilsimporttimezone
fromrest_framework_json_api.utilsimportformat_resource_type
fromrest_framework_json_api.serializersimportResourceIdentifierObjectSerializer
fromexample.modelsimportBlog, Entry, Author
importpytest
fromexample.tests.utilsimportload_json
pytestmark=pytest.mark.django_db
classTestResourceIdentifierObjectSerializer(TestCase):
defsetUp(self):
self.blog=Blog.objects.create(name='Some Blog', tagline="It's a blog")
self.entry=Entry.objects.create(
blog=self.blog,
headline='headline',
body_text='body_text',
pub_date=timezone.now(),
mod_date=timezone.now(),
n_comments=0,
n_pingbacks=0,
rating=3
)
foriinrange(1,6):
name='some_author{}'.format(i)
self.entry.authors.add(
Author.objects.create(name=name, email='{}@example.org'.format(name))
)
deftest_data_in_correct_format_when_instantiated_with_blog_object(self):
serializer=ResourceIdentifierObjectSerializer(instance=self.blog)
expected_data= {'type': format_resource_type('Blog'), 'id': str(self.blog.id)}
assertserializer.data==expected_data
deftest_data_in_correct_format_when_instantiated_with_entry_object(self):
serializer=ResourceIdentifierObjectSerializer(instance=self.entry)
expected_data= {'type': format_resource_type('Entry'), 'id': str(self.entry.id)}
assertserializer.data==expected_data
deftest_deserialize_primitive_data_blog(self):
initial_data= {
'type': format_resource_type('Blog'),
'id': str(self.blog.id)
}
serializer=ResourceIdentifierObjectSerializer(data=initial_data, model_class=Blog)
self.assertTrue(serializer.is_valid(), msg=serializer.errors)
assertserializer.validated_data==self.blog
deftest_data_in_correct_format_when_instantiated_with_queryset(self):
qs=Author.objects.all()
serializer=ResourceIdentifierObjectSerializer(instance=qs, many=True)
type_string=format_resource_type('Author')
author_pks=Author.objects.values_list('pk', flat=True)
expected_data= [{'type': type_string, 'id': str(pk)} forpkinauthor_pks]
assertserializer.data==expected_data
deftest_deserialize_many(self):
type_string=format_resource_type('Author')
author_pks=Author.objects.values_list('pk', flat=True)
initial_data= [{'type': type_string, 'id': str(pk)} forpkinauthor_pks]
serializer=ResourceIdentifierObjectSerializer(data=initial_data, model_class=Author, many=True)
self.assertTrue(serializer.is_valid(), msg=serializer.errors)
print(serializer.data)
classTestModelSerializer(object):
deftest_model_serializer_with_implicit_fields(self, comment, client):
expected= {
"data": {
"type": "comments",
"id": str(comment.pk),
"attributes": {
"body": comment.body
},
"relationships": {
"entry": {
"data": {
"type": "entries",
"id": str(comment.entry.pk)
}
},
"author": {
"data": {
"type": "authors",
"id": str(comment.author.pk)
}
},
}
}
}
response=client.get(reverse("comment-detail", kwargs={'pk': comment.pk}))
assertresponse.status_code==200
parsed_content=load_json(response.content)
assertexpected==parsed_content