2015-12-15 10:19:40 +08:00
|
|
|
import unittest
|
2017-02-02 02:12:59 +08:00
|
|
|
from contextlib import contextmanager
|
2015-12-15 10:19:40 +08:00
|
|
|
|
2017-01-18 00:16:15 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2015-12-15 10:19:40 +08:00
|
|
|
from django.db import connection
|
|
|
|
from django.test import TestCase, override_settings
|
|
|
|
|
|
|
|
|
2017-02-02 02:12:59 +08:00
|
|
|
@contextmanager
|
|
|
|
def get_connection():
|
|
|
|
new_connection = connection.copy()
|
|
|
|
yield new_connection
|
|
|
|
new_connection.close()
|
|
|
|
|
|
|
|
|
2015-12-15 10:19:40 +08:00
|
|
|
@override_settings(DEBUG=True)
|
2017-02-12 04:37:49 +08:00
|
|
|
@unittest.skipUnless(connection.vendor == 'mysql', 'MySQL tests')
|
|
|
|
class IsolationLevelTests(TestCase):
|
2015-12-15 10:19:40 +08:00
|
|
|
|
2017-02-02 02:12:59 +08:00
|
|
|
read_committed = 'read committed'
|
|
|
|
repeatable_read = 'repeatable read'
|
|
|
|
isolation_values = {
|
|
|
|
level: level.replace(' ', '-').upper()
|
|
|
|
for level in (read_committed, repeatable_read)
|
|
|
|
}
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
super().setUpClass()
|
|
|
|
configured_isolation_level = connection.isolation_level or cls.isolation_values[cls.repeatable_read]
|
|
|
|
cls.configured_isolation_level = configured_isolation_level.upper()
|
|
|
|
cls.other_isolation_level = (
|
|
|
|
cls.read_committed
|
|
|
|
if configured_isolation_level != cls.isolation_values[cls.read_committed]
|
|
|
|
else cls.repeatable_read
|
|
|
|
)
|
|
|
|
|
2017-01-18 00:16:15 +08:00
|
|
|
@staticmethod
|
|
|
|
def get_isolation_level(connection):
|
|
|
|
with connection.cursor() as cursor:
|
2017-11-15 04:40:44 +08:00
|
|
|
cursor.execute("SELECT @@session.%s" % connection.transaction_isolation_variable)
|
2017-01-18 00:16:15 +08:00
|
|
|
return cursor.fetchone()[0]
|
|
|
|
|
2015-12-15 10:19:40 +08:00
|
|
|
def test_auto_is_null_auto_config(self):
|
|
|
|
query = 'set sql_auto_is_null = 0'
|
|
|
|
connection.init_connection_state()
|
|
|
|
last_query = connection.queries[-1]['sql'].lower()
|
|
|
|
if connection.features.is_sql_auto_is_null_enabled:
|
|
|
|
self.assertIn(query, last_query)
|
|
|
|
else:
|
|
|
|
self.assertNotIn(query, last_query)
|
2017-01-18 00:16:15 +08:00
|
|
|
|
|
|
|
def test_connect_isolation_level(self):
|
2017-02-02 02:12:59 +08:00
|
|
|
self.assertEqual(self.get_isolation_level(connection), self.configured_isolation_level)
|
2017-01-18 00:16:15 +08:00
|
|
|
|
2017-02-02 02:12:59 +08:00
|
|
|
def test_setting_isolation_level(self):
|
|
|
|
with get_connection() as new_connection:
|
|
|
|
new_connection.settings_dict['OPTIONS']['isolation_level'] = self.other_isolation_level
|
|
|
|
self.assertEqual(
|
|
|
|
self.get_isolation_level(new_connection),
|
|
|
|
self.isolation_values[self.other_isolation_level]
|
|
|
|
)
|
2017-01-18 00:16:15 +08:00
|
|
|
|
2017-02-02 02:12:59 +08:00
|
|
|
def test_uppercase_isolation_level(self):
|
2017-01-18 00:16:15 +08:00
|
|
|
# Upper case values are also accepted in 'isolation_level'.
|
2017-02-02 02:12:59 +08:00
|
|
|
with get_connection() as new_connection:
|
|
|
|
new_connection.settings_dict['OPTIONS']['isolation_level'] = self.other_isolation_level.upper()
|
|
|
|
self.assertEqual(
|
|
|
|
self.get_isolation_level(new_connection),
|
|
|
|
self.isolation_values[self.other_isolation_level]
|
|
|
|
)
|
2017-01-18 00:16:15 +08:00
|
|
|
|
2017-02-02 04:34:17 +08:00
|
|
|
def test_default_isolation_level(self):
|
|
|
|
# If not specified in settings, the default is read committed.
|
|
|
|
with get_connection() as new_connection:
|
|
|
|
new_connection.settings_dict['OPTIONS'].pop('isolation_level', None)
|
|
|
|
self.assertEqual(
|
|
|
|
self.get_isolation_level(new_connection),
|
|
|
|
self.isolation_values[self.read_committed]
|
|
|
|
)
|
|
|
|
|
2017-01-18 00:16:15 +08:00
|
|
|
def test_isolation_level_validation(self):
|
|
|
|
new_connection = connection.copy()
|
|
|
|
new_connection.settings_dict['OPTIONS']['isolation_level'] = 'xxx'
|
|
|
|
msg = (
|
|
|
|
"Invalid transaction isolation level 'xxx' specified.\n"
|
|
|
|
"Use one of 'read committed', 'read uncommitted', "
|
|
|
|
"'repeatable read', 'serializable', or None."
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
|
|
|
new_connection.cursor()
|