﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
27539	assertNumQueries fails if debug cursor wrapper fills	Malcolm Box	reficul31	"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....






"	Bug	closed	Testing framework	1.10	Normal	fixed			Accepted	1	0	0	1	0	0
