Ticket #14415: t14415.3.diff

File t14415.3.diff, 4.2 KB (added by Shai Berger, 10 years ago)

just adding PORT to t14415.2.diff

  • django/test/simple.py

     
    217217
    218218    def setup_databases(self, **kwargs):
    219219        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 = {}
    222225        for alias in connections:
    223226            connection = connections[alias]
    224             # If the database is a test mirror, redirect it's connection
    225             # instead of creating a test database.
    226227            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']
    230231            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
    233266        return old_names, mirrors
    234267
    235268    def run_suite(self, suite, **kwargs):
     
    239272        from django.db import connections
    240273        old_names, mirrors = old_config
    241274        # Point all the mirrors back to the originals
    242         for alias, connection in mirrors:
    243             connections._connections[alias] = connection
     275        for alias, old_name in mirrors:
     276            connections[alias].settings_dict['NAME'] = old_name
    244277        # 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
    247283
    248284    def teardown_test_environment(self, **kwargs):
    249285        unittest.removeHandler()
Back to Top