-
Notifications
You must be signed in to change notification settings - Fork 347
Add parameter info to fixture assert_num_queries to display additiona… #663
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
Conversation
Codecov Report
@@ Coverage Diff @@
## master #663 +/- ##
=========================================
+ Coverage 94.38% 94.4% +0.02%
=========================================
Files 33 33
Lines 1834 1841 +7
Branches 156 157 +1
=========================================
+ Hits 1731 1738 +7
Misses 78 78
Partials 25 25
Continue to review full report at Codecov.
|
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.
Thank you for your contribution.
I am not sure if this is really needed, given that you could also just add a comment above / next to the assertion?!
@blueyed Yes, you could. But that way the comment won't be part of the error message supplied by giving |
I know that it's possible to customize the error message like this:
But this doesn't seem to be possible, if you do the assertion via a context manager? |
I've meant that you would see it from the traceback already if it was a comment.
Would be nice if this pattern could be used, but it would also miss the original info then, i.e. the number of queries. |
Yes, you are right, that it is visible in the traceback. But not visible enough for me. I like the spot on top of the error message above the queries. And I don't think, it is very complicated. We use this parameter in our edited version of assert_num_queries in our projects and it can be a big help for developers running in this errors, who are new to the project. |
Ok. What do you think about this on top? diff --git c/docs/helpers.rst i/docs/helpers.rst
index afe2f89..5e8cb17 100644
--- c/docs/helpers.rst
+++ i/docs/helpers.rst
@@ -278,24 +278,23 @@ Example
assert settings.USE_TZ
-``django_assert_num_queries``
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
.. fixture:: django_assert_num_queries
+``django_assert_num_queries``
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+.. py:function:: django_assert_num_queries(connection=None, info=None)
+
+ :param connection: optional non-default DB connection
+ :param str info: optional info message to display on failure
+
This fixture allows to check for an expected number of DB queries.
-It wraps `django.test.utils.CaptureQueriesContext`. A non-default DB
-connection can be passed in using the `connection` keyword argument, an
-additional info message which is displayed on fail can be passed in using
-the `info` keyword argument, and it will yield the wrapped
+It wraps `django.test.utils.CaptureQueriesContext` and yields the wrapped
CaptureQueriesContext instance.
-Example
-"""""""
-
-::
+Example usage::
def test_queries(django_assert_num_queries):
with django_assert_num_queries(3) as captured:
@@ -306,20 +305,21 @@ Example
assert 'foo' in captured.captured_queries[0]['sql']
+.. fixture:: django_assert_max_num_queries
+
``django_assert_max_num_queries``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-.. fixture:: django_assert_max_num_queries
+.. py:function:: django_assert_num_queries(connection=None, info=None)
+
+ :param connection: optional non-default DB connection
+ :param str info: optional info message to display on failure
This fixture allows to check for an expected maximum number of DB queries.
It is a specialized version of :fixture:`django_assert_num_queries`.
-
-Example
-"""""""
-
-::
+Example usage::
def test_max_queries(django_assert_max_num_queries):
with django_assert_max_num_queries(3): |
I'm totally okay with that! Much more understandable now. |
Added. btw: got confused when rebasing this: you had the same commit almost the same, and resolved the conflict in a merge commit. For the future, please try to push a fixup instead. |
Thank you. And I will push a fixup in future PRs. Sry for confusing you. |
…l message on failure.