Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 14 additions & 5 deletions example/tests/integration/test_includes.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
import pytest, json
import pytest
from django.core.urlresolvers import reverse

from example.tests.utils import load_json

pytestmark = pytest.mark.django_db


def test_included_data_on_list(multiple_entries, client):
multiple_entries[1].comments = []
response = client.get(reverse("entry-list") + '?include=comments')
response = client.get(reverse("entry-list") + '?include=comments&page_size=5')
included = load_json(response.content).get('included')

assert [x.get('type') for x in included] == ['comments']
assert len(load_json(response.content)['data']) == len(multiple_entries), 'Incorrect entry count'
assert [x.get('type') for x in included] == ['comments', 'comments'], 'List included types are incorrect'

comment_count = len([resource for resource in included if resource["type"] == "comments"])
expected_comment_count = sum([entry.comment_set.count() for entry in multiple_entries])
assert comment_count == expected_comment_count, 'List comment count is incorrect'


def test_included_data_on_detail(single_entry, client):
response = client.get(reverse("entry-detail", kwargs={'pk': single_entry.pk}) + '?include=comments')
included = load_json(response.content).get('included')

assert [x.get('type') for x in included] == ['comments']
assert [x.get('type') for x in included] == ['comments'], 'Detail included types are incorrect'

comment_count = len([resource for resource in included if resource["type"] == "comments"])
expected_comment_count = single_entry.comment_set.count()
assert comment_count == expected_comment_count, 'Detail comment count is incorrect'
2 changes: 2 additions & 0 deletions rest_framework_json_api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Utils.
"""
import copy

import inflection
from django.conf import settings
from django.utils import six, encoding
Expand Down Expand Up @@ -410,6 +411,7 @@ def extract_included(fields, resource, resource_instance, included_resources):
current_serializer = fields.serializer
context = current_serializer.context
included_serializers = get_included_serializers(current_serializer)
included_resources = copy.copy(included_resources)

for field_name, field in six.iteritems(fields):
# Skip URL field
Expand Down