Ticket #14415: t14415.diff

File t14415.diff, 3.9 KB (added by Russell Keith-Magee, 14 years ago)

Alternative solution based around the test runner

  • django/test/simple.py

    diff -r b731c0310f0b django/test/simple.py
    a b  
    217217
    218218    def setup_databases(self, **kwargs):
    219219        from django.db import connections
     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 = {}
     225        for alias in connections:
     226            connection = connections[alias]
     227            if connection.settings_dict['TEST_MIRROR']:
     228                # If the database is marked as a test mirror, save
     229                # the alias.
     230                mirrored_aliases[alias] = connection.settings_dict['TEST_MIRROR']
     231            else:
     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['ENGINE'],
     237                        connection.settings_dict['NAME'],
     238                    ), []).append(alias)
     239
     240        # Second pass -- actually create the databases.
    220241        old_names = []
    221242        mirrors = []
    222         for alias in connections:
    223             connection = connections[alias]
    224             # If the database is a test mirror, redirect it's connection
    225             # instead of creating a test database.
    226             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]
    230             else:
    231                 old_names.append((connection, connection.settings_dict['NAME']))
    232                 connection.creation.create_test_db(self.verbosity, autoclobber=not self.interactive)
     243        for (engine, db_name), aliases in test_databases.items():
     244            # Actually create the database for the first connection
     245            connection = connections[aliases[0]]
     246            old_names.append((connection, db_name, True))
     247            test_db_name = connection.creation.create_test_db(self.verbosity, autoclobber=not self.interactive)
     248            for alias in aliases[1:]:
     249                connection = connections[alias]
     250                if db_name:
     251                    old_names.append((connection, db_name, False))
     252                    connection.settings_dict['NAME'] = test_db_name
     253                else:
     254                    # If settings_dict['NAME'] isn't defined, we have a backend where
     255                    # the name isn't important -- e.g., SQLite, which uses :memory:.
     256                    # Force create the database instead of assuming it's a duplicate.
     257                    old_names.append((connection, db_name, True))
     258                    connection.creation.create_test_db(self.verbosity, autoclobber=not self.interactive)
     259
     260        for alias, mirror_alias in mirrored_aliases.items():
     261            mirrors.append((alias, connections[alias]))
     262            connections._connections[alias] = connections[mirror_alias]
    233263        return old_names, mirrors
    234264
    235265    def run_suite(self, suite, **kwargs):
     
    242272        for alias, connection in mirrors:
    243273            connections._connections[alias] = connection
    244274        # Destroy all the non-mirror databases
    245         for connection, old_name in old_names:
    246             connection.creation.destroy_test_db(old_name, self.verbosity)
     275        for connection, old_name, destroy in old_names:
     276            if destroy:
     277                connection.creation.destroy_test_db(old_name, self.verbosity)
     278            else:
     279                connection.settings_dict['NAME'] = old_name
    247280
    248281    def teardown_test_environment(self, **kwargs):
    249282        unittest.removeHandler()
Back to Top