Ticket #14415: t14415.3.diff
File t14415.3.diff, 4.2 KB (added by , 14 years ago) |
---|
-
django/test/simple.py
217 217 218 218 def setup_databases(self, **kwargs): 219 219 from django.db import connections 220 old_names = [] 221 mirrors = [] 220 221 # First pass -- work out which databases actually need to be created, 222 # and which ones are test mirrors or duplicate entries in DATABASES 223 mirrored_aliases = {} 224 test_databases = {} 222 225 for alias in connections: 223 226 connection = connections[alias] 224 # If the database is a test mirror, redirect it's connection225 # instead of creating a test database.226 227 if connection.settings_dict['TEST_MIRROR']: 227 mirrors.append((alias, connection))228 mirror_alias = connection.settings_dict['TEST_MIRROR']229 connections._connections[alias] = connections[mirror_alias]228 # If the database is marked as a test mirror, save 229 # the alias. 230 mirrored_aliases[alias] = connection.settings_dict['TEST_MIRROR'] 230 231 else: 231 old_names.append((connection, connection.settings_dict['NAME'])) 232 connection.creation.create_test_db(self.verbosity, autoclobber=not self.interactive) 232 # Store the (engine, name) pair. If we have two aliases 233 # with the same pair, we only need to create the test database 234 # once. 235 test_databases.setdefault(( 236 connection.settings_dict['HOST'], 237 connection.settings_dict['PORT'], 238 connection.settings_dict['ENGINE'], 239 connection.settings_dict['NAME'], 240 ), []).append(alias) 241 242 # Second pass -- actually create the databases. 243 old_names = [] 244 mirrors = [] 245 for (host, port, engine, db_name), aliases in test_databases.items(): 246 # Actually create the database for the first connection 247 connection = connections[aliases[0]] 248 old_names.append((connection, db_name, True)) 249 test_db_name = connection.creation.create_test_db(self.verbosity, autoclobber=not self.interactive) 250 for alias in aliases[1:]: 251 connection = connections[alias] 252 if db_name: 253 old_names.append((connection, db_name, False)) 254 connection.settings_dict['NAME'] = test_db_name 255 else: 256 # If settings_dict['NAME'] isn't defined, we have a backend where 257 # the name isn't important -- e.g., SQLite, which uses :memory:. 258 # Force create the database instead of assuming it's a duplicate. 259 old_names.append((connection, db_name, True)) 260 connection.creation.create_test_db(self.verbosity, autoclobber=not self.interactive) 261 262 for alias, mirror_alias in mirrored_aliases.items(): 263 mirrors.append((alias, connections[alias].settings_dict['NAME'])) 264 connections[alias].settings_dict['NAME'] = connections[mirror_alias].settings_dict['NAME'] 265 233 266 return old_names, mirrors 234 267 235 268 def run_suite(self, suite, **kwargs): … … 239 272 from django.db import connections 240 273 old_names, mirrors = old_config 241 274 # Point all the mirrors back to the originals 242 for alias, connectionin mirrors:243 connections ._connections[alias] = connection275 for alias, old_name in mirrors: 276 connections[alias].settings_dict['NAME'] = old_name 244 277 # Destroy all the non-mirror databases 245 for connection, old_name in old_names: 246 connection.creation.destroy_test_db(old_name, self.verbosity) 278 for connection, old_name, destroy in old_names: 279 if destroy: 280 connection.creation.destroy_test_db(old_name, self.verbosity) 281 else: 282 connection.settings_dict['NAME'] = old_name 247 283 248 284 def teardown_test_environment(self, **kwargs): 249 285 unittest.removeHandler()