github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/dbsync/dbsync_test.go (about)

     1  package dbsync
     2  
     3  import (
     4  	"database/sql"
     5  	"fmt"
     6  	"testing"
     7  
     8  	_ "github.com/go-sql-driver/mysql"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestSync(t *testing.T) {
    13  	// create database db_test;
    14  	// create table t_bucket (pk varchar(100) primary key, v int not null);
    15  	// insert into t_bucket(pk, v) values('aa', 1);
    16  	// insert into t_bucket(pk, v) values('bb', 1);
    17  	// update t_bucket set v = v+1 where pk = 'aa';
    18  	// update t_bucket set v = v+1 where pk = 'bb';
    19  	// delete from t_bucket where pk = 'aa';
    20  	// delete from t_bucket where pk = 'bb';
    21  	dsn := "root:root@tcp(127.0.0.1:3306)/db_test?charset=utf8mb4&parseTime=True&loc=Local"
    22  	db, err := sql.Open("mysql", dsn)
    23  	assert.Nil(t, err)
    24  	dbsync := NewDbSync(db, "t_bucket", WithPk("pk"), WithV("v"), WithDuration("10s"),
    25  		WithNotify(func(event Event, id, v string) {
    26  			fmt.Printf("event:%s pk: %s, v:%s\n", event, id, v)
    27  		}))
    28  
    29  	dbsync.Start()
    30  
    31  	// time.Sleep(1 * time.Hour)
    32  	dbsync.Stop()
    33  }