Skip to content
Prev Previous commit
Next Next commit
Add tests for resource_name support
  • Loading branch information
czosel committed Dec 5, 2016
commit 40539017ec7161ef236af8eb341b0c46350b1788
48 changes: 45 additions & 3 deletions example/tests/integration/test_model_resource_name.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import pytest
from django.core.urlresolvers import reverse
from example import models, serializers, views
from example.tests.utils import dump_json, load_json
from rest_framework import status

from example.tests.utils import load_json
from django.core.urlresolvers import reverse

from example import models, serializers, views
pytestmark = pytest.mark.django_db


Expand Down Expand Up @@ -37,6 +38,24 @@ def _check_relationship_and_included_comment_type_are_the_same(django_client, ur
@pytest.mark.usefixtures("single_entry")
class TestModelResourceName:

create_data = {
'data': {
'type': 'resource_name_from_JSONAPIMeta',
'id': None,
'attributes': {
'body': 'example',
},
'relationships': {
'entry': {
'data': {
'type': 'resource_name_from_JSONAPIMeta',
'id': 1
}
}
}
}
}

def test_model_resource_name_on_list(self, client):
models.Comment.__bases__ += (_PatchedModel,)
response = client.get(reverse("comment-list"))
Expand Down Expand Up @@ -74,10 +93,33 @@ def test_resource_name_precendence(self, client):
assert (data.get('type') == 'resource_name_from_view'), (
'resource_name from view incorrect on list')

def test_model_resource_name_create(self, client):
models.Comment.__bases__ += (_PatchedModel,)
models.Entry.__bases__ += (_PatchedModel,)
response = client.post(reverse("comment-list"),
dump_json(self.create_data),
content_type='application/vnd.api+json')

assert response.status_code == status.HTTP_201_CREATED

def test_serializer_resource_name_create(self, client):
serializers.CommentSerializer.Meta.resource_name = "renamed_comments"
serializers.EntrySerializer.Meta.resource_name = "renamed_entries"
self.create_data['data']['type'] = 'renamed_comments'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should probably deepcopy self.create_data. Otherwise, I think you're modifying the class state for all tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed!

self.create_data['data']['relationships']['entry']['data']['type'] = 'renamed_entries'

response = client.post(reverse("comment-list"),
dump_json(self.create_data),
content_type='application/vnd.api+json')

assert response.status_code == status.HTTP_201_CREATED

def teardown_method(self, method):
models.Comment.__bases__ = (models.Comment.__bases__[0],)
models.Entry.__bases__ = (models.Entry.__bases__[0],)
try:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not super familiar with these tests. Would you mind explaining why this tear down code is not needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem!
Instead of setting the resource_name properties directly, i'm using monkeypatch.setattr, which will remember the previous state of the attribute and undo the change after the test ran. For more info on monkeypatch see this blog post or the docs.

delattr(serializers.CommentSerializer.Meta, "resource_name")
delattr(serializers.EntrySerializer.Meta, "resource_name")
except AttributeError:
pass
try:
Expand Down
7 changes: 2 additions & 5 deletions rest_framework_json_api/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,9 @@ def to_representation(self, value):

def get_resource_type_from_included_serializer(self, field_name):
"""
Given a field_name, check if the serializer has a
corresponding included_serializer with a Meta.resource_name property

Returns the resource name or None
Check to see it this resource has a different resource_name when
included and return that name, or None
"""
root = getattr(self.parent, 'parent', self.parent) or self.parent
root = self.get_root_serializer()

if root is not None:
Expand Down