Ticket #16329: 16329.3.diff

File 16329.3.diff, 3.4 KB (added by Ramiro Morales, 13 years ago)

Patch for the isue, includes tests

  • django/db/backends/sqlite3/creation.py

    diff -r fcf918fa9b40 django/db/backends/sqlite3/creation.py
    a b  
    7272
    7373    def set_autocommit(self):
    7474        self.connection.connection.isolation_level = None
     75
     76    def test_db_signature(self):
     77        """
     78        Returns a tuple that uniquely identifies a test database.
     79
     80        This takes into account the special cases of ":memory:" and "" for
     81        SQLite since the databases will be distinct despite having the same
     82        TEST_NAME. See http://www.sqlite.org/inmemorydb.html
     83        """
     84        settings_dict = self.connection.settings_dict
     85        test_dbname = self._get_test_db_name()
     86        sig = [self.connection.settings_dict['NAME']]
     87        if test_dbname == ':memory:':
     88            sig.append(self.connection.alias)
     89        return tuple(sig)
  • tests/regressiontests/test_runner/tests.py

    diff -r fcf918fa9b40 tests/regressiontests/test_runner/tests.py
    a b  
    1111from django.core.management import call_command
    1212from django.test import simple
    1313from django.test.simple import DjangoTestSuiteRunner, get_tests
     14from django.test.testcases import connections_support_transactions
    1415from django.test.utils import get_warnings_state, restore_warnings_state
    1516from django.utils import unittest
    1617from django.utils.importlib import import_module
     
    262263        "Test for #12658 - Tests with ImportError's shouldn't fail silently"
    263264        module = import_module(TEST_APP_ERROR)
    264265        self.assertRaises(ImportError, get_tests, module)
     266
     267
     268class Sqlite3InMemoryTestDbs(unittest.TestCase):
     269    def test_transaction_support(self):
     270        """Ticket #16329: sqlite3 in-memory test databases"""
     271        from django import db
     272        old_db_connections = db.connections
     273        for option in ('NAME', 'TEST_NAME'):
     274            try:
     275                db.connections = db.ConnectionHandler({
     276                    'default': {
     277                        'ENGINE': 'django.db.backends.sqlite3',
     278                        option: ':memory:',
     279                    },
     280                    'other': {
     281                        'ENGINE': 'django.db.backends.sqlite3',
     282                        option: ':memory:',
     283                    },
     284                })
     285                other = db.connections['other']
     286                self.assertEqual(other.features.supports_transactions, None)
     287                DjangoTestSuiteRunner(verbosity=0).setup_databases()
     288                # Transaction support should be properly initialised for the 'other' DB
     289                self.assertNotEqual(
     290                    other.features.supports_transactions,
     291                    None,
     292                    "DATABASES setting '%s' option set to sqlite3's ':memory:' value causes problems with transaction support detection." % option
     293                )
     294                # And all the DBs should report that support transactions
     295                self.assertTrue(
     296                    connections_support_transactions(),
     297                    "DATABASES setting '%s' option set to sqlite3's ':memory:' value causes problems with transaction support detection." % option
     298                )
     299            finally:
     300                db.connections = old_db_connections
Back to Top