-
Notifications
You must be signed in to change notification settings - Fork 347
Added assert_num_queries fixture #387
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
blueyed
merged 14 commits into
pytest-dev:master
from
lukaszb:feature/assert_num_queries
Feb 7, 2017
Merged
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
76548c8
Added assert_num_queries fixture
lukaszb 4f2875e
move imports
lukaszb f87689d
typo
lukaszb 4a23b16
use pytest.fail instead of assert
lukaszb caa952c
test against transactional_db
lukaszb cf8e6fd
removed unused fixture from tests
lukaszb 61b8d84
test output
lukaszb ab0bbca
fix transactional test
lukaszb 03dd0a7
post review fixes
lukaszb d41f2dd
review fixes
lukaszb 0e7ba73
Merge branch 'feature/assert_num_queries' of github.com:lukaszb/pytes…
lukaszb 5a90300
Merge branch 'master' into feature/assert_num_queries
lukaszb 4742c86
Merge branch 'master' into feature/assert_num_queries
lukaszb 5d36c0b
Updated documentation
lukaszb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -8,7 +8,7 @@ | |
|
||
import pytest | ||
|
||
from django.db import connection | ||
from django.db import connection, transaction | ||
from django.conf import settings as real_settings | ||
from django.test.client import Client, RequestFactory | ||
from django.test.testcases import connections_support_transactions | ||
|
@@ -49,6 +49,68 @@ def test_rf(rf): | |
assert isinstance(rf, RequestFactory) | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_django_assert_num_queries_db(django_assert_num_queries): | ||
with django_assert_num_queries(3): | ||
Item.objects.create(name='foo') | ||
Item.objects.create(name='bar') | ||
Item.objects.create(name='baz') | ||
|
||
with pytest.raises(pytest.fail.Exception): | ||
with django_assert_num_queries(2): | ||
Item.objects.create(name='quux') | ||
|
||
|
||
@pytest.mark.django_db(transaction=True) | ||
def test_django_assert_num_queries_transactional_db(transactional_db, django_assert_num_queries): | ||
with transaction.atomic(): | ||
|
||
with django_assert_num_queries(3): | ||
Item.objects.create(name='foo') | ||
Item.objects.create(name='bar') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please test for the expected output here, using |
||
Item.objects.create(name='baz') | ||
|
||
with pytest.raises(pytest.fail.Exception): | ||
with django_assert_num_queries(2): | ||
Item.objects.create(name='quux') | ||
|
||
|
||
def test_django_assert_num_queries_output(django_testdir): | ||
django_testdir.create_test_module(""" | ||
from django.contrib.contenttypes.models import ContentType | ||
import pytest | ||
|
||
@pytest.mark.django_db | ||
def test_queries(django_assert_num_queries): | ||
with django_assert_num_queries(1): | ||
list(ContentType.objects.all()) | ||
ContentType.objects.count() | ||
""") | ||
result = django_testdir.runpytest_subprocess('--tb=short') | ||
result.stdout.fnmatch_lines(['*Expected to perform 1 queries but 2 were done*']) | ||
assert result.ret == 1 | ||
|
||
|
||
def test_django_assert_num_queries_output_verbose(django_testdir): | ||
django_testdir.create_test_module(""" | ||
from django.contrib.contenttypes.models import ContentType | ||
import pytest | ||
|
||
@pytest.mark.django_db | ||
def test_queries(django_assert_num_queries): | ||
with django_assert_num_queries(11): | ||
list(ContentType.objects.all()) | ||
ContentType.objects.count() | ||
""") | ||
result = django_testdir.runpytest_subprocess('--tb=short', '-v') | ||
result.stdout.fnmatch_lines([ | ||
'*Expected to perform 11 queries but 2 were done*', | ||
'*Queries:*', | ||
'*========*', | ||
]) | ||
assert result.ret == 1 | ||
|
||
|
||
class TestSettings: | ||
"""Tests for the settings fixture, order matters""" | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/assert_num_queries/django_assert_num_queries/