Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
+ tests for relations strategy
  • Loading branch information
Boris Pleshakov committed Mar 26, 2020
commit db139e05a999d3cc1fa5f0e00771875d776b2e9c
8 changes: 8 additions & 0 deletions example/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ def get_first_entry(self, obj):
class CommentWithNestedFieldsSerializer(serializers.ModelSerializer):
entry = EntryDRFSerializers()

included_serializers = {
'entry': 'example.serializers.EntryDRFSerializers'
}

class Meta:
model = Comment
exclude = ('created_at', 'modified_at', 'author')
Expand All @@ -270,6 +274,10 @@ class Meta:
class AuthorWithNestedFieldsSerializer(serializers.ModelSerializer):
comments = CommentWithNestedFieldsSerializer(many=True)

included_serializers = {
'comments': 'example.serializers.CommentWithNestedFieldsSerializer'
}

class Meta:
model = Author
fields = ('name', 'email', 'comments')
Expand Down
117 changes: 115 additions & 2 deletions example/tests/test_rendering_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from django.utils import timezone
from rest_framework.reverse import reverse

from rest_framework_json_api.settings import JSONAPISettings
from rest_framework_json_api.settings import JSONAPISettings, \
ATTRIBUTE_RENDERING_STRATEGY, RELATIONS_RENDERING_STRATEGY
from . import TestBase
from example.models import Author, Blog, Comment, Entry
from django.test import override_settings
Expand Down Expand Up @@ -39,7 +40,7 @@ def setUp(self):
)

def test_attribute_rendering_strategy(self):
with override_settings(JSON_API_NESTED_SERIALIZERS_RENDERING_STRATEGY='ATTRIBUTE'):
with override_settings(JSON_API_NESTED_SERIALIZERS_RENDERING_STRATEGY=ATTRIBUTE_RENDERING_STRATEGY):
response = self.client.get(self.list_url)

expected = {
Expand Down Expand Up @@ -79,6 +80,118 @@ def test_attribute_rendering_strategy(self):
}
assert expected == response.json()

def test_relations_rendering_strategy(self):
with override_settings(JSON_API_NESTED_SERIALIZERS_RENDERING_STRATEGY=RELATIONS_RENDERING_STRATEGY):
response = self.client.get(self.list_url)

expected = {
"links": {
"first": "http://testserver/authors-nested?page%5Bnumber%5D=1",
"last": "http://testserver/authors-nested?page%5Bnumber%5D=5",
"next": "http://testserver/authors-nested?page%5Bnumber%5D=2",
"prev": None
},
"data": [
{
"type": "authors",
"id": "1",
"attributes": {
"name": "some_author1",
"email": "some_author1@example.org"
},
"relationships": {
"comments": {
"data": [
{
"type": "comments",
"id": "1"
}
]
}
}
}
],
"meta": {
"pagination": {
"page": 1,
"pages": 5,
"count": 5
}
}
}
assert expected == response.json()

def test_relations_rendering_strategy_included(self):
with override_settings(JSON_API_NESTED_SERIALIZERS_RENDERING_STRATEGY=RELATIONS_RENDERING_STRATEGY):
response = self.client.get(self.list_url, data={'include': 'comments,comments.entry'})

expected = {
"links": {
"first": "http://testserver/authors-nested?include=comments%2Ccomments.entry&page%5Bnumber%5D=1",
"last": "http://testserver/authors-nested?include=comments%2Ccomments.entry&page%5Bnumber%5D=5",
"next": "http://testserver/authors-nested?include=comments%2Ccomments.entry&page%5Bnumber%5D=2",
"prev": None
},
"data": [
{
"type": "authors",
"id": "1",
"attributes": {
"name": "some_author1",
"email": "some_author1@example.org"
},
"relationships": {
"comments": {
"data": [
{
"type": "comments",
"id": "1"
}
]
}
}
}
],
"included": [
{
"type": "comments",
"id": "1",
"attributes": {
"body": "testing one two three"
},
"relationships": {
"entry": {
"data": {
"type": "entries",
"id": "1"
}
}
}
},
{
"type": "entries",
"id": "1",
"attributes": {},
"relationships": {
"tags": {
"data": []
}
},
"links": {
"self": "http://testserver/drf-blogs/1"
}
}
],
"meta": {
"pagination": {
"page": 1,
"pages": 5,
"count": 5
}
}
}
assert expected == response.json()


class TestRenderingStrategySettings(TestBase):

Expand Down