Skip to content

Commit 959aec7

Browse files
slivercn2ygk
andauthored
Change default behavior of tests to use nested serializers (#816)
Co-authored-by: Alan Crosswell <[email protected]>
1 parent 8dbe008 commit 959aec7

10 files changed

+25
-39
lines changed

example/serializers.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ class Meta:
4040

4141
class BlogSerializer(serializers.ModelSerializer):
4242
copyright = serializers.SerializerMethodField()
43-
tags = TaggedItemSerializer(many=True, read_only=True)
43+
tags = relations.ResourceRelatedField(many=True, read_only=True)
4444

45-
include_serializers = {
45+
included_serializers = {
4646
'tags': 'example.serializers.TaggedItemSerializer',
4747
}
4848

@@ -147,7 +147,7 @@ def __init__(self, *args, **kwargs):
147147
model=Entry,
148148
read_only=True
149149
)
150-
tags = TaggedItemSerializer(many=True, read_only=True)
150+
tags = relations.ResourceRelatedField(many=True, read_only=True)
151151

152152
def get_suggested(self, obj):
153153
return Entry.objects.exclude(pk=obj.pk)

example/settings/dev.py

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767

6868
JSON_API_FORMAT_FIELD_NAMES = 'camelize'
6969
JSON_API_FORMAT_TYPES = 'camelize'
70+
JSON_API_SERIALIZE_NESTED_SERIALIZERS_AS_ATTRIBUTE = True
7071
REST_FRAMEWORK = {
7172
'PAGE_SIZE': 5,
7273
'EXCEPTION_HANDLER': 'rest_framework_json_api.exceptions.exception_handler',

example/tests/integration/test_meta.py

+2-10
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ def test_top_level_meta_for_list_view(blog, client):
1818
"links": {
1919
"self": 'http://testserver/blogs/1'
2020
},
21-
"relationships": {
22-
"tags": {
23-
"data": []
24-
}
25-
},
21+
'relationships': {'tags': {'data': [], 'meta': {'count': 0}}},
2622
"meta": {
2723
"copyright": datetime.now().year
2824
},
@@ -53,11 +49,7 @@ def test_top_level_meta_for_detail_view(blog, client):
5349
"attributes": {
5450
"name": blog.name
5551
},
56-
"relationships": {
57-
"tags": {
58-
"data": []
59-
}
60-
},
52+
'relationships': {'tags': {'data': [], 'meta': {'count': 0}}},
6153
"links": {
6254
"self": "http://testserver/blogs/1"
6355
},

example/tests/integration/test_non_paginated_responses.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ def test_multiple_entries_no_pagination(multiple_entries, client):
7272
"self": "http://testserver/entries/1/relationships/featured_hyperlinked"
7373
}
7474
},
75-
"tags": {
76-
"data": []
77-
}
75+
'tags': {'data': [], 'meta': {'count': 0}},
7876
}
7977
},
8078
{
@@ -135,9 +133,7 @@ def test_multiple_entries_no_pagination(multiple_entries, client):
135133
"self": "http://testserver/entries/2/relationships/featured_hyperlinked"
136134
}
137135
},
138-
"tags": {
139-
"data": []
140-
}
136+
'tags': {'data': [], 'meta': {'count': 0}},
141137
}
142138
},
143139
]

