mirror of
https://github.com/django/django.git
synced 2025-08-03 18:38:50 +00:00
Fixed #28203 -- Ignored connection configuration queries in assertNumQueries().
This commit is contained in:
parent
f599747fc8
commit
98b3b14a64
3 changed files with 36 additions and 1 deletions
|
@ -14,7 +14,8 @@ from django.forms import EmailField, IntegerField
|
|||
from django.http import HttpResponse
|
||||
from django.template.loader import render_to_string
|
||||
from django.test import (
|
||||
SimpleTestCase, TestCase, skipIfDBFeature, skipUnlessDBFeature,
|
||||
SimpleTestCase, TestCase, TransactionTestCase, skipIfDBFeature,
|
||||
skipUnlessDBFeature,
|
||||
)
|
||||
from django.test.html import HTMLParseError, parse_html
|
||||
from django.test.utils import (
|
||||
|
@ -157,6 +158,32 @@ class AssertNumQueriesTests(TestCase):
|
|||
self.assertNumQueries(2, test_func)
|
||||
|
||||
|
||||
@unittest.skipUnless(
|
||||
connection.vendor != 'sqlite' or not connection.is_in_memory_db(),
|
||||
'For SQLite in-memory tests, closing the connection destroys the database.'
|
||||
)
|
||||
class AssertNumQueriesUponConnectionTests(TransactionTestCase):
|
||||
available_apps = []
|
||||
|
||||
def test_ignores_connection_configuration_queries(self):
|
||||
real_ensure_connection = connection.ensure_connection
|
||||
connection.close()
|
||||
|
||||
def make_configuration_query():
|
||||
is_opening_connection = connection.connection is None
|
||||
real_ensure_connection()
|
||||
|
||||
if is_opening_connection:
|
||||
# Avoid infinite recursion. Creating a cursor calls
|
||||
# ensure_connection() which is currently mocked by this method.
|
||||
connection.cursor().execute('SELECT 1' + connection.features.bare_select_suffix)
|
||||
|
||||
ensure_connection = 'django.db.backends.base.base.BaseDatabaseWrapper.ensure_connection'
|
||||
with mock.patch(ensure_connection, side_effect=make_configuration_query):
|
||||
with self.assertNumQueries(1):
|
||||
list(Car.objects.all())
|
||||
|
||||
|
||||
class AssertQuerysetEqualTests(TestCase):
|
||||
def setUp(self):
|
||||
self.p1 = Person.objects.create(name='p1')
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue