-
Notifications
You must be signed in to change notification settings - Fork 352
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
Changes from 8 commits
76548c8
4f2875e
f87689d
4a23b16
caa952c
cf8e6fd
61b8d84
ab0bbca
03dd0a7
d41f2dd
0e7ba73
5a90300
4742c86
5d36c0b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -217,3 +217,21 @@ Example | |
| def test_with_specific_settings(settings): | ||
| settings.USE_TZ = True | ||
| assert settings.USE_TZ | ||
|
|
||
| ``assert_num_queries`` | ||
| ~~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| This fixture allows to check that expected number of queries are performed. | ||
| Note that currently it only supports default database. | ||
|
||
|
|
||
|
|
||
| Example | ||
| """"""" | ||
|
|
||
| :: | ||
|
|
||
| def test_queries(assert_num_queries): | ||
| with assert_num_queries(3): | ||
|
||
| Item.objects.create('foo') | ||
| Item.objects.create('bar') | ||
| Item.objects.create('baz') | ||
| 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_assert_num_queries_db(assert_num_queries): | ||
| with 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 assert_num_queries(2): | ||
| Item.objects.create(name='quux') | ||
|
|
||
|
|
||
| @pytest.mark.django_db(transaction=True) | ||
| def test_assert_num_queries_transactional_db(transactional_db, assert_num_queries): | ||
| with transaction.atomic(): | ||
|
|
||
| with assert_num_queries(3): | ||
| Item.objects.create(name='foo') | ||
| Item.objects.create(name='bar') | ||
|
Contributor
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 assert_num_queries(2): | ||
| Item.objects.create(name='quux') | ||
|
|
||
|
|
||
| def test_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(assert_num_queries): | ||
| with 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_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(assert_num_queries): | ||
| with 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""" | ||
|
|
||
|
|
||
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/of queries/of DB queries/