example/tests/integration/test_pagination.py

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ def test_pagination_with_single_entry(single_entry, client):
7373
}
7474
},
7575
"tags": {
76+
'meta': {'count': 1},
7677
"data": [
7778
{
7879
"id": "1",

example/tests/test_filters.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -461,9 +461,7 @@ def test_search_keywords(self):
461461
'self': 'http://testserver/entries/7/relationships/suggested_hyperlinked', # noqa: E501
462462
'related': 'http://testserver/entries/7/suggested/'}
463463
},
464-
'tags': {
465-
'data': []
466-
},
464+
'tags': {'data': [], 'meta': {'count': 0}},
467465
'featuredHyperlinked': {
468466
'links': {
469467
'self': 'http://testserver/entries/7/relationships/featured_hyperlinked', # noqa: E501

example/tests/test_serializers.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def setUp(self):
4545
)
4646

4747
def test_forward_relationship_not_loaded_when_not_included(self):
48-
to_representation_method = 'example.serializers.BlogSerializer.to_representation'
48+
to_representation_method = 'example.serializers.TaggedItemSerializer.to_representation'
4949
with mock.patch(to_representation_method) as mocked_serializer:
5050
class EntrySerializer(ModelSerializer):
5151
blog = BlogSerializer()
@@ -79,7 +79,12 @@ class Meta:
7979
expected = dict(
8080
[
8181
('id', 1),
82-
('blog', dict([('type', 'blogs'), ('id', 1)])),
82+
('blog', dict([
83+
('name', 'Some Blog'),
84+
('tags', []),
85+
('copyright', 2020),
86+
('url', 'http://testserver/blogs/1')
87+
])),
8388
('headline', 'headline'),
8489
('body_text', 'body_text'),
8590
('pub_date', DateField().to_representation(self.entry.pub_date)),

example/tests/test_views.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ def test_get_object_gives_correct_blog(self):
538538
'id': '{}'.format(self.blog.id),
539539
'links': {'self': 'http://testserver/blogs/{}'.format(self.blog.id)},
540540
'meta': {'copyright': datetime.now().year},
541-
'relationships': {'tags': {'data': []}},
541+
'relationships': {'tags': {'data': [], 'meta': {'count': 0}}},
542542
'type': 'blogs'
543543
},
544544
'meta': {'apiDocs': '/docs/api/blogs'}
@@ -632,7 +632,7 @@ def test_get_object_gives_correct_entry(self):
632632
'/suggested_hyperlinked'.format(self.second_entry.id)
633633
}
634634
},
635-
'tags': {'data': []}},
635+
'tags': {'data': [], 'meta': {'count': 0}}},
636636
'type': 'posts'
637637
}
638638
}

example/tests/unit/test_default_drf_serializers.py

+6-12
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,10 @@ def test_blog_create(client):
9595

9696
expected = {
9797
'data': {
98-
'attributes': {'name': blog.name},
98+
'attributes': {'name': blog.name, 'tags': []},
9999
'id': '{}'.format(blog.id),
100100
'links': {'self': 'http://testserver/blogs/{}'.format(blog.id)},
101101
'meta': {'copyright': datetime.now().year},
102-
'relationships': {'tags': {'data': []}},
103102
'type': 'blogs'
104103
},
105104
'meta': {'apiDocs': '/docs/api/blogs'}
@@ -116,11 +115,10 @@ def test_get_object_gives_correct_blog(client, blog, entry):
116115
resp = client.get(url)
117116
expected = {
118117
'data': {
119-
'attributes': {'name': blog.name},
118+
'attributes': {'name': blog.name, 'tags': []},
120119
'id': '{}'.format(blog.id),
121120
'links': {'self': 'http://testserver/blogs/{}'.format(blog.id)},
122121
'meta': {'copyright': datetime.now().year},
123-
'relationships': {'tags': {'data': []}},
124122
'type': 'blogs'
125123
},
126124
'meta': {'apiDocs': '/docs/api/blogs'}
@@ -154,11 +152,10 @@ def test_get_object_patches_correct_blog(client, blog, entry):
154152

155153
expected = {
156154
'data': {
157-
'attributes': {'name': new_name},
155+
'attributes': {'name': new_name, 'tags': []},
158156
'id': '{}'.format(blog.id),
159157
'links': {'self': 'http://testserver/blogs/{}'.format(blog.id)},
160158
'meta': {'copyright': datetime.now().year},
161-
'relationships': {'tags': {'data': []}},
162159
'type': 'blogs'
163160
},
164161
'meta': {'apiDocs': '/docs/api/blogs'}
@@ -189,17 +186,14 @@ def test_get_entry_list_with_blogs(client, entry):
189186
'first': 'http://testserver/drf-entries/1/suggested/?page%5Bnumber%5D=1',
190187
'last': 'http://testserver/drf-entries/1/suggested/?page%5Bnumber%5D=1',
191188
'next': None,
192-
'prev': None
189+
'prev': None,
193190
},
194191
'data': [
195192
{
196193
'type': 'entries',
197194
'id': '1',
198-
'attributes': {},
199-
'relationships': {
200-
'tags': {
201-
'data': []
202-
}
195+
'attributes': {
196+
'tags': [],
203197
},
204198
'links': {
205199
'self': 'http://testserver/drf-blogs/1'

pytest.ini

-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@ DJANGO_SETTINGS_MODULE=example.settings.test
33
filterwarnings =
44
error::DeprecationWarning
55
error::PendingDeprecationWarning
6-
ignore::DeprecationWarning:rest_framework_json_api.serializers

0 commit comments

Comments
 (0)