-
Notifications
You must be signed in to change notification settings - Fork 346
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
Added assert_num_queries fixture #387
Conversation
Not sure why some tests failed. It seems unrelated to my change but I might be wrong - help with investigating appreciated. |
All tests (except for QA) appear to fail. |
pytest_django/fixtures.py
Outdated
@@ -6,6 +6,10 @@ | |||
|
|||
import pytest | |||
|
|||
from contextlib import contextmanager | |||
from django.db import connection | |||
from django.test.utils import CaptureQueriesContext |
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.
These imports might cause the test failures.
Try importing it in the new fixture.
@blueyed thanks for comments, I've applied post review fixes, please check |
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.
Still looks good to me in general.
I think the fixture should be named django_assert_db_queries
(especially for the prefix) though.
Then it could later be enhanced to accept an optional arg (list) to match the actual SQL - probably using pytest's fnmatch functionality maybe.
We're using something similar already and it would be nice to have it upstream in pytest-django, and it's always annoying to figure out what the diff of the actual queries is.
Made me remember https://github.com/yourlabs/django-dbdiff, which has a mode where it generates the expected data that you then can copy and paste into your test initially. But this is all a bit out of scope for this PR probably?! :)
docs/helpers.rst
Outdated
``assert_num_queries`` | ||
~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
This fixture allows to check that expected number of queries are performed. |
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/
docs/helpers.rst
Outdated
~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
This fixture allows to check that expected number of queries are performed. | ||
Note that currently it only supports default database. |
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.
better:
Note that it currently only supports the default database.
@pelme |
@blueyed thanks for review, I've applied changes. As for comparing queries - I believe it's out of scope of this PR. |
Sure. I was just throwing around ideas, hoping for feedback.. :) |
I've looked at this last week IIRC, e.g. to support not just the default database etc.. I just wanted to leave a remark that the following should be included probably (optionally?!) to not include ContentType queries: # Clear ContentType cache, to make this test predictable (when run alone).
ContentType.objects.clear_cache() What about a flag/kwarg for this, defaulting to that behavior? |
@blueyed I wanted this to be same as what Django provides. Looking their |
Here is a local patch from reviewing this a while ago, please consider applying it (no need to credit me).
|
…t-django into feature/assert_num_queries
@blueyed applied (apart from line length changes as at |
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.
Needs some small update and a rebase.
docs/helpers.rst
Outdated
:: | ||
|
||
def test_queries(assert_num_queries): | ||
with assert_num_queries(3): |
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/
@pelme |
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.
Looks good to me! It is certainly a very useful feature that I can see myself using!
Cool. |
@blueyed aligned with master and updated docs. Do you need me to rebase or you'd simply "squash and merge"? |
@lukaszb |
@blueyed cool, no problem btw, I know how it works ;-) |
Hello! Is there a release planned that would include this feature? |
Have you considered whether this should really be a fixture? It's static; there's no setup or teardown. The |
@coady |
Hello again. Can someone say when this will be part of a release? |
It provides a fixture with functionality that is given by Django's TestCase class (
assertNumQueries
). It currently supports only default connection but can be easily extended in future.