Skip to content

Commit

Permalink
Change default behavior of tests to use nested serializers (#816)
Browse files Browse the repository at this point in the history
Co-authored-by: Alan Crosswell <[email protected]>
  • Loading branch information
sliverc and n2ygk authored Aug 25, 2020
1 parent 8dbe008 commit 959aec7
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 39 deletions.
6 changes: 3 additions & 3 deletions example/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ class Meta:

class BlogSerializer(serializers.ModelSerializer):
copyright = serializers.SerializerMethodField()
tags = TaggedItemSerializer(many=True, read_only=True)
tags = relations.ResourceRelatedField(many=True, read_only=True)

include_serializers = {
included_serializers = {
'tags': 'example.serializers.TaggedItemSerializer',
}

Expand Down Expand Up @@ -147,7 +147,7 @@ def __init__(self, *args, **kwargs):
model=Entry,
read_only=True
)
tags = TaggedItemSerializer(many=True, read_only=True)
tags = relations.ResourceRelatedField(many=True, read_only=True)

def get_suggested(self, obj):
return Entry.objects.exclude(pk=obj.pk)
Expand Down
1 change: 1 addition & 0 deletions example/settings/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@

JSON_API_FORMAT_FIELD_NAMES = 'camelize'
JSON_API_FORMAT_TYPES = 'camelize'
JSON_API_SERIALIZE_NESTED_SERIALIZERS_AS_ATTRIBUTE = True
REST_FRAMEWORK = {
'PAGE_SIZE': 5,
'EXCEPTION_HANDLER': 'rest_framework_json_api.exceptions.exception_handler',
Expand Down
12 changes: 2 additions & 10 deletions example/tests/integration/test_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ def test_top_level_meta_for_list_view(blog, client):
"links": {
"self": 'http://testserver/blogs/1'
},
"relationships": {
"tags": {
"data": []
}
},
'relationships': {'tags': {'data': [], 'meta': {'count': 0}}},
"meta": {
"copyright": datetime.now().year
},
Expand Down Expand Up @@ -53,11 +49,7 @@ def test_top_level_meta_for_detail_view(blog, client):
"attributes": {
"name": blog.name
},
"relationships": {
"tags": {
"data": []
}
},
'relationships': {'tags': {'data': [], 'meta': {'count': 0}}},
"links": {
"self": "http://testserver/blogs/1"
},
Expand Down
8 changes: 2 additions & 6 deletions example/tests/integration/test_non_paginated_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ def test_multiple_entries_no_pagination(multiple_entries, client):
"self": "http://testserver/entries/1/relationships/featured_hyperlinked"
}
},
"tags": {
"data": []
}
'tags': {'data': [], 'meta': {'count': 0}},
}
},
{
Expand Down Expand Up @@ -135,9 +133,7 @@ def test_multiple_entries_no_pagination(multiple_entries, client):
"self": "http://testserver/entries/2/relationships/featured_hyperlinked"
}
},
"tags": {
"data": []
}
'tags': {'data': [], 'meta': {'count': 0}},
}
},
]
Expand Down
1 change: 1 addition & 0 deletions example/tests/integration/test_pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def test_pagination_with_single_entry(single_entry, client):
}
},
"tags": {
'meta': {'count': 1},
"data": [
{
"id": "1",
Expand Down
4 changes: 1 addition & 3 deletions example/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,7 @@ def test_search_keywords(self):
'self': 'http://testserver/entries/7/relationships/suggested_hyperlinked', # noqa: E501
'related': 'http://testserver/entries/7/suggested/'}
},
'tags': {
'data': []
},
'tags': {'data': [], 'meta': {'count': 0}},
'featuredHyperlinked': {
'links': {
'self': 'http://testserver/entries/7/relationships/featured_hyperlinked', # noqa: E501
Expand Down
9 changes: 7 additions & 2 deletions example/tests/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def setUp(self):
)

def test_forward_relationship_not_loaded_when_not_included(self):
to_representation_method = 'example.serializers.BlogSerializer.to_representation'
to_representation_method = 'example.serializers.TaggedItemSerializer.to_representation'
with mock.patch(to_representation_method) as mocked_serializer:
class EntrySerializer(ModelSerializer):
blog = BlogSerializer()
Expand Down Expand Up @@ -79,7 +79,12 @@ class Meta:
expected = dict(
[
('id', 1),
('blog', dict([('type', 'blogs'), ('id', 1)])),
('blog', dict([
('name', 'Some Blog'),
('tags', []),
('copyright', 2020),
('url', 'http://testserver/blogs/1')
])),
('headline', 'headline'),
('body_text', 'body_text'),
('pub_date', DateField().to_representation(self.entry.pub_date)),
Expand Down
4 changes: 2 additions & 2 deletions example/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ def test_get_object_gives_correct_blog(self):
'id': '{}'.format(self.blog.id),
'links': {'self': 'http://testserver/blogs/{}'.format(self.blog.id)},
'meta': {'copyright': datetime.now().year},
'relationships': {'tags': {'data': []}},
'relationships': {'tags': {'data': [], 'meta': {'count': 0}}},
'type': 'blogs'
},
'meta': {'apiDocs': '/docs/api/blogs'}
Expand Down Expand Up @@ -632,7 +632,7 @@ def test_get_object_gives_correct_entry(self):
'/suggested_hyperlinked'.format(self.second_entry.id)
}
},
'tags': {'data': []}},
'tags': {'data': [], 'meta': {'count': 0}}},
'type': 'posts'
}
}
Expand Down
18 changes: 6 additions & 12 deletions example/tests/unit/test_default_drf_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,10 @@ def test_blog_create(client):

