From 92e286498acd4f4562be11c21879856de8f47252 Mon Sep 17 00:00:00 2001 From: reficul31 Date: Tue, 28 Feb 2017 06:30:09 +0530 Subject: [PATCH] Fixed #27539 -- Made TransactionTestCase._pre_setup() clear the queries_log so it's less likely to overflow. TransactionTestCase.assertNumQueries() fails in an overflow situation. --- django/test/testcases.py | 5 +++++ tests/test_utils/test_transactiontestcase.py | 18 +++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/django/test/testcases.py b/django/test/testcases.py index 6492144a2e..9d890301b9 100644 --- a/django/test/testcases.py +++ b/django/test/testcases.py @@ -827,6 +827,11 @@ class TransactionTestCase(SimpleTestCase): enter=False, ) 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 def _databases_names(cls, include_mirrors=True): diff --git a/tests/test_utils/test_transactiontestcase.py b/tests/test_utils/test_transactiontestcase.py index 593c06f7e3..40c9b7576f 100644 --- a/tests/test_utils/test_transactiontestcase.py +++ b/tests/test_utils/test_transactiontestcase.py @@ -1,6 +1,7 @@ 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): @@ -28,3 +29,18 @@ class TestSerializedRollbackInhibitsPostMigrate(TransactionTestCase): reset_sequences=False, inhibit_post_migrate=True, 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)