Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ff85259

Browse files
committed
Change default behavior of tests to use nested serializers
1 parent fbf9545 commit ff85259

File tree

10 files changed

+30
-44
lines changed

10 files changed

+30
-44
lines changed

‎example/serializers.py‎

Lines changed: 3 additions & 3 deletions
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/tests/integration/test_meta.py‎

Lines changed: 2 additions & 10 deletions
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‎

Lines changed: 2 additions & 6 deletions
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‎

Lines changed: 1 addition & 0 deletions
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‎

Lines changed: 1 addition & 3 deletions
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_generic_viewset.py‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,6 @@ def test_custom_validation_exceptions(self):
9595
"""
9696
expected = {
9797
'errors': [
98-
{
99-
'id': 'armageddon101',
100-
'detail': 'Hey! You need a last name!',
101-
'meta': 'something',
102-
},
10398
{
10499
'status': '400',
105100
'source': {
@@ -108,6 +103,12 @@ def test_custom_validation_exceptions(self):
108103
'detail': 'Enter a valid email address.',
109104
'code': 'invalid',
110105
},
106+
{
107+
'id': 'armageddon101',
108+
'detail': 'Hey! You need a last name!',
109+
'meta': 'something',
110+
'source': {'pointer': '/data/attributes/lastName'}
111+
},
111112
]
112113
}
113114
response = self.client.post('/identities', {

‎example/tests/test_serializers.py‎

Lines changed: 7 additions & 2 deletions
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‎

Lines changed: 2 additions & 2 deletions
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‎

Lines changed: 6 additions & 12 deletions
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‎

Lines changed: 0 additions & 1 deletion
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)

AltStyle によって変換されたページ (->オリジナル) /