Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Allow json api renderer to be used as test request renderer class #396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 9, 2018
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
4 changes: 4 additions & 0 deletions example/settings/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,8 @@
'rest_framework.renderers.BrowsableAPIRenderer',
),
'DEFAULT_METADATA_CLASS': 'rest_framework_json_api.metadata.JSONAPIMetadata',
'TEST_REQUEST_RENDERER_CLASSES': (
'rest_framework_json_api.renderers.JSONRenderer',
),
'TEST_REQUEST_DEFAULT_FORMAT': 'vnd.api+json'
}
6 changes: 6 additions & 0 deletions example/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
from pytest_factoryboy import register
from rest_framework.test import APIClient

from example.factories import (
ArtProjectFactory,
Expand Down Expand Up @@ -56,3 +57,8 @@ def single_company(art_project_factory, research_project_factory, company_factor
@pytest.fixture
def single_art_project(art_project_factory):
return art_project_factory()


@pytest.fixture
def client():
return APIClient()
27 changes: 12 additions & 15 deletions example/tests/integration/test_includes.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import pytest
from django.core.urlresolvers import reverse

from example.tests.utils import load_json

pytestmark = pytest.mark.django_db


Expand All @@ -14,9 +12,9 @@ def test_default_included_data_on_list(multiple_entries, client):

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

assert len(load_json(response.content)['data']) == len(multiple_entries), (
assert len(response.json()['data']) == len(multiple_entries), (
'Incorrect entry count'
)
assert [x.get('type') for x in included] == ['comments', 'comments'], (
Expand All @@ -34,7 +32,7 @@ def test_default_included_data_on_detail(single_entry, client):

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

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

Expand All @@ -48,7 +46,7 @@ def test_dynamic_related_data_is_included(single_entry, entry_factory, client):
response = client.get(
reverse("entry-detail", kwargs={'pk': single_entry.pk}) + '?include=featured'
)
included = load_json(response.content).get('included')
included = response.json().get('included')

assert [x.get('type') for x in included] == ['entries'], 'Dynamic included types are incorrect'
assert len(included) == 1, 'The dynamically included blog entries are of an incorrect count'
Expand All @@ -59,7 +57,7 @@ def test_dynamic_many_related_data_is_included(single_entry, entry_factory, clie
response = client.get(
reverse("entry-detail", kwargs={'pk': single_entry.pk}) + '?include=suggested'
)
included = load_json(response.content).get('included')
included = response.json().get('included')

assert included
assert [x.get('type') for x in included] == ['entries'], 'Dynamic included types are incorrect'
Expand All @@ -69,12 +67,11 @@ def test_missing_field_not_included(author_bio_factory, author_factory, client):
# First author does not have a bio
author = author_factory(bio=None)
response = client.get(reverse('author-detail', args=[author.pk]) + '?include=bio')
data = load_json(response.content)
assert 'included' not in data
assert 'included' not in response.json()
# Second author does
author = author_factory()
response = client.get(reverse('author-detail', args=[author.pk]) + '?include=bio')
data = load_json(response.content)
data = response.json()
assert 'included' in data
assert len(data['included']) == 1
assert data['included'][0]['attributes']['body'] == author.bio.body
Expand All @@ -83,9 +80,9 @@ def test_missing_field_not_included(author_bio_factory, author_factory, client):
def test_deep_included_data_on_list(multiple_entries, client):
response = client.get(reverse("entry-list") + '?include=comments,comments.author,'
'comments.author.bio,comments.writer&page_size=5')
included = load_json(response.content).get('included')
included = response.json().get('included')

assert len(load_json(response.content)['data']) == len(multiple_entries), (
assert len(response.json()['data']) == len(multiple_entries), (
'Incorrect entry count'
)
assert [x.get('type') for x in included] == [
Expand Down Expand Up @@ -117,9 +114,9 @@ def test_deep_included_data_on_list(multiple_entries, client):
# Also include entry authors
response = client.get(reverse("entry-list") + '?include=authors,comments,comments.author,'
'comments.author.bio&page_size=5')
included = load_json(response.content).get('included')
included = response.json().get('included')

assert len(load_json(response.content)['data']) == len(multiple_entries), (
assert len(response.json()['data']) == len(multiple_entries), (
'Incorrect entry count'
)
assert [x.get('type') for x in included] == [
Expand All @@ -138,7 +135,7 @@ def test_deep_included_data_on_detail(single_entry, client):
# are returned along with the leaf nodes
response = client.get(reverse("entry-detail", kwargs={'pk': single_entry.pk}) +
'?include=comments,comments.author.bio')
included = load_json(response.content).get('included')
included = response.json().get('included')

assert [x.get('type') for x in included] == ['authorBios', 'authors', 'comments'], \
'Detail included types are incorrect'
Expand Down
8 changes: 2 additions & 6 deletions example/tests/integration/test_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import pytest
from django.core.urlresolvers import reverse

from example.tests.utils import load_json

pytestmark = pytest.mark.django_db


Expand Down Expand Up @@ -42,9 +40,8 @@ def test_top_level_meta_for_list_view(blog, client):
}

response = client.get(reverse("blog-list"))
parsed_content = load_json(response.content)

assert expected == parsed_content
assert expected == response.json()


def test_top_level_meta_for_detail_view(blog, client):
Expand Down Expand Up @@ -74,6 +71,5 @@ def test_top_level_meta_for_detail_view(blog, client):
}

response = client.get(reverse("blog-detail", kwargs={'pk': blog.pk}))
parsed_content = load_json(response.content)

assert expected == parsed_content
assert expected == response.json()
27 changes: 11 additions & 16 deletions example/tests/integration/test_model_resource_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from rest_framework import status

from example import models, serializers, views
from example.tests.utils import dump_json, load_json

pytestmark = pytest.mark.django_db

Expand All @@ -19,8 +18,8 @@ def _check_resource_and_relationship_comment_type_match(django_client):
entry_response = django_client.get(reverse("entry-list"))
comment_response = django_client.get(reverse("comment-list"))

comment_resource_type = load_json(comment_response.content).get('data')[0].get('type')
comment_relationship_type = load_json(entry_response.content).get(
comment_resource_type = comment_response.json().get('data')[0].get('type')
comment_relationship_type = entry_response.json().get(
'data')[0].get('relationships').get('comments').get('data')[0].get('type')

assert comment_resource_type == comment_relationship_type, (
Expand All @@ -30,8 +29,8 @@ def _check_resource_and_relationship_comment_type_match(django_client):

def _check_relationship_and_included_comment_type_are_the_same(django_client, url):
response = django_client.get(url + "?include=comments")
data = load_json(response.content).get('data')[0]
comment = load_json(response.content).get('included')[0]
data = response.json().get('data')[0]
comment = response.json().get('included')[0]

comment_relationship_type = data.get('relationships').get('comments').get('data')[0].get('type')
comment_included_type = comment.get('type')
Expand Down Expand Up @@ -65,7 +64,7 @@ class TestModelResourceName:
def test_model_resource_name_on_list(self, client):
models.Comment.__bases__ += (_PatchedModel,)
response = client.get(reverse("comment-list"))
data = load_json(response.content)['data'][0]
data = response.json()['data'][0]
# name should be super-author instead of model name RenamedAuthor
assert (data.get('type') == 'resource_name_from_JSONAPIMeta'), (
'resource_name from model incorrect on list')
Expand All @@ -74,14 +73,14 @@ def test_model_resource_name_on_list(self, client):
def test_resource_name_precendence(self, client, monkeypatch):
# default
response = client.get(reverse("comment-list"))
data = load_json(response.content)['data'][0]
data = response.json()['data'][0]
assert (data.get('type') == 'comments'), (
'resource_name from model incorrect on list')

# model > default
models.Comment.__bases__ += (_PatchedModel,)
response = client.get(reverse("comment-list"))
data = load_json(response.content)['data'][0]
data = response.json()['data'][0]
assert (data.get('type') == 'resource_name_from_JSONAPIMeta'), (
'resource_name from model incorrect on list')

Expand All @@ -93,23 +92,21 @@ def test_resource_name_precendence(self, client, monkeypatch):
False
)
response = client.get(reverse("comment-list"))
data = load_json(response.content)['data'][0]
data = response.json()['data'][0]
assert (data.get('type') == 'resource_name_from_serializer'), (
'resource_name from serializer incorrect on list')

# view > serializer > model
monkeypatch.setattr(views.CommentViewSet, 'resource_name', 'resource_name_from_view', False)
response = client.get(reverse("comment-list"))
data = load_json(response.content)['data'][0]
data = response.json()['data'][0]
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')
response = client.post(reverse("comment-list"), self.create_data)

assert response.status_code == status.HTTP_201_CREATED

Expand All @@ -130,9 +127,7 @@ def test_serializer_resource_name_create(self, client, monkeypatch):
create_data['data']['type'] = 'renamed_comments'
create_data['data']['relationships']['entry']['data']['type'] = 'renamed_entries'

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

assert response.status_code == status.HTTP_201_CREATED

Expand Down
24 changes: 3 additions & 21 deletions example/tests/integration/test_non_paginated_responses.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import pytest
from django.core.urlresolvers import reverse

from rest_framework_json_api.pagination import PageNumberPagination

from example.tests.utils import load_json
from example.views import EntryViewSet

try:
from unittest import mock
except ImportError:
Expand All @@ -14,12 +9,11 @@
pytestmark = pytest.mark.django_db


# rf == request_factory
@mock.patch(
'rest_framework_json_api.utils'
'.get_default_included_resources_from_serializer',
new=lambda s: [])
def test_multiple_entries_no_pagination(multiple_entries, rf):
def test_multiple_entries_no_pagination(multiple_entries, client):

expected = {
"data": [
Expand Down Expand Up @@ -102,18 +96,6 @@ def test_multiple_entries_no_pagination(multiple_entries, rf):
]
}

class NoPagination(PageNumberPagination):
page_size = None

class NonPaginatedEntryViewSet(EntryViewSet):
pagination_class = NoPagination

request = rf.get(
reverse("entry-list"))
view = NonPaginatedEntryViewSet.as_view({'get': 'list'})
response = view(request)
response.render()

parsed_content = load_json(response.content)
response = client.get(reverse("nopage-entry-list"))

assert expected == parsed_content
assert expected == response.json()
5 changes: 1 addition & 4 deletions example/tests/integration/test_pagination.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import pytest
from django.core.urlresolvers import reverse

from example.tests.utils import load_json

try:
from unittest import mock
except ImportError:
Expand Down Expand Up @@ -81,6 +79,5 @@ def test_pagination_with_single_entry(single_entry, client):
}

response = client.get(reverse("entry-list"))
parsed_content = load_json(response.content)

assert expected == parsed_content
assert expected == response.json()
Loading