From d2b03e5d8d79f74ce18f2eeadaac498dc119c1b4 Mon Sep 17 00:00:00 2001 From: Bertrand Bordage Date: Wed, 17 Jun 2015 23:00:52 +0200 Subject: [PATCH] Creates a MySQL cursor before each test to avoid the extra `SET SQL_AUTO_IS_NULL = 0` request. --- cachalot/tests/settings.py | 7 +++++++ cachalot/tests/thread_safety.py | 7 +++++++ cachalot/tests/transaction.py | 7 +++++++ cachalot/tests/write.py | 4 ++++ 4 files changed, 25 insertions(+) diff --git a/cachalot/tests/settings.py b/cachalot/tests/settings.py index a14335c..418e94c 100644 --- a/cachalot/tests/settings.py +++ b/cachalot/tests/settings.py @@ -6,6 +6,7 @@ try: except ImportError: # For Python 2.6 from unittest2 import skipIf +from django import VERSION as django_version from django.conf import settings from django.core.cache import DEFAULT_CACHE_ALIAS from django.db import connection @@ -16,6 +17,12 @@ from .models import Test class SettingsTestCase(TransactionTestCase): + def setUp(self): + if django_version >= (1, 7) and connection.vendor == 'mysql': + # We need to reopen the connection or Django + # will execute an extra SQL request below. + connection.cursor() + @override_settings(CACHALOT_ENABLED=False) def test_decorator(self): with self.assertNumQueries(1): diff --git a/cachalot/tests/thread_safety.py b/cachalot/tests/thread_safety.py index 6635254..8188674 100644 --- a/cachalot/tests/thread_safety.py +++ b/cachalot/tests/thread_safety.py @@ -3,6 +3,7 @@ from __future__ import unicode_literals from threading import Thread +from django import VERSION as django_version from django.db import connection, transaction from django.test import TransactionTestCase, skipUnlessDBFeature @@ -22,6 +23,12 @@ class TestThread(Thread): @skipUnlessDBFeature('test_db_allows_multiple_connections') class ThreadSafetyTestCase(TransactionTestCase): + def setUp(self): + if django_version >= (1, 7) and connection.vendor == 'mysql': + # We need to reopen the connection or Django + # will execute an extra SQL request below. + connection.cursor() + def test_concurrent_caching(self): t1 = TestThread().start_and_join() t = Test.objects.create(name='test') diff --git a/cachalot/tests/transaction.py b/cachalot/tests/transaction.py index b833daa..2fab882 100644 --- a/cachalot/tests/transaction.py +++ b/cachalot/tests/transaction.py @@ -2,6 +2,7 @@ from __future__ import unicode_literals +from django import VERSION as django_version from django.contrib.auth.models import User from django.db import connection, transaction from django.test import TransactionTestCase @@ -10,6 +11,12 @@ from .models import Test class AtomicTestCase(TransactionTestCase): + def setUp(self): + if django_version >= (1, 7) and connection.vendor == 'mysql': + # We need to reopen the connection or Django + # will execute an extra SQL request below. + connection.cursor() + def test_successful_read_atomic(self): is_sqlite = connection.vendor == 'sqlite' diff --git a/cachalot/tests/write.py b/cachalot/tests/write.py index 9279769..fe50610 100644 --- a/cachalot/tests/write.py +++ b/cachalot/tests/write.py @@ -25,6 +25,10 @@ class WriteTestCase(TransactionTestCase): def setUp(self): self.is_sqlite = connection.vendor == 'sqlite' + if django_version >= (1, 7) and connection.vendor == 'mysql': + # We need to reopen the connection or Django + # will execute an extra SQL request below. + connection.cursor() def test_create(self): with self.assertNumQueries(1):