Fixed #27539 -- Made TransactionTestCase._pre_setup() clear the queries_log so it's less likely to overflow.

TransactionTestCase.assertNumQueries() fails in an overflow situation.
This commit is contained in:
reficul31 2017-02-28 06:30:09 +05:30 committed by Tim Graham
parent 6f44f714c9
commit 92e286498a
2 changed files with 22 additions and 1 deletions

View File

@ -827,6 +827,11 @@ class TransactionTestCase(SimpleTestCase):
enter=False, enter=False,
) )
raise raise
# Clear the queries_log so that it's less likley to overflow (a single
# test probably won't execute 9K queries). If queries_log overflows,
# then assertNumQueries() doesn't work.
for db_name in self._databases_names(include_mirrors=False):
connections[db_name].queries_log.clear()
@classmethod @classmethod
def _databases_names(cls, include_mirrors=True): def _databases_names(cls, include_mirrors=True):

View File

@ -1,6 +1,7 @@
from unittest import mock from unittest import mock
from django.test import TransactionTestCase from django.db import connections
from django.test import TestCase, TransactionTestCase, override_settings
class TestSerializedRollbackInhibitsPostMigrate(TransactionTestCase): class TestSerializedRollbackInhibitsPostMigrate(TransactionTestCase):
@ -28,3 +29,18 @@ class TestSerializedRollbackInhibitsPostMigrate(TransactionTestCase):
reset_sequences=False, inhibit_post_migrate=True, reset_sequences=False, inhibit_post_migrate=True,
database='default', verbosity=0, database='default', verbosity=0,
) )
@override_settings(DEBUG=True) # Enable query logging for test_queries_cleared
class TransactionTestCaseMultiDbTests(TestCase):
available_apps = []
multi_db = True
def test_queries_cleared(self):
"""
TransactionTestCase._pre_setup() clears the connections' queries_log
so that it's less likely to overflow. An overflow causes
assertNumQueries() to fail.
"""
for alias in connections:
self.assertEqual(len(connections[alias].queries_log), 0, 'Failed for alias %s' % alias)