-
Notifications
You must be signed in to change notification settings - Fork 347
Create --querycount parameter #412
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 all commits
767dc47
cd37548
060c9d5
d45c6b3
564c04b
fd9e6fb
e646ae9
ffc35ea
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 |
---|---|---|
@@ -0,0 +1,147 @@ | ||
class TestQueryCount(object): | ||
"""Test report generated by --querycount parameter""" | ||
|
||
def test_querycount_report_header(self, django_testdir): | ||
django_testdir.create_test_module(''' | ||
def test_zero_queries(): | ||
pass | ||
''') | ||
|
||
result = django_testdir.runpytest_subprocess('--querycount=5') | ||
result.stdout.fnmatch_lines([ | ||
'*== top 5 tests with most queries ==*' | ||
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. Test for "test_zero_queries" (not) in the output here!? 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. I intended just to test the headers here. Do you think I should add tests to the report lines here or the updated version of the other test case is enough? 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. The update is fine. |
||
]) | ||
|
||
def test_header_not_set_without_parameter(self, django_testdir): | ||
django_testdir.create_test_module(''' | ||
def test_zero_queries(): | ||
pass | ||
''') | ||
|
||
result = django_testdir.runpytest_subprocess() | ||
assert 'tests with most queries' not in result.stdout.str() | ||
|
||
def test_disabled_when_noquerycount_is_also_used(self, django_testdir): | ||
django_testdir.create_test_module(''' | ||
def test_zero_queries(): | ||
pass | ||
''') | ||
|
||
result = django_testdir.runpytest_subprocess( | ||
'--querycount=5 --noquerycount' | ||
) | ||
assert 'tests with most queries' not in result.stdout.str() | ||
|
||
def test_query_optimization_tips_for_the_current_version_of_django( | ||
self, | ||
django_testdir | ||
): | ||
django_testdir.create_test_module(''' | ||
def test_zero_queries(): | ||
pass | ||
''') | ||
|
||
result = django_testdir.runpytest_subprocess('--querycount=5') | ||
|
||
import django | ||
major, minor = django.VERSION[0:2] | ||
|
||
url = ( | ||
'https://docs.djangoproject.com' | ||
'/en/{major}.{minor}/topics/db/optimization/' | ||
).format( | ||
major=major, | ||
minor=minor | ||
) | ||
|
||
assert url in result.stdout.str() | ||
|
||
def test_querycount_report_lines(self, django_testdir): | ||
django_testdir.create_test_module(''' | ||
import pytest | ||
from django.db import connection | ||
|
||
@pytest.mark.django_db | ||
def test_one_query(): | ||
with connection.cursor() as cursor: | ||
cursor.execute('SELECT 1') | ||
|
||
assert True | ||
|
||
@pytest.mark.django_db | ||
def test_two_queries(): | ||
with connection.cursor() as cursor: | ||
cursor.execute('SELECT 1') | ||
cursor.execute('SELECT 1') | ||
|
||
assert True | ||
|
||
@pytest.mark.django_db | ||
def test_failed_one_query(): | ||
with connection.cursor() as cursor: | ||
cursor.execute('SELECT 1') | ||
|
||
assert False | ||
|
||
def test_zero_queries(): | ||
assert True | ||
''') | ||
|
||
result = django_testdir.runpytest_subprocess('--querycount=4') | ||
lines = result.stdout.get_lines_after( | ||
'*top 4 tests with most queries*' | ||
) | ||
assert 'test_two_queries' in lines[0] | ||
assert 'test_one_query' in lines[1] | ||
assert 'test_failed' in lines[2] | ||
assert 'test_zero_queries' in lines[3] | ||
|
||
def test_report_all_lines_on_querycount_zero(self, django_testdir): | ||
django_testdir.create_test_module(''' | ||
import pytest | ||
from django.db import connection | ||
|
||
@pytest.mark.django_db | ||
def test_one_query(): | ||
with connection.cursor() as cursor: | ||
cursor.execute('SELECT 1') | ||
|
||
assert True | ||
|
||
@pytest.mark.django_db | ||
def test_two_queries(): | ||
with connection.cursor() as cursor: | ||
cursor.execute('SELECT 1') | ||
cursor.execute('SELECT 1') | ||
|
||
assert True | ||
''') | ||
|
||
result = django_testdir.runpytest_subprocess('--querycount=0') | ||
lines = result.stdout.get_lines_after( | ||
'*top tests with most queries*' | ||
) | ||
assert 'test_two_queries' in lines[0] | ||
assert 'test_one_query' in lines[1] | ||
|
||
def test_should_report_fixture_queries(self, django_testdir): | ||
django_testdir.create_test_module(''' | ||
import pytest | ||
from django.db import connection | ||
|
||
@pytest.fixture | ||
def one_query(): | ||
with connection.cursor() as cursor: | ||
cursor.execute('SELECT 1') | ||
|
||
@pytest.mark.django_db | ||
def test_without_queries(one_query): | ||
pass | ||
''') | ||
|
||
result = django_testdir.runpytest_subprocess( | ||
'--setup-show', | ||
'--querycount=5' | ||
) | ||
|
||
assert '(# of queries executed: 1)' in result.stdout.str() |
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.
Please document
--no-querycount
(although both work).