-
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 1 commit
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 |
|---|---|---|
|
|
@@ -6,6 +6,10 @@ | |
|
|
||
| import pytest | ||
|
|
||
| from contextlib import contextmanager | ||
| from django.db import connection | ||
| from django.test.utils import CaptureQueriesContext | ||
|
||
|
|
||
| from . import live_server_helper | ||
|
|
||
| from .django_compat import is_django_unittest | ||
|
|
@@ -15,7 +19,7 @@ | |
| __all__ = ['django_db_setup', 'db', 'transactional_db', 'admin_user', | ||
| 'django_user_model', 'django_username_field', | ||
| 'client', 'admin_client', 'rf', 'settings', 'live_server', | ||
| '_live_server_helper'] | ||
| '_live_server_helper', 'assert_num_queries'] | ||
|
|
||
|
|
||
| @pytest.fixture(scope='session') | ||
|
|
@@ -323,3 +327,21 @@ def _live_server_helper(request): | |
| """ | ||
| if 'live_server' in request.funcargnames: | ||
| request.getfuncargvalue('transactional_db') | ||
|
|
||
|
|
||
| @pytest.fixture(scope='function') | ||
| def assert_num_queries(db, pytestconfig): | ||
|
||
|
|
||
| @contextmanager | ||
| def _assert_num_queries(num): | ||
| with CaptureQueriesContext(connection) as context: | ||
| yield | ||
| msg = "Expected to perform %s queries but %s was done" % (num, len(context)) | ||
|
||
| if pytestconfig.getoption('verbose') > 0: | ||
| sqls = (q['sql'] for q in context.captured_queries) | ||
| msg += '\n\nQueries:\n========\n\n%s' % '\n\n'.join(sqls) | ||
| else: | ||
| msg += " (add -v option to show queries)" | ||
| assert len(context) == num, msg | ||
|
||
|
|
||
| return _assert_num_queries | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,6 +49,22 @@ def test_rf(rf): | |
| assert isinstance(rf, RequestFactory) | ||
|
|
||
|
|
||
| @pytest.mark.django_db | ||
| def test_assert_num_queries(assert_num_queries, django_user_model): | ||
| with assert_num_queries(3): | ||
| Item.objects.create(name='foo') | ||
| Item.objects.create(name='bar') | ||
| Item.objects.create(name='baz') | ||
|
|
||
|
|
||
| @pytest.mark.django_db | ||
| def test_assert_num_queries_fails_properly(assert_num_queries, django_user_model): | ||
| with pytest.raises(AssertionError): | ||
| 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 |
||
|
|
||
|
|
||
| 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/