Split setup_databases.

It contained two sections separated by comments. It makes more sense to
put each section in its own function.
This commit is contained in:
Aymeric Augustin 2015-09-05 19:39:42 +02:00
parent c4bdf52005
commit 5ace1887f2
1 changed files with 27 additions and 6 deletions

View File

@ -1,3 +1,4 @@
import collections
import logging import logging
import os import os
import unittest import unittest
@ -321,9 +322,19 @@ def partition_suite(suite, classes, bins, reverse=False):
bins[-1].add(test) bins[-1].add(test)
def setup_databases(verbosity, interactive, keepdb=False, debug_sql=False, **kwargs): def get_unique_databases_and_mirrors():
# First pass -- work out which databases actually need to be created, """
# and which ones are test mirrors or duplicate entries in DATABASES Figure out which databases actually need to be created.
Deduplicate entries in DATABASES that correspond the same database or are
configured as test mirrors.
Returns two values:
- test_databases: ordered mapping of signatures to (name, list of aliases)
where all aliases share the same unerlying database
- mirrored_aliases: mapping of mirror aliases to original aliases
"""
mirrored_aliases = {} mirrored_aliases = {}
test_databases = {} test_databases = {}
dependencies = {} dependencies = {}
@ -351,12 +362,21 @@ def setup_databases(verbosity, interactive, keepdb=False, debug_sql=False, **kwa
if alias != DEFAULT_DB_ALIAS and connection.creation.test_db_signature() != default_sig: if alias != DEFAULT_DB_ALIAS and connection.creation.test_db_signature() != default_sig:
dependencies[alias] = test_settings.get('DEPENDENCIES', [DEFAULT_DB_ALIAS]) dependencies[alias] = test_settings.get('DEPENDENCIES', [DEFAULT_DB_ALIAS])
# Second pass -- actually create the databases. test_databases = dependency_ordered(test_databases.items(), dependencies)
test_databases = collections.OrderedDict(test_databases)
return test_databases, mirrored_aliases
def setup_databases(verbosity, interactive, keepdb=False, debug_sql=False, **kwargs):
"""
Creates the test databases.
"""
test_databases, mirrored_aliases = get_unique_databases_and_mirrors()
old_names = [] old_names = []
mirrors = [] mirrors = []
for signature, (db_name, aliases) in dependency_ordered( for signature, (db_name, aliases) in test_databases.items():
test_databases.items(), dependencies):
test_db_name = None test_db_name = None
# Actually create the database for the first connection # Actually create the database for the first connection
for alias in aliases: for alias in aliases:
@ -382,4 +402,5 @@ def setup_databases(verbosity, interactive, keepdb=False, debug_sql=False, **kwa
if debug_sql: if debug_sql:
for alias in connections: for alias in connections:
connections[alias].force_debug_cursor = True connections[alias].force_debug_cursor = True
return old_names, mirrors return old_names, mirrors