#27539: assertNumQueries fails if debug cursor wrapper fills
---------------------------------------------+------------------------
               Reporter:  Malcolm Box        |          Owner:  nobody
                   Type:  Bug                |         Status:  new
              Component:  Testing framework  |        Version:  1.10
               Severity:  Normal             |       Keywords:
           Triage Stage:  Unreviewed         |      Has patch:  0
    Needs documentation:  0                  |    Needs tests:  0
Patch needs improvement:  0                  |  Easy pickings:  0
                  UI/UX:  0                  |
---------------------------------------------+------------------------
 Recently I had a test fail that went like this:


 {{{
 with assertNumQueries(5):
    response = self.client.get('/a/view')
    # Various checks that the response had the correct data
 }}}

 This was failing with `AssertionError: 0 queries executed, 5 expected` yet
 was hitting the database and returning the correct data. The failure only
 happened when running certain subsets of the test suite - but not if run
 in isolation.

 Eventually I tracked this down to the following code in
 `CaptureQueriesContext`


 {{{
     @property
     def captured_queries(self):
         return
 self.connection.queries[self.initial_queries:self.final_queries]

     def __enter__(self):
         self.force_debug_cursor = self.connection.force_debug_cursor
         self.connection.force_debug_cursor = True
         self.initial_queries = len(self.connection.queries_log)
         self.final_queries = None
         request_started.disconnect(reset_queries)
         return self
 }}}

 and this code in `BaseDatabaseWrapper`:


 {{{
 class BaseDatabaseWrapper(object):
     queries_limit = 9000

     def __init__(self, settings_dict, alias=DEFAULT_DB_ALIAS,
         # Query logging in debug mode or when explicitly enabled.
         self.queries_log = deque(maxlen=self.queries_limit)
 }}}

 This means that if the queries log hits 9K entries, `assertNumQueries`
 will no longer see there as being any new queries, and will return 0 for
 the `captured_queries`, which is rather tricky to debug.

 The minimal solution would be for `CaptureQueriesContext` to check if
 `self.initial_queries` is >= than `queries_limit` and noisily fail if so,
 since the test won't work if it is.

 I don't yet have a better solution....

--
Ticket URL: <https://code.djangoproject.com/ticket/27539>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/047.5505dfa954db6853196b3b3a448c3ac2%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to