Пример #1
0
def load_test_data(server, db_num=1):
    """Load/insert data into the test databases.

    A considerable amount of data should be considered in order to take some
    time to load, allowing mysqlrplsync to be executed at the same time the
    data is still being inserted.

    server[in]      Target server to load the test data.
    db_num[in]      Number of databases to load the data (by default: 1).
                    It is assumed that a matching number of test databases
                    have been previously created.

    Note: method prepared to be invoked by a different thread.
    """
    # Create a new server instance with a new connection (for multithreading).
    srv = Server({'conn_info': server})
    srv.connect()

    for db_index in xrange(db_num):
        db_name = '`test_rplsync_db{0}`'.format(
            '' if db_num == 1 else db_index
        )
        # Insert random data on all tables.
        random_values = string.letters + string.digits
        for _ in xrange(TEST_DB_NUM_ROWS):
            columns = []
            values = []
            for table_index in xrange(TEST_DB_NUM_TABLES):
                columns.append('rnd_txt{0}'.format(table_index))
                rnd_text = "".join(
                    [random.choice(random_values) for _ in xrange(20)]
                )
                values.append("'{0}'".format(rnd_text))
                insert = ("INSERT INTO {0}.`t{1}` ({2}) VALUES ({3})"
                          "").format(db_name, table_index, ', '.join(columns),
                                     ', '.join(values))
                srv.exec_query(insert)
                srv.commit()