expected = {
'data': {
'attributes': {'name': blog.name},
'attributes': {'name': blog.name, 'tags': []},
'id': '{}'.format(blog.id),
'links': {'self': 'http://testserver/blogs/{}'.format(blog.id)},
'meta': {'copyright': datetime.now().year},
'relationships': {'tags': {'data': []}},
'type': 'blogs'
},
'meta': {'apiDocs': '/docs/api/blogs'}
Expand All @@ -116,11 +115,10 @@ def test_get_object_gives_correct_blog(client, blog, entry):
resp = client.get(url)
expected = {
'data': {
'attributes': {'name': blog.name},
'attributes': {'name': blog.name, 'tags': []},
'id': '{}'.format(blog.id),
'links': {'self': 'http://testserver/blogs/{}'.format(blog.id)},
'meta': {'copyright': datetime.now().year},
'relationships': {'tags': {'data': []}},
'type': 'blogs'
},
'meta': {'apiDocs': '/docs/api/blogs'}
Expand Down Expand Up @@ -154,11 +152,10 @@ def test_get_object_patches_correct_blog(client, blog, entry):

expected = {
'data': {
'attributes': {'name': new_name},
'attributes': {'name': new_name, 'tags': []},
'id': '{}'.format(blog.id),
'links': {'self': 'http://testserver/blogs/{}'.format(blog.id)},
'meta': {'copyright': datetime.now().year},
'relationships': {'tags': {'data': []}},
'type': 'blogs'
},
'meta': {'apiDocs': '/docs/api/blogs'}
Expand Down Expand Up @@ -189,17 +186,14 @@ def test_get_entry_list_with_blogs(client, entry):
'first': 'http://testserver/drf-entries/1/suggested/?page%5Bnumber%5D=1',
'last': 'http://testserver/drf-entries/1/suggested/?page%5Bnumber%5D=1',
'next': None,
'prev': None
'prev': None,
},
'data': [
{
'type': 'entries',
'id': '1',
'attributes': {},
'relationships': {
'tags': {
'data': []
}
'attributes': {
'tags': [],
},
'links': {
'self': 'http://testserver/drf-blogs/1'
Expand Down
1 change: 0 additions & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ DJANGO_SETTINGS_MODULE=example.settings.test
filterwarnings =
error::DeprecationWarning
error::PendingDeprecationWarning
ignore::DeprecationWarning:rest_framework_json_api.serializers

0 comments on commit 959aec7

Please sign in to comment.