Django

Code

Ticket #5416 (new)

Opened 10 months ago

Last modified 10 months ago

Add assertNumQueries() to testing framework

Reported by: adrian Assigned to: nobody
Milestone: Component: Unit test system
Version: SVN Keywords: feature_request
Cc: Triage Stage: Accepted
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

The Django testing framework should provide assertNumQueries(), which would assert that a given action executed a given number of database queries. This should work at the view level ("view X uses 3 queries total") or the individual statement level ("model method X uses only 1 query").

This might have to be implemented as two methods -- a "start counting" method and a "stop counting" method:

    def test_something(self):
        self.startCountingQueries()
        do_something_that_should_only_use_two_queries()
        self.assertNumQueries(2)

In this example, startCountingQueries() would reset the counter to 0, and every query would be tallied from that point on. assertNumQueries() would simply assert the query count was the given number.

Note that this depends on #5415, which provides signals for every SQL query.

Attachments

Change History

09/13/07 10:19:43 changed by adrian

  • needs_better_patch changed.
  • stage changed from Unreviewed to Accepted.
  • needs_tests changed.
  • needs_docs changed.

09/14/07 05:04:03 changed by Simon G. <dev@simon.net.nz>

Malcolm suggests that this wait until after his queryset refactor as it'll be easier then.

09/14/07 13:39:03 changed by Nis Jørgensen <nis@superlativ.dk>

I am wondering whether we would want to provide this. The number of queries being run is an internal part of the framework, not something external code should ever rely on, nor expect to be stable.

09/15/07 11:31:42 changed by ikelly

In some cases, DDL in particular, this may be backend-dependent. For example, creating a model with an AutoField? requires one query in PostgreSQL (CREATE TABLE) but three in Oracle (CREATE TABLE, CREATE SEQUENCE, CREATE TRIGGER).

09/15/07 12:35:31 changed by adrian

Nis: While SQL statements are indeed an internal part of the framework, they're also something that developers should be able to test -- particular as we add "lazily loaded" features such as in #5420.

If you don't want to test the number of queries in your own unit tests, you don't have to. :)

09/15/07 14:50:10 changed by PhiR

  • keywords set to feature_request.

Add/Change #5416 (Add assertNumQueries() to testing framework)




Change Properties
Action