Exemplo n.º 1
0
    def create_test_db(self, verbosity=0, autoclobber=False):
        kwargs = self.__create_test_kwargs()
        if not _IS_JYTHON:
            old_database = kwargs['database']
            max_db_name_length = self.connection.ops.max_db_name_length()
            kwargs['database'] = truncate_name("%s%s" % ( TEST_DBNAME_PREFIX, old_database ), max_db_name_length)
            kwargsKeys = list(kwargs.keys())
            if ( kwargsKeys.__contains__('port') and
                     kwargsKeys.__contains__('host') ):
                kwargs['dsn'] = "DATABASE=%s;HOSTNAME=%s;PORT=%s;PROTOCOL=TCPIP;" % (
                    kwargs.get('dbname'),
                    kwargs.get('host'),
                    kwargs.get('port')
                )
            else:
                kwargs['dsn'] = ''
            if kwargsKeys.__contains__('port'):
                del kwargs['port']

            if not autoclobber:
                confirm = input("Wants to create %s as test database. Type yes to create it else type no" % (
                    kwargs.get('database') ))
            if autoclobber or confirm == 'yes':
                try:
                    if verbosity > 1:
                        print("Creating Test Database %s" % ( kwargs.get('database') ))
                    Database.createdb(**kwargs)
                except Exception as inst:
                    message = repr(inst)
                    if message.find('Not supported:') != -1:
                        if not autoclobber:
                            confirm = input(
                                "Not able to create test database, %s. Type yes to use %s as test database, or no to exit" % (
                                    message.split(":")[1], old_database ))
                        if autoclobber or confirm == 'yes':
                            kwargs['database'] = old_database
                            self.__clean_up(self.connection.cursor())
                            self.connection._commit()
                            self.connection.close()
                        else:
                            print("Tests cancelled")
                            sys.exit(1)
                    else:
                        sys.stderr.write("Error occurred during creation of test database: %s" % message)
                        index = message.find('SQLCODE')

                        if ( message != '' ) & ( index != -1 ):
                            error_code = message[( index + 8 ): ( index + 13 )]
                            if error_code != '-1005':
                                print("Tests cancelled")
                                sys.exit(1)
                            else:
                                if not autoclobber:
                                    confirm = input(
                                        "\nTest database: %s already exist. Type yes to recreate it, or no to exit" % (
                                            kwargs.get('database') ))
                                if autoclobber or confirm == 'yes':
                                    if verbosity > 1:
                                        print("Recreating Test Database %s" % ( kwargs.get('database') ))
                                    Database.recreatedb(**kwargs)
                                else:
                                    print("Tests cancelled.")
                                    sys.exit(1)
            else:
                confirm = input(
                    "Wants to use %s as test database, Type yes to use it as test database or no to exit" % (
                        old_database ))
                if confirm == 'yes':
                    kwargs['database'] = old_database
                    self.__clean_up(self.connection.cursor())
                    self.connection._commit()
                    self.connection.close()
                else:
                    sys.exit(1)
        else:
            self.__clean_up(self.connection.cursor())
            self.connection._commit()
            self.connection.close()

        test_database = kwargs.get('database')
        if verbosity > 1:
            print("Preparing Database...")

        if djangoVersion[0:2] <= ( 1, 1 ):
            settings.DATABASE_NAME = test_database
            settings.__setattr__('PCONNECT', False)
            call_command('syncdb', verbosity=verbosity, interactive=False)
        else:
            self.connection.settings_dict['NAME'] = test_database
            self.connection.settings_dict['PCONNECT'] = False
            # Confirm the feature set of the test database
            if (1, 2) < djangoVersion[0:2] < (1, 5):
                self.connection.features.confirm()

            command = 'syncdb' if djangoVersion[0:2] < (1, 7) else 'migrate'
            call_command(command, database=self.connection.alias, verbosity=verbosity, interactive=False,
                         load_initial_data=False)
            # We need to then do a flush to ensure that any data installed by custom SQL has been removed.
            call_command('flush', database=self.connection.alias, verbosity=verbosity, interactive=False)
        return test_database
    def create_test_db(self,
                       verbosity=0,
                       autoclobber=False,
                       keepdb=False,
                       serialize=False):
        kwargs = self.__create_test_kwargs()
        if not _IS_JYTHON:
            old_database = kwargs['database']
            max_db_name_length = self.connection.ops.max_db_name_length()
            kwargs['database'] = truncate_name(
                "%s%s" % (TEST_DBNAME_PREFIX, old_database),
                max_db_name_length)
            kwargsKeys = list(kwargs.keys())
            if (kwargsKeys.__contains__('port')
                    and kwargsKeys.__contains__('host')):
                kwargs[
                    'dsn'] = "DATABASE=%s;HOSTNAME=%s;PORT=%s;PROTOCOL=TCPIP;" % (
                        kwargs.get('dbname'), kwargs.get('host'),
                        kwargs.get('port'))
            else:
                kwargs['dsn'] = ''
            if kwargsKeys.__contains__('port'):
                del kwargs['port']

            if not autoclobber:
                confirm = input(
                    "Wants to create %s as test database. Type yes to create it else type no"
                    % (kwargs.get('database')))
            if autoclobber or confirm == 'yes':
                try:
                    if verbosity > 1:
                        print(("Creating Test Database %s" %
                               (kwargs.get('database'))))
                    Database.createdb(**kwargs)
                except Exception as inst:
                    message = repr(inst)
                    if (message.find('Not supported:') != -1):
                        if not autoclobber:
                            confirm = input(
                                "Not able to create test database, %s. Type yes to use %s as test database, or no to exit"
                                % (message.split(":")[1], old_database))
                        else:
                            confirm = input(
                                "Not able to create test database, %s. Type yes to use %s as test database, or no to exit"
                                % (message.split(":")[1], old_database))
                        if autoclobber or confirm == 'yes':
                            kwargs['database'] = old_database
                            self.__clean_up(self.connection.cursor())
                            self.connection._commit()
                            self.connection.close()
                        else:
                            print("Tests cancelled")
                            sys.exit(1)
                    else:
                        sys.stderr.write(
                            "Error occurred during creation of test database: %s"
                            % (message))
                        index = message.find('SQLCODE')

                        if (message != '') & (index != -1):
                            error_code = message[(index + 8):(index + 13)]
                            if (error_code != '-1005'):
                                print("Tests cancelled")
                                sys.exit(1)
                            else:
                                if not autoclobber:
                                    confirm = input(
                                        "\nTest database: %s already exist. Type yes to recreate it, or no to exit"
                                        % (kwargs.get('database')))
                                else:
                                    confirm = input(
                                        "\nTest database: %s already exist. Type yes to recreate it, or no to exit"
                                        % (kwargs.get('database')))
                                if autoclobber or confirm == 'yes':
                                    if verbosity > 1:
                                        print(("Recreating Test Database %s" %
                                               (kwargs.get('database'))))
                                    Database.recreatedb(**kwargs)
                                else:
                                    print("Tests cancelled.")
                                    sys.exit(1)
            else:
                confirm = input(
                    "Wants to use %s as test database, Type yes to use it as test database or no to exit"
                    % (old_database))
                if confirm == 'yes':
                    kwargs['database'] = old_database
                    self.__clean_up(self.connection.cursor())
                    self.connection._commit()
                    self.connection.close()
                else:
                    sys.exit(1)
        else:
            self.__clean_up(self.connection.cursor())
            self.connection._commit()
            self.connection.close()

        test_database = kwargs.get('database')
        if verbosity > 1:
            print("Preparing Database...")

        if (djangoVersion[0:2] <= (1, 1)):
            settings.DATABASE_NAME = test_database
            settings.__setattr__('PCONNECT', False)
            call_command('syncdb', verbosity=verbosity, interactive=False)
        else:
            self.connection.settings_dict['NAME'] = test_database
            self.connection.settings_dict['PCONNECT'] = False
            # Confirm the feature set of the test database
            if ((1, 2) < djangoVersion[0:2] < (1, 5)):
                self.connection.features.confirm()
            if (djangoVersion[0:2] < (1, 7)):
                call_command('syncdb',
                             database=self.connection.alias,
                             verbosity=verbosity,
                             interactive=False,
                             load_initial_data=False)
            else:
                if (djangoVersion[0:2] >= (2, 0)):
                    call_command('migrate',
                                 database=self.connection.alias,
                                 verbosity=verbosity,
                                 interactive=False)
                    if serialize:
                        self.connection._test_serialized_contents = self.serialize_db_to_string(
                        )
                else:
                    call_command('migrate',
                                 database=self.connection.alias,
                                 verbosity=verbosity,
                                 interactive=False,
                                 load_initial_data=False)
            # We need to then do a flush to ensure that any data installed by custom SQL has been removed.
            #call_command('flush', database=self.connection.alias, verbosity = verbosity, interactive=False)
        return test_database
Exemplo n.º 3
0
    def create_test_db(self, verbosity=0, autoclobber=False, keepdb=False, serialize=False):
        kwargs = self.__create_test_kwargs()
        if not _IS_JYTHON:
            old_database = kwargs["database"]
            max_db_name_length = self.connection.ops.max_db_name_length()
            kwargs["database"] = truncate_name("%s%s" % (TEST_DBNAME_PREFIX, old_database), max_db_name_length)
            kwargsKeys = kwargs.keys()
            if kwargsKeys.__contains__("port") and kwargsKeys.__contains__("host"):
                kwargs["dsn"] = "DATABASE=%s;HOSTNAME=%s;PORT=%s;PROTOCOL=TCPIP;" % (
                    kwargs.get("dbname"),
                    kwargs.get("host"),
                    kwargs.get("port"),
                )
            else:
                kwargs["dsn"] = ""
            if kwargsKeys.__contains__("port"):
                del kwargs["port"]

            if not autoclobber:
                confirm = raw_input(
                    "Wants to create %s as test database. Type yes to create it else type no" % (kwargs.get("database"))
                )
            if autoclobber or confirm == "yes":
                try:
                    if verbosity > 1:
                        print "Creating Test Database %s" % (kwargs.get("database"))
                    Database.createdb(**kwargs)
                except Exception, inst:
                    message = repr(inst)
                    if message.find("Not supported:") != -1:
                        if not autoclobber:
                            confirm = raw_input(
                                "Not able to create test database, %s. Type yes to use %s as test database, or no to exit"
                                % (message.split(":")[1], old_database)
                            )
                        if autoclobber or confirm == "yes":
                            kwargs["database"] = old_database
                            self.__clean_up(self.connection.cursor())
                            self.connection._commit()
                            self.connection.close()
                        else:
                            print "Tests cancelled"
                            sys.exit(1)
                    else:
                        sys.stderr.write("Error occurred during creation of test database: %s" % (message))
                        index = message.find("SQLCODE")

                        if (message != "") & (index != -1):
                            error_code = message[(index + 8) : (index + 13)]
                            if error_code != "-1005":
                                print "Tests cancelled"
                                sys.exit(1)
                            else:
                                if not autoclobber:
                                    confirm = raw_input(
                                        "\nTest database: %s already exist. Type yes to recreate it, or no to exit"
                                        % (kwargs.get("database"))
                                    )
                                if autoclobber or confirm == "yes":
                                    if verbosity > 1:
                                        print "Recreating Test Database %s" % (kwargs.get("database"))
                                    Database.recreatedb(**kwargs)
                                else:
                                    print "Tests cancelled."
                                    sys.exit(1)
            else:
                confirm = raw_input(
                    "Wants to use %s as test database, Type yes to use it as test database or no to exit"
                    % (old_database)
                )
                if confirm == "yes":
                    kwargs["database"] = old_database
                    self.__clean_up(self.connection.cursor())
                    self.connection._commit()
                    self.connection.close()
                else:
                    sys.exit(1)
Exemplo n.º 4
0
    def create_test_db(self, verbosity=0, autoclobber=False):
        kwargs = self.__create_test_kwargs()
        if not _IS_JYTHON:
            old_database = kwargs['database']
            max_db_name_length = self.connection.ops.max_db_name_length()
            kwargs['database'] = truncate_name(
                "%s%s" % (TEST_DBNAME_PREFIX, old_database),
                max_db_name_length)
            kwargsKeys = kwargs.keys()
            if (kwargsKeys.__contains__('port')
                    and kwargsKeys.__contains__('host')):
                kwargs[
                    'dsn'] = "DATABASE=%s;HOSTNAME=%s;PORT=%s;PROTOCOL=TCPIP;" % (
                        kwargs.get('dbname'), kwargs.get('host'),
                        kwargs.get('port'))
            else:
                kwargs['dsn'] = ''
            if kwargsKeys.__contains__('port'):
                del kwargs['port']

            if not autoclobber:
                confirm = raw_input(
                    "Wants to create %s as test database. Type yes to create it else type no"
                    % (kwargs.get('database')))
            if autoclobber or confirm == 'yes':
                try:
                    if verbosity > 1:
                        print "Creating Test Database %s" % (
                            kwargs.get('database'))
                    Database.createdb(**kwargs)
                except Exception, inst:
                    message = repr(inst)
                    if (message.find('Not supported:') != -1):
                        if not autoclobber:
                            confirm = raw_input(
                                "Not able to create test database, %s. Type yes to use %s as test database, or no to exit"
                                % (message.split(":")[1], old_database))
                        if autoclobber or confirm == 'yes':
                            kwargs['database'] = old_database
                            self.__clean_up(self.connection.cursor())
                            self.connection._commit()
                            self.connection.close()
                        else:
                            print "Tests cancelled"
                            sys.exit(1)
                    else:
                        sys.stderr.write(
                            "Error occurred during creation of test database: %s"
                            % (message))
                        index = message.find('SQLCODE')

                        if (message != '') & (index != -1):
                            error_code = message[(index + 8):(index + 13)]
                            if (error_code != '-1005'):
                                print "Tests cancelled"
                                sys.exit(1)
                            else:
                                if not autoclobber:
                                    confirm = raw_input(
                                        "\nTest database: %s already exist. Type yes to recreate it, or no to exit"
                                        % (kwargs.get('database')))
                                if autoclobber or confirm == 'yes':
                                    if verbosity > 1:
                                        print "Recreating Test Database %s" % (
                                            kwargs.get('database'))
                                    Database.recreatedb(**kwargs)
                                else:
                                    print "Tests cancelled."
                                    sys.exit(1)
            else:
                confirm = raw_input(
                    "Wants to use %s as test database, Type yes to use it as test database or no to exit"
                    % (old_database))
                if confirm == 'yes':
                    kwargs['database'] = old_database
                    self.__clean_up(self.connection.cursor())
                    self.connection._commit()
                    self.connection.close()
                else:
                    sys.exit(1)