-
Notifications
You must be signed in to change notification settings - Fork 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #337
- Loading branch information
Showing
16 changed files
with
235 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,4 +40,7 @@ pip-delete-this-directory.txt | |
# VirtualEnv | ||
.venv/ | ||
|
||
# Developers | ||
*.sw* | ||
manage.py | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from django.utils import timezone | ||
from rest_framework.test import APITestCase | ||
|
||
from example.factories import CommentFactory | ||
from example.models import Author, Blog, Comment, Entry | ||
|
||
|
||
class PerformanceTestCase(APITestCase): | ||
def setUp(self): | ||
self.author = Author.objects.create(name='Super powerful superhero', email='[email protected]') | ||
self.blog = Blog.objects.create(name='Some Blog', tagline="It's a blog") | ||
self.other_blog = Blog.objects.create(name='Other blog', tagline="It's another blog") | ||
self.first_entry = Entry.objects.create( | ||
blog=self.blog, | ||
headline='headline one', | ||
body_text='body_text two', | ||
pub_date=timezone.now(), | ||
mod_date=timezone.now(), | ||
n_comments=0, | ||
n_pingbacks=0, | ||
rating=3 | ||
) | ||
self.second_entry = Entry.objects.create( | ||
blog=self.blog, | ||
headline='headline two', | ||
body_text='body_text one', | ||
pub_date=timezone.now(), | ||
mod_date=timezone.now(), | ||
n_comments=0, | ||
n_pingbacks=0, | ||
rating=1 | ||
) | ||
self.comment = Comment.objects.create(entry=self.first_entry) | ||
CommentFactory.create_batch(50) | ||
|
||
def test_query_count_no_includes(self): | ||
""" We expect a simple list view to issue only two queries. | ||
1. The number of results in the set (e.g. a COUNT query), | ||
only necessary because we're using PageNumberPagination | ||
2. The SELECT query for the set | ||
""" | ||
with self.assertNumQueries(2): | ||
response = self.client.get('/comments?page_size=25') | ||
self.assertEqual(len(response.data['results']), 25) | ||
|
||
def test_query_count_include_author(self): | ||
""" We expect a list view with an include have three queries: | ||
1. Primary resource COUNT query | ||
2. Primary resource SELECT | ||
3. Authors prefetched | ||
3. Entries prefetched | ||
""" | ||
with self.assertNumQueries(4): | ||
response = self.client.get('/comments?include=author&page_size=25') | ||
self.assertEqual(len(response.data['results']), 25) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from rest_framework.renderers import BrowsableAPIRenderer | ||
|
||
|
||
class BrowsableAPIRendererWithoutForms(BrowsableAPIRenderer): | ||
"""Renders the browsable api, but excludes the forms.""" | ||
|
||
def get_context(self, *args, **kwargs): | ||
ctx = super().get_context(*args, **kwargs) | ||
ctx['display_edit_forms'] = False | ||
return ctx | ||
|
||
def show_form_for_method(self, view, method, request, obj): | ||
"""We never want to do this! So just return False.""" | ||
return False | ||
|
||
def get_rendered_html_form(self, data, view, method, request): | ||
"""Why render _any_ forms at all. This method should return | ||
rendered HTML, so let's simply return an empty string. | ||
""" | ||
return "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,6 @@ recommonmark | |
Sphinx | ||
sphinx_rtd_theme | ||
tox | ||
mock | ||
django-debug-toolbar | ||
packaging==16.8 |
Oops, something went wrong.