Repository: django/django
File: django/db/backends/base/creation.py (Python)

Code before:
            #    won't import tests in that module's other submodules.
            # 2. When the parallel test runner spawns workers with an empty
            #    import cache.
            test_to_skip = import_string(test_name)
            test_frame = sys.modules.get(test_to_skip.__module__)
        else:
            test_to_skip = getattr(test_frame, class_or_method)
        return test_frame, test_to_skip

    def _skip_test(self, test_name, reason=None):
        # Only load unittest if we're actually testing.
        from unittest import expectedFailure, skip

        module_or_class_name, _, name_to_skip = test_name.rpartition(".")
        test_app = test_name.split(".")[0]
        # Importing a test app that isn't installed raises RuntimeError.
        if test_app in settings.INSTALLED_APPS:
            test_frame, test_to_skip = self._get_test_and_frame(
                module_or_class_name, name_to_skip, test_name
            )
            if reason:
                setattr(test_frame, name_to_skip, skip(reason)(test_to_skip))
            else:
                setattr(test_frame, name_to_skip, expectedFailure(test_to_skip))

    def mark_expected_failures_and_skips(self):
        """
        Mark tests in Django's test suite which are expected failures on this
        database and tests that should be skipped on this database.
        """
        for test_name in self.connection.features.django_test_expected_failures:
            self._skip_test(test_name)
        for reason, tests in self.connection.features.django_test_skips.items():
            for test_name in tests:
                self._skip_test(test_name, reason)

    def sql_table_creation_suffix(self):
        """
        SQL to append to the end of the test table creation statements.
        """
        return ""

    def test_db_signature(self):
        """
        Return a tuple with elements of self.connection.settings_dict (a
        DATABASES setting value) that uniquely identify a database
        accordingly to the RDBMS particularities.
        """
        settings_dict = self.connection.settings_dict
        return (
            settings_dict["HOST"],

Review comment (refactor):
Perhaps it's better to follow the pattern in the rest of this file (at least with destroy_test_db/_destroy_test_db) and put the helper methods after the method that calls them. Otherwise, it seems to give more prominence to the helpers.

Code after:
        # Only load unittest during testing.
        from unittest import expectedFailure, skip

        module_or_class_name, _, name_to_mark = test_name.rpartition(".")
        test_app = test_name.split(".")[0]
        # Importing a test app that isn't installed raises RuntimeError.
        if test_app in settings.INSTALLED_APPS:
            try:
                test_frame = import_string(module_or_class_name)
            except ImportError:
                # import_string() can raise ImportError if a submodule's parent
                # module hasn't already been imported during test discovery.
                # This can happen in at least two cases:
                # 1. When running a subset of tests in a module, the test
                #    runner won't import tests in that module's other
                #    submodules.
                # 2. When the parallel test runner spawns workers with an empty
                #    import cache.
                test_to_mark = import_string(test_name)
                test_frame = sys.modules.get(test_to_mark.__module__)
            else:
                test_to_mark = getattr(test_frame, name_to_mark)
            if skip_reason:
                setattr(test_frame, name_to_mark, skip(skip_reason)(test_to_mark))
            else:
                setattr(test_frame, name_to_mark, expectedFailure(test_to_mark))

    def sql_table_creation_suffix(self):
        """
        SQL to append to the end of the test table creation statements.
        """
        return ""

    def test_db_signature(self):
        """
        Return a tuple with elements of self.connection.settings_dict (a
        DATABASES setting value) that uniquely identify a database
        accordingly to the RDBMS particularities.
        """
        settings_dict = self.connection.settings_dict
        return (
            settings_dict["HOST"],
            settings_dict["PORT"],
            settings_dict["ENGINE"],
            self._get_test_db_name(),
        )

    def setup_worker_connection(self, _worker_id):
        settings_dict = self.get_test_db_clone_settings(str(_worker_id))
        # connection.settings_dict must be updated in place for changes to be
        # reflected in django.db.connections. If the following line assigned