Skip to content

Add fixture for asserting maximum number of database queries #547

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

Merged
merged 3 commits into from
Jul 27, 2018

Conversation

anrie
Copy link
Contributor

@anrie anrie commented Dec 2, 2017

Often I don't want to test against the exact number of queries performed by a particular piece of code but just be sure that changes to the codebase don't end up in an unnoticed excessive increase of queries.

django-test-plus has a really nice helper for this kind of scenario: https://github.com/revsys/django-test-plus#assertnumquerieslessthannumber---context

This PR tries to port the same functionality to pytest-django.

Open issues:

  • No tests so far
  • Does renaming assert_num_queries to assert_exact_num_queries make sense?

Cheers,
Andreas

@blueyed
Copy link
Contributor

blueyed commented Dec 3, 2017

Please rebase, since our tests in master were failing with pytest 3.3.

@anrie anrie force-pushed the feat/max_num_queries branch 2 times, most recently from fcacbe0 to 2bf9f3b Compare December 4, 2017 08:09
@codecov-io
Copy link

codecov-io commented Dec 4, 2017

Codecov Report

Merging #547 into master will increase coverage by 0.08%.
The diff coverage is 97.87%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #547      +/-   ##
==========================================
+ Coverage   92.33%   92.42%   +0.08%     
==========================================
  Files          32       32              
  Lines        1710     1742      +32     
  Branches      142      143       +1     
==========================================
+ Hits         1579     1610      +31     
- Misses         94       95       +1     
  Partials       37       37
Flag Coverage Δ
#dj110 84.67% <97.87%> (+0.22%) ⬆️
#dj111 86.73% <97.87%> (+0.18%) ⬆️
#dj18 85.47% <97.87%> (+0.33%) ⬆️
#dj19 84.55% <97.87%> (+0.23%) ⬆️
#dj20 84.95% <97.87%> (+0.22%) ⬆️
#dj21 84.95% <97.87%> (+0.22%) ⬆️
#mysql_innodb 84.95% <97.87%> (+0.22%) ⬆️
#mysql_myisam 84.9% <97.87%> (+0.22%) ⬆️
#postgres 88.28% <97.87%> (+0.16%) ⬆️
#py27 89.83% <97.87%> (+0.13%) ⬆️
#py34 84.55% <97.87%> (+0.23%) ⬆️
#py35 84.67% <97.87%> (+0.22%) ⬆️
#py36 85.41% <97.87%> (+0.21%) ⬆️
#sqlite 86.73% <97.87%> (+0.18%) ⬆️
#sqlite_file 84.55% <97.87%> (+0.23%) ⬆️
Impacted Files Coverage Δ
pytest_django/plugin.py 86.04% <100%> (+0.04%) ⬆️
pytest_django/fixtures.py 97.05% <100%> (+0.14%) ⬆️
tests/test_fixtures.py 99.59% <96%> (-0.41%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c0e5b6c...7a655f8. Read the comment docs.

@anrie
Copy link
Contributor Author

anrie commented Dec 4, 2017

@blueyed I did a rebase and the tests are now passing.

@blueyed
Copy link
Contributor

blueyed commented Dec 25, 2017

@lukaszb
What do you think?

@lukaszb
Copy link
Contributor

lukaszb commented Dec 25, 2017

I'd leave previous name - it might be less strict but it's how Django named their test method and I'm for consistency here.

Other than that I don't see anything wrong with max alternative.

@blueyed
Copy link
Contributor

blueyed commented Dec 25, 2017

I agree with @lukaszb.

@anrie
Please change the existing fixture to keep the existing name.

@anrie anrie force-pushed the feat/max_num_queries branch from 2bf9f3b to 36a0f5d Compare December 26, 2017 10:18
@anrie
Copy link
Contributor Author

anrie commented Dec 26, 2017

@blueyed There you go!

@blueyed blueyed added this to the 3.2 milestone Jan 18, 2018
@blueyed blueyed modified the milestones: 3.2, 3.3 Apr 14, 2018
@blueyed blueyed force-pushed the feat/max_num_queries branch from bb984c7 to f132019 Compare July 26, 2018 04:00
@blueyed
Copy link
Contributor

blueyed commented Jul 26, 2018

Rebased and fixed a warning reported by Sphinx.

@blueyed
Copy link
Contributor

blueyed commented Jul 26, 2018

Extended it a bit to return the wrapped context manager, and added support for connection.
Does it look fine?

@anrie
Copy link
Contributor Author

anrie commented Jul 27, 2018

Looks very good to me!

@blueyed blueyed merged commit 7a655f8 into pytest-dev:master Jul 27, 2018
blueyed added a commit that referenced this pull request Jul 27, 2018
@blueyed
Copy link
Contributor

blueyed commented Jul 27, 2018

Thank you, merged!

beyondgeeks pushed a commit to beyondgeeks/django-pytest that referenced this pull request Sep 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants