From 1f653056ad89564d69896af919492a9fb6c6d33e Mon Sep 17 00:00:00 2001 From: Jonathan Senecal Date: Fri, 16 Oct 2015 11:56:17 -0400 Subject: [PATCH 1/6] Updated the tests to check for issue #131 --- example/tests/integration/test_includes.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/example/tests/integration/test_includes.py b/example/tests/integration/test_includes.py index 3284c79b..900000c3 100644 --- a/example/tests/integration/test_includes.py +++ b/example/tests/integration/test_includes.py @@ -1,20 +1,25 @@ -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') included = load_json(response.content).get('included') + assert len(load_json(response.content)['data']) == len(multiple_entries) assert [x.get('type') for x in included] == ['comments'] + assert (len([resource for resource in included if resource["type"] == "comments"]) == + sum([entry.comment_set.count() for entry in multiple_entries])) + 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 (len([resource for resource in included if resource["type"] == "comments"]) == + single_entry.comment_set.count()) From 3a58ed967ab815ef4ba1434d0f424c7136e3f1f4 Mon Sep 17 00:00:00 2001 From: Jerel Unruh Date: Fri, 16 Oct 2015 11:38:56 -0500 Subject: [PATCH 2/6] Increased pagination for list test and added assert comments --- example/tests/integration/test_includes.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/example/tests/integration/test_includes.py b/example/tests/integration/test_includes.py index 900000c3..457d22ae 100644 --- a/example/tests/integration/test_includes.py +++ b/example/tests/integration/test_includes.py @@ -7,19 +7,21 @@ def test_included_data_on_list(multiple_entries, client): - 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 len(load_json(response.content)['data']) == len(multiple_entries) - assert [x.get('type') for x in included] == ['comments'] - assert (len([resource for resource in included if resource["type"] == "comments"]) == - sum([entry.comment_set.count() for entry in multiple_entries])) + assert len(load_json(response.content)['data']) == len(multiple_entries), 'Correct entry count' + assert [x.get('type') for x in included] == ['comments'], 'List included types are correct' + + 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 correct' 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 (len([resource for resource in included if resource["type"] == "comments"]) == - single_entry.comment_set.count()) + assert [x.get('type') for x in included] == ['comments'], 'Detail included types are correct' + comment_count = len([resource for resource in included if resource["type"] == "comments"]) + assert comment_count == single_entry.comment_set.count(), 'Detail comment count is correct' From 603dc3b4eff77c07f5d3df8e4f416ba6e27e52d2 Mon Sep 17 00:00:00 2001 From: Jonathan Senecal Date: Fri, 16 Oct 2015 12:49:55 -0400 Subject: [PATCH 3/6] Oops - Backwards error messages fix --- example/tests/integration/test_includes.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/example/tests/integration/test_includes.py b/example/tests/integration/test_includes.py index 457d22ae..bf5e1fa3 100644 --- a/example/tests/integration/test_includes.py +++ b/example/tests/integration/test_includes.py @@ -10,18 +10,18 @@ def test_included_data_on_list(multiple_entries, client): response = client.get(reverse("entry-list") + '?include=comments&page_size=5') included = load_json(response.content).get('included') - assert len(load_json(response.content)['data']) == len(multiple_entries), 'Correct entry count' - assert [x.get('type') for x in included] == ['comments'], 'List included types are correct' + assert len(load_json(response.content)['data']) == len(multiple_entries), 'Incorrect entry count' + assert [x.get('type') for x in included] == ['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 correct' + 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'], 'Detail included types are correct' + 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"]) - assert comment_count == single_entry.comment_set.count(), 'Detail comment count is correct' + assert comment_count == single_entry.comment_set.count(), 'Detail comment count is incorrect' From 0e87688ba96f784f6f7ce54f080cbecc1e5e7b94 Mon Sep 17 00:00:00 2001 From: Jonathan Senecal Date: Fri, 16 Oct 2015 13:01:16 -0400 Subject: [PATCH 4/6] Copy included_resources before removing items from it Fixes #131 --- rest_framework_json_api/utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rest_framework_json_api/utils.py b/rest_framework_json_api/utils.py index 5418141d..bc09e368 100644 --- a/rest_framework_json_api/utils.py +++ b/rest_framework_json_api/utils.py @@ -2,6 +2,7 @@ Utils. """ import copy + import inflection from django.conf import settings from django.utils import six, encoding @@ -421,7 +422,7 @@ def extract_included(fields, resource, resource_instance, included_resources): continue try: - included_resources.remove(field_name) + included_resources.copy().remove(field_name) except ValueError: # Skip fields not in requested included resources continue From 1a8f67ec1eaa97aebe25d7d6625a237f8e1ce151 Mon Sep 17 00:00:00 2001 From: Jonathan Senecal Date: Fri, 16 Oct 2015 13:02:44 -0400 Subject: [PATCH 5/6] Fix for test included_data_on_list included types check --- example/tests/integration/test_includes.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/example/tests/integration/test_includes.py b/example/tests/integration/test_includes.py index bf5e1fa3..d7f6f6d1 100644 --- a/example/tests/integration/test_includes.py +++ b/example/tests/integration/test_includes.py @@ -11,7 +11,7 @@ def test_included_data_on_list(multiple_entries, client): included = load_json(response.content).get('included') assert len(load_json(response.content)['data']) == len(multiple_entries), 'Incorrect entry count' - assert [x.get('type') for x in included] == ['comments'], 'List included types are incorrect' + 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]) @@ -23,5 +23,7 @@ def test_included_data_on_detail(single_entry, client): included = load_json(response.content).get('included') 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"]) - assert comment_count == single_entry.comment_set.count(), 'Detail comment count is incorrect' + expected_comment_count = single_entry.comment_set.count() + assert comment_count == expected_comment_count, 'Detail comment count is incorrect' From 33e54bb75dc09bb7eadf04c0df11dd6ae45122ea Mon Sep 17 00:00:00 2001 From: Jonathan Senecal Date: Fri, 16 Oct 2015 13:11:23 -0400 Subject: [PATCH 6/6] Fix for AttributeError: 'list' object has no attribute 'copy' in python27 --- rest_framework_json_api/utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rest_framework_json_api/utils.py b/rest_framework_json_api/utils.py index bc09e368..147ef177 100644 --- a/rest_framework_json_api/utils.py +++ b/rest_framework_json_api/utils.py @@ -411,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 @@ -422,7 +423,7 @@ def extract_included(fields, resource, resource_instance, included_resources): continue try: - included_resources.copy().remove(field_name) + included_resources.remove(field_name) except ValueError: # Skip fields not in requested included resources continue