Ticket #14415: t14415.2.diff

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

Updated patch for test database issue

  • django/test/simple.py

    diff -r 9e713caf7598 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['HOST'],
     237                        connection.settings_dict['ENGINE'],
     238                        connection.settings_dict['NAME'],
     239                    ), []).append(alias)
     240
     241        # Second pass -- actually create the databases.
    220242        old_names = []
    221243        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)
     244        for (host, engine, db_name), aliases in test_databases.items():
     245            # Actually create the database for the first connection
     246            connection = connections[aliases[0]]
     247            old_names.append((connection, db_name, True))
     248            test_db_name = connection.creation.create_test_db(self.verbosity, autoclobber=not self.interactive)
     249            for alias in aliases[1:]:
     250                connection = connections[alias]
     251                if db_name:
     252                    old_names.append((connection, db_name, False))
     253                    connection.settings_dict['NAME'] = test_db_name
     254                else:
     255                    # If settings_dict['NAME'] isn't defined, we have a backend where
     256                    # the name isn't important -- e.g., SQLite, which uses :memory:.
     257                    # Force create the database instead of assuming it's a duplicate.
     258                    old_names.append((connection, db_name, True))
     259                    connection.creation.create_test_db(self.verbosity, autoclobber=not self.interactive)
     260
     261        for alias, mirror_alias in mirrored_aliases.items():
     262            mirrors.append((alias, connections[alias].settings_dict['NAME']))
     263            connections[alias].settings_dict['NAME'] = connections[mirror_alias].settings_dict['NAME']
     264
    233265        return old_names, mirrors
    234266
    235267    def run_suite(self, suite, **kwargs):
     
    239271        from django.db import connections
    240272        old_names, mirrors = old_config
    241273        # Point all the mirrors back to the originals
    242         for alias, connection in mirrors:
    243             connections._connections[alias] = connection
     274        for alias, old_name in mirrors:
     275            connections[alias].settings_dict['NAME'] = old_name
    244276        # Destroy all the non-mirror databases
    245         for connection, old_name in old_names:
    246             connection.creation.destroy_test_db(old_name, self.verbosity)
     277        for connection, old_name, destroy in old_names:
     278            if destroy:
     279                connection.creation.destroy_test_db(old_name, self.verbosity)
     280            else:
     281                connection.settings_dict['NAME'] = old_name
    247282
    248283    def teardown_test_environment(self, **kwargs):
    249284        unittest.removeHandler()
Back to Top