Opened 7 years ago

Closed 7 years ago

#28203 closed Bug (fixed)

assertNumQueries should ignore initial connection configuration queries

Reported by: François Freitag Owned by: François Freitag
Component: Testing framework Version: dev
Severity: Normal Keywords: assertNumQueries
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Upon connection to the database, Django may trigger queries to configure the connection state (e.g. MySQL transaction isolation and AUTO_IS_NULL, oracle dates format, postgresql timezones).
IMHO, these queries should be ignored by assertNumQueries.

My use case, on MySQL 5.5.54-0 and MariaDB 10.1.23-1:

from django.contrib.contenttypes.models import ContentType
from django.test import TransactionTestCase

class MyTests(TransactionTestCase):

    available_apps = []

    def test_assertNumQueries_counts_db_configuration_queries(self):
        # Emulates connection being closed after previous query/test
        # possibly because the connection expired (e.g. CONN_MAX_AGE=0)
        with self.assertNumQueries(1):

        # Fails with:
        # AssertionError: 2 != 1 : 2 queries executed, 1 expected
        # Captured queries were:
        # SET TX_ISOLATION = 'read-committed'
        # SELECT `django_content_type`.`id`, `django_content_type`.`app_label`, `django_content_type`.`model` FROM `django_content_type`

Thanks to Cole Maclean for the initial report.

Change History (4)

comment:1 by Tim Graham, 7 years ago

Triage Stage: UnreviewedAccepted

comment:2 by François Freitag, 7 years ago

Owner: changed from nobody to François Freitag
Status: newassigned

comment:3 by François Freitag, 7 years ago

Has patch: set

comment:4 by Tim Graham <timograham@…>, 7 years ago

Resolution: fixed
Status: assignedclosed

In 98b3b14a:

Fixed #28203 -- Ignored connection configuration queries in assertNumQueries().

Note: See TracTickets for help on using tickets.
Back to Top