[1.8.x] Refs #12118 -- Allowed "mode=memory" in SQLite test database names.

Backport of 3543fec3b7 from master
This commit is contained in:
Riccardo Magliocchetti 2015-10-05 19:25:06 +02:00 committed by Tim Graham
parent de570d4da9
commit e2db171e15
3 changed files with 32 additions and 3 deletions

View File

@ -18,14 +18,16 @@ class DatabaseCreation(BaseDatabaseCreation):
def _get_test_db_name(self):
test_database_name = self.connection.settings_dict['TEST']['NAME']
can_share_in_memory_db = self.connection.features.can_share_in_memory_db
if test_database_name and test_database_name != ':memory:':
if 'mode=memory' in test_database_name:
if 'mode=memory' in test_database_name and not can_share_in_memory_db:
raise ImproperlyConfigured(
"Using `mode=memory` parameter in the database name is not allowed, "
"Using a shared memory database with `mode=memory` in the "
"database name is not supported in your environment, "
"use `:memory:` instead."
)
return test_database_name
if self.connection.features.can_share_in_memory_db:
if can_share_in_memory_db:
return 'file:memorydb_%s?mode=memory&cache=shared' % self.connection.alias
return ':memory:'

View File

@ -11,3 +11,6 @@ Bugfixes
* Fixed a regression causing ``ModelChoiceField`` to ignore
``prefetch_related()`` on its queryset (:ticket:`25496`).
* Allowed "mode=memory" in SQLite test database name if supported
(:ticket:`12118`).

View File

@ -146,6 +146,30 @@ class SQLiteTests(TestCase):
models.Item.objects.all().aggregate,
**{'complex': aggregate('last_modified') + aggregate('last_modified')})
def test_memory_db_test_name(self):
"""
A named in-memory db should be allowed where supported.
"""
from django.db.backends.sqlite3.base import DatabaseWrapper
settings_dict = {
'TEST': {
'NAME': 'file:memorydb_test?mode=memory&cache=shared',
}
}
wrapper = DatabaseWrapper(settings_dict)
creation = wrapper.creation
if creation.connection.features.can_share_in_memory_db:
expected = creation.connection.settings_dict['TEST']['NAME']
self.assertEqual(creation._get_test_db_name(), expected)
else:
msg = (
"Using a shared memory database with `mode=memory` in the "
"database name is not supported in your environment, "
"use `:memory:` instead."
)
with self.assertRaisesMessage(ImproperlyConfigured, msg):
creation._get_test_db_name()
@unittest.skipUnless(connection.vendor == 'postgresql', "Test only for PostgreSQL")
class PostgreSQLTests(TestCase):