Simplified deduplication of test databases.

Use the same code path for:

- a database that has the same settings as another database
  (as defined by test_db_signature)
- a database this is defined as a mirror of another database

There's no conceptual difference between these two cases.

Thanks Shai for the suggestion.
This commit is contained in:
Aymeric Augustin 2015-09-06 09:45:36 +02:00
parent e8bfc1c747
commit 49eee84245
1 changed files with 22 additions and 28 deletions

View File

@ -321,66 +321,63 @@ def partition_suite(suite, classes, bins, reverse=False):
bins[-1].add(test) bins[-1].add(test)
def get_unique_databases_and_mirrors(): def get_unique_databases():
""" """
Figure out which databases actually need to be created. Figure out which databases actually need to be created.
Deduplicate entries in DATABASES that correspond the same database or are Deduplicate entries in DATABASES that correspond the same database or are
configured as test mirrors. configured as test mirrors.
Returns two values: Returns an ordered mapping of signatures to (name, list of aliases)
where all aliases share the same unerlying database.
- 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 = {}
test_databases = {} test_databases = {}
dependencies = {} dependencies = {}
default_sig = connections[DEFAULT_DB_ALIAS].creation.test_db_signature() default_sig = connections[DEFAULT_DB_ALIAS].creation.test_db_signature()
for alias in connections: for alias in connections:
connection = connections[alias] connection = connections[alias]
test_settings = connection.settings_dict['TEST'] test_settings = connection.settings_dict['TEST']
if test_settings['MIRROR']: if test_settings['MIRROR']:
# If the database is marked as a test mirror, save target = test_settings['MIRROR']
# the alias. signature = connections[target].creation.test_db_signature()
mirrored_aliases[alias] = test_settings['MIRROR']
else: else:
# Store a tuple with DB parameters that uniquely identify it. signature = connection.creation.test_db_signature()
# If we have two aliases with the same values for that tuple,
# we only need to create the test database once.
item = test_databases.setdefault(
connection.creation.test_db_signature(),
(connection.settings_dict['NAME'], set())
)
item[1].add(alias)
if 'DEPENDENCIES' in test_settings: if 'DEPENDENCIES' in test_settings:
dependencies[alias] = test_settings['DEPENDENCIES'] dependencies[alias] = test_settings['DEPENDENCIES']
else: elif alias != DEFAULT_DB_ALIAS and 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])
# Store a tuple with DB parameters that uniquely identify it.
# If we have two aliases with the same values for that tuple,
# we only need to create the test database once.
item = test_databases.setdefault(
signature, (connection.settings_dict['NAME'], set()))
item[1].add(alias)
test_databases = dependency_ordered(test_databases.items(), dependencies) test_databases = dependency_ordered(test_databases.items(), dependencies)
test_databases = collections.OrderedDict(test_databases) test_databases = collections.OrderedDict(test_databases)
return test_databases, mirrored_aliases return test_databases
def setup_databases(verbosity, interactive, keepdb=False, debug_sql=False, **kwargs): def setup_databases(verbosity, interactive, keepdb=False, debug_sql=False, **kwargs):
""" """
Creates the test databases. Creates the test databases.
""" """
test_databases, mirrored_aliases = get_unique_databases_and_mirrors() test_databases = get_unique_databases()
old_names = [] old_names = []
for signature, (db_name, aliases) in test_databases.items(): for signature, (db_name, aliases) in test_databases.items():
first_alias = None first_alias = None
# Actually create the database for the first connection
for alias in aliases: for alias in aliases:
connection = connections[alias] connection = connections[alias]
old_names.append((connection, db_name, first_alias is None)) old_names.append((connection, db_name, first_alias is None))
# Actually create the database for the first connection
if first_alias is None: if first_alias is None:
first_alias = alias first_alias = alias
connection.creation.create_test_db( connection.creation.create_test_db(
@ -389,14 +386,11 @@ def setup_databases(verbosity, interactive, keepdb=False, debug_sql=False, **kwa
keepdb=keepdb, keepdb=keepdb,
serialize=connection.settings_dict.get("TEST", {}).get("SERIALIZE", True), serialize=connection.settings_dict.get("TEST", {}).get("SERIALIZE", True),
) )
# Configure all other connections as mirrors of the first one
else: else:
connections[alias].creation.set_as_test_mirror( connections[alias].creation.set_as_test_mirror(
connections[first_alias].settings_dict) connections[first_alias].settings_dict)
for alias, mirror_alias in mirrored_aliases.items():
connections[alias].creation.set_as_test_mirror(
connections[mirror_alias].settings_dict)
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