123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992 |
- package v1
- import (
- "fmt"
- "git.sxidc.com/service-supports/dps-sdk/client"
- "math/rand"
- "testing"
- "time"
- )
- var tableModelDescribe = client.TableModelDescribe{
- Fields: []client.TableModelField{
- {"ID", "gorm:\"primary_key;type:varchar(32);comment:id;\""},
- {"Name", "gorm:\"not null;type:varchar(128);comment:数据库名称;\""},
- {"Time", "gorm:\"not null;type:timestamp with time zone;comment:数据库时间;\""},
- {"TableNum", "gorm:\"not null;type:integer;comment:数据库表数量;\""},
- },
- }
- func TestAutoMigrate(t *testing.T) {
- initClient(t, "localhost:30170", "2b78141779ee432295ca371b91c5cac7")
- defer destroyClient(t, "2b78141779ee432295ca371b91c5cac7")
- newToolKit(t).
- autoMigrate(&client.AutoMigrateRequest{
- AutoMigrateItem: client.AutoMigrateItem{
- TablePrefixWithSchema: "test." + simpleUUID()[0:8],
- Version: "v1",
- TableModelDescribe: tableModelDescribe,
- },
- }).
- autoMigrateBatch(&client.AutoMigrateBatchRequest{
- Items: []client.AutoMigrateItem{
- {
- TablePrefixWithSchema: "test." + simpleUUID()[0:8],
- Version: "v1",
- TableModelDescribe: tableModelDescribe,
- },
- {
- TablePrefixWithSchema: "test." + simpleUUID()[0:8],
- Version: "v1",
- TableModelDescribe: tableModelDescribe,
- },
- },
- })
- }
- func TestTransaction(t *testing.T) {
- initClient(t, "localhost:30170", "2b78141779ee432295ca371b91c5cac7")
- defer destroyClient(t, "2b78141779ee432295ca371b91c5cac7")
- tablePrefix := "test." + simpleUUID()[0:8]
- id := simpleUUID()
- name := simpleUUID()
- now := time.Now().Local()
- tableNum := rand.New(rand.NewSource(now.Unix())).Intn(10)
- newName := simpleUUID()
- newNow := time.Now().Local()
- newTableNum := rand.New(rand.NewSource(now.Unix())).Intn(10)
- var count int64
- resultMap := make(map[string]any)
- newToolKit(t).
- autoMigrate(&client.AutoMigrateRequest{
- AutoMigrateItem: client.AutoMigrateItem{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- TableModelDescribe: tableModelDescribe,
- },
- }).
- transaction(func(tx client.Transaction) error {
- statement, err := tx.InsertTx(&client.InsertRequest{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- KeyColumns: []string{"id"},
- TableRow: map[string]any{
- "id": id,
- "name": name,
- "time": now,
- "table_num": tableNum,
- },
- UserID: "test",
- })
- if err != nil {
- return err
- }
- fmt.Println(statement)
- err = tx.End()
- if err != nil {
- return err
- }
- return nil
- }).
- queryByKeys(&client.QueryByKeysRequest{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- KeyValues: map[string]string{"id": id},
- }, &resultMap).
- assertEqual(id, resultMap["id"], "ID不一致").
- assertEqual(name, resultMap["name"], "名称不一致").
- assertEqual(now.UnixMilli(), resultMap["time"].(time.Time).UnixMilli(), "时间不一致").
- assertEqual(tableNum, resultMap["table_num"], "表数量不一致").
- transaction(func(tx client.Transaction) error {
- statement, err := tx.UpdateTx(&client.UpdateRequest{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- KeyValues: map[string]string{"id": id},
- NewTableRow: map[string]any{
- "id": id,
- "name": newName,
- "time": newNow,
- "table_num": newTableNum,
- },
- UserID: "test",
- })
- if err != nil {
- return err
- }
- fmt.Println(statement)
- err = tx.End()
- if err != nil {
- return err
- }
- return nil
- }).
- queryByKeys(&client.QueryByKeysRequest{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- KeyValues: map[string]string{"id": id},
- }, &resultMap).
- assertEqual(id, resultMap["id"], "ID不一致").
- assertEqual(newName, resultMap["name"], "名称不一致").
- assertEqual(newNow.UnixMilli(), resultMap["time"].(time.Time).UnixMilli(), "时间不一致").
- assertEqual(newTableNum, resultMap["table_num"], "表数量不一致").
- transaction(func(tx client.Transaction) error {
- statement, err := tx.UpdateTx(&client.UpdateRequest{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- KeyValues: map[string]string{"id": id},
- NewTableRow: map[string]any{
- "id": id,
- "name": name,
- "time": now,
- "table_num": tableNum,
- },
- UserID: "test",
- })
- if err != nil {
- return err
- }
- fmt.Println(statement)
- statement, err = tx.DeleteTx(&client.DeleteRequest{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- KeyValues: map[string]string{"id": id},
- UserID: "test",
- })
- if err != nil {
- return err
- }
- fmt.Println(statement)
- err = tx.End()
- if err != nil {
- return err
- }
- return nil
- }).
- countWhere(&client.CountWhereRequest{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- Where: []client.ColumnCompare{
- {
- Column: "id",
- Value: id,
- Compare: client.CompareEqual,
- },
- },
- }, &count).
- assertEqual(int64(0), count, "数量不一致")
- }
- func TestTransactionBatch(t *testing.T) {
- initClient(t, "localhost:30170", "2b78141779ee432295ca371b91c5cac7")
- defer destroyClient(t, "2b78141779ee432295ca371b91c5cac7")
- tablePrefix := "test." + simpleUUID()[0:8]
- id1 := simpleUUID()
- name1 := simpleUUID()
- now1 := time.Now().Local()
- tableNum1 := rand.New(rand.NewSource(now1.Unix())).Intn(10)
- id2 := simpleUUID()
- name2 := simpleUUID()
- now2 := time.Now().Local()
- tableNum2 := rand.New(rand.NewSource(now2.Unix())).Intn(10)
- var count int64
- resultMap := make(map[string]any)
- newToolKit(t).
- autoMigrate(&client.AutoMigrateRequest{
- AutoMigrateItem: client.AutoMigrateItem{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- TableModelDescribe: tableModelDescribe,
- },
- }).
- transaction(func(tx client.Transaction) error {
- statement, err := tx.InsertBatchTx(&client.InsertBatchRequest{
- Items: []*client.InsertTableItem{
- {
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- Items: []*client.InsertItem{
- {
- KeyColumns: []string{"id"},
- TableRow: map[string]any{
- "id": id1,
- "name": name1,
- "time": now1,
- "table_num": tableNum1,
- },
- },
- {
- KeyColumns: []string{"id"},
- TableRow: map[string]any{
- "id": id2,
- "name": name2,
- "time": now2,
- "table_num": tableNum2,
- },
- },
- },
- },
- },
- UserID: "test",
- })
- if err != nil {
- return err
- }
- fmt.Println(statement)
- err = tx.End()
- if err != nil {
- return err
- }
- return nil
- }).
- queryByKeys(&client.QueryByKeysRequest{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- KeyValues: map[string]string{"id": id1},
- }, &resultMap).
- assertEqual(id1, resultMap["id"], "ID不一致").
- assertEqual(name1, resultMap["name"], "名称不一致").
- assertEqual(now1.UnixMilli(), resultMap["time"].(time.Time).UnixMilli(), "时间不一致").
- assertEqual(tableNum1, resultMap["table_num"], "表数量不一致").
- queryByKeys(&client.QueryByKeysRequest{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- KeyValues: map[string]string{"id": id2},
- }, &resultMap).
- assertEqual(id2, resultMap["id"], "ID不一致").
- assertEqual(name2, resultMap["name"], "名称不一致").
- assertEqual(now2.UnixMilli(), resultMap["time"].(time.Time).UnixMilli(), "时间不一致").
- assertEqual(tableNum2, resultMap["table_num"], "表数量不一致").
- transaction(func(tx client.Transaction) error {
- statement, err := tx.DeleteBatchTx(&client.DeleteBatchRequest{
- Items: []*client.DeleteTableItem{
- {
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- Items: []*client.DeleteItem{
- {KeyValues: map[string]string{"id": id1}},
- {KeyValues: map[string]string{"id": id2}},
- },
- },
- },
- UserID: "test",
- })
- if err != nil {
- return err
- }
- fmt.Println(statement)
- err = tx.End()
- if err != nil {
- return err
- }
- return nil
- }).
- countWhere(&client.CountWhereRequest{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- Where: []client.ColumnCompare{
- {
- Column: "id",
- Value: id1,
- Compare: client.CompareEqual,
- },
- },
- }, &count).
- assertEqual(int64(0), count, "数量不一致").
- countWhere(&client.CountWhereRequest{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- Where: []client.ColumnCompare{
- {
- Column: "id",
- Value: id2,
- Compare: client.CompareEqual,
- },
- },
- }, &count).
- assertEqual(int64(0), count, "数量不一致")
- }
- func TestInsert(t *testing.T) {
- initClient(t, "localhost:30170", "2b78141779ee432295ca371b91c5cac7")
- defer destroyClient(t, "2b78141779ee432295ca371b91c5cac7")
- tablePrefix := "test." + simpleUUID()[0:8]
- id := simpleUUID()
- name := simpleUUID()
- now := time.Now().Local()
- tableNum := rand.New(rand.NewSource(now.Unix())).Intn(10)
- resultMap := make(map[string]any)
- newToolKit(t).
- autoMigrate(&client.AutoMigrateRequest{
- AutoMigrateItem: client.AutoMigrateItem{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- TableModelDescribe: tableModelDescribe,
- },
- }).
- insert(&client.InsertRequest{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- KeyColumns: []string{"id"},
- TableRow: map[string]any{
- "id": id,
- "name": name,
- "time": now,
- "table_num": tableNum,
- },
- UserID: "test",
- }).
- queryByKeys(&client.QueryByKeysRequest{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- KeyValues: map[string]string{"id": id},
- }, &resultMap).
- assertEqual(id, resultMap["id"], "ID不一致").
- assertEqual(name, resultMap["name"], "名称不一致").
- assertEqual(now.UnixMilli(), resultMap["time"].(time.Time).Local().UnixMilli(), "时间不一致").
- assertEqual(tableNum, resultMap["table_num"], "表数量不一致")
- }
- func TestInsertBatch(t *testing.T) {
- initClient(t, "localhost:30170", "2b78141779ee432295ca371b91c5cac7")
- defer destroyClient(t, "2b78141779ee432295ca371b91c5cac7")
- tablePrefix := "test." + simpleUUID()[0:8]
- id1 := simpleUUID()
- name1 := simpleUUID()
- now1 := time.Now().Local()
- tableNum1 := rand.New(rand.NewSource(now1.Unix())).Intn(10)
- id2 := simpleUUID()
- name2 := simpleUUID()
- now2 := time.Now().Local()
- tableNum2 := rand.New(rand.NewSource(now2.Unix())).Intn(10)
- resultsMap := make([]map[string]any, 0)
- var totalCount int64
- newToolKit(t).
- autoMigrate(&client.AutoMigrateRequest{
- AutoMigrateItem: client.AutoMigrateItem{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- TableModelDescribe: tableModelDescribe,
- },
- }).
- insertBatch(&client.InsertBatchRequest{
- Items: []*client.InsertTableItem{
- {
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- Items: []*client.InsertItem{
- {
- KeyColumns: []string{"id"},
- TableRow: map[string]any{
- "id": id1,
- "name": name1,
- "time": now1,
- "table_num": tableNum1,
- },
- },
- {
- KeyColumns: []string{"id"},
- TableRow: map[string]any{
- "id": id2,
- "name": name2,
- "time": now2,
- "table_num": tableNum2,
- },
- },
- },
- },
- },
- UserID: "test",
- }).
- queryByWhereAndOrderBy(&client.QueryByWhereAndOrderByRequest{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- Where: []client.ColumnCompare{
- {Column: "id", Value: id1, Compare: client.CompareEqual},
- {Column: "name", Value: name1, Compare: client.CompareEqual},
- {Column: "table_num", Value: tableNum1, Compare: client.CompareEqual},
- },
- PageNo: 1,
- PageSize: 1,
- }, &resultsMap, &totalCount).
- assertEqual(1, int(totalCount), "总数不一致").
- assertEqual(id1, resultsMap[0]["id"], "ID不一致").
- assertEqual(name1, resultsMap[0]["name"], "名称不一致").
- assertEqual(now1.UnixMilli(), resultsMap[0]["time"].(time.Time).Local().UnixMilli(), "时间不一致").
- assertEqual(tableNum1, resultsMap[0]["table_num"], "表数量不一致").
- commonQuery(&client.CommonQueryRequest{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- Where: []client.ColumnCompare{
- {Column: "id", Value: id2, Compare: client.CompareEqual},
- {Column: "name", Value: name2, Compare: client.CompareEqual},
- {Column: "table_num", Value: tableNum2, Compare: client.CompareEqual},
- },
- PageNo: 1,
- PageSize: 1,
- }, &resultsMap, &totalCount).
- assertEqual(1, int(totalCount), "总数不一致").
- assertEqual(id2, resultsMap[0]["id"], "ID不一致").
- assertEqual(name2, resultsMap[0]["name"], "名称不一致").
- assertEqual(now2.UnixMilli(), resultsMap[0]["time"].(time.Time).Local().UnixMilli(), "时间不一致").
- assertEqual(tableNum2, resultsMap[0]["table_num"], "表数量不一致")
- }
- func TestUpdate(t *testing.T) {
- initClient(t, "localhost:30170", "2b78141779ee432295ca371b91c5cac7")
- defer destroyClient(t, "2b78141779ee432295ca371b91c5cac7")
- tablePrefix := "test." + simpleUUID()[0:8]
- id := simpleUUID()
- name := simpleUUID()
- now := time.Now().Local()
- tableNum := rand.New(rand.NewSource(now.Unix())).Intn(10)
- newName := simpleUUID()
- newNow := time.Now().Local()
- newTableNum := rand.New(rand.NewSource(now.Unix())).Intn(10)
- resultMap := make(map[string]any)
- newToolKit(t).
- autoMigrate(&client.AutoMigrateRequest{
- AutoMigrateItem: client.AutoMigrateItem{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- TableModelDescribe: tableModelDescribe,
- },
- }).
- insert(&client.InsertRequest{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- KeyColumns: []string{"id"},
- TableRow: map[string]any{
- "id": id,
- "name": name,
- "time": now,
- "table_num": tableNum,
- },
- UserID: "test",
- }).
- update(&client.UpdateRequest{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- KeyValues: map[string]string{"id": id},
- NewTableRow: map[string]any{
- "id": id,
- "name": newName,
- "time": newNow,
- "table_num": newTableNum,
- },
- UserID: "test",
- }).
- queryByKeys(&client.QueryByKeysRequest{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- KeyValues: map[string]string{"id": id},
- }, &resultMap).
- assertEqual(id, resultMap["id"], "ID不一致").
- assertEqual(newName, resultMap["name"], "名称不一致").
- assertEqual(newNow.UnixMilli(), resultMap["time"].(time.Time).Local().UnixMilli(), "时间不一致").
- assertEqual(newTableNum, resultMap["table_num"], "表数量不一致")
- }
- func TestDelete(t *testing.T) {
- initClient(t, "localhost:30170", "2b78141779ee432295ca371b91c5cac7")
- defer destroyClient(t, "2b78141779ee432295ca371b91c5cac7")
- tablePrefix := "test." + simpleUUID()[0:8]
- id := simpleUUID()
- name := simpleUUID()
- now := time.Now().Local()
- tableNum := rand.New(rand.NewSource(now.Unix())).Intn(10)
- var count int64
- newToolKit(t).
- autoMigrate(&client.AutoMigrateRequest{
- AutoMigrateItem: client.AutoMigrateItem{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- TableModelDescribe: tableModelDescribe,
- },
- }).
- insert(&client.InsertRequest{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- KeyColumns: []string{"id"},
- TableRow: map[string]any{
- "id": id,
- "name": name,
- "time": now,
- "table_num": tableNum,
- },
- UserID: "test",
- }).
- delete(&client.DeleteRequest{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- KeyValues: map[string]string{"id": id},
- UserID: "test",
- }).
- countWhere(&client.CountWhereRequest{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- Where: []client.ColumnCompare{
- {
- Column: "id",
- Value: id,
- Compare: client.CompareEqual,
- },
- },
- }, &count).
- assertEqual(int64(0), count, "数量不一致")
- }
- func TestDeleteBatch(t *testing.T) {
- initClient(t, "localhost:30170", "2b78141779ee432295ca371b91c5cac7")
- defer destroyClient(t, "2b78141779ee432295ca371b91c5cac7")
- tablePrefix := "test." + simpleUUID()[0:8]
- id1 := simpleUUID()
- name1 := simpleUUID()
- now1 := time.Now().Local()
- tableNum1 := rand.New(rand.NewSource(now1.Unix())).Intn(10)
- id2 := simpleUUID()
- name2 := simpleUUID()
- now2 := time.Now().Local()
- tableNum2 := rand.New(rand.NewSource(now2.Unix())).Intn(10)
- var count int64
- newToolKit(t).
- autoMigrate(&client.AutoMigrateRequest{
- AutoMigrateItem: client.AutoMigrateItem{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- TableModelDescribe: tableModelDescribe,
- },
- }).
- insertBatch(&client.InsertBatchRequest{
- Items: []*client.InsertTableItem{
- {
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- Items: []*client.InsertItem{
- {
- KeyColumns: []string{"id"},
- TableRow: map[string]any{
- "id": id1,
- "name": name1,
- "time": now1,
- "table_num": tableNum1,
- },
- },
- {
- KeyColumns: []string{"id"},
- TableRow: map[string]any{
- "id": id2,
- "name": name2,
- "time": now2,
- "table_num": tableNum2,
- },
- },
- },
- },
- },
- UserID: "test",
- }).
- deleteBatch(&client.DeleteBatchRequest{
- Items: []*client.DeleteTableItem{
- {
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- Items: []*client.DeleteItem{
- {KeyValues: map[string]string{"id": id1}},
- {KeyValues: map[string]string{"id": id2}},
- },
- },
- },
- UserID: "test",
- }).
- commonCount(&client.CommonCountRequest{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- }, &count).
- assertEqual(int64(0), count, "数量不一致")
- }
- func TestReply(t *testing.T) {
- initClient(t, "localhost:30170", "2b78141779ee432295ca371b91c5cac7")
- defer destroyClient(t, "2b78141779ee432295ca371b91c5cac7")
- tablePrefix := "test." + simpleUUID()[0:8]
- id := simpleUUID()
- name := simpleUUID()
- now := time.Now().Local()
- tableNum := rand.New(rand.NewSource(now.Unix())).Intn(10)
- resultMap := make(map[string]any)
- newToolKit(t).
- autoMigrate(&client.AutoMigrateRequest{
- AutoMigrateItem: client.AutoMigrateItem{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- TableModelDescribe: tableModelDescribe,
- },
- }).
- insert(&client.InsertRequest{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- KeyColumns: []string{"id"},
- TableRow: map[string]any{
- "id": id,
- "name": name,
- "time": now,
- "table_num": tableNum,
- },
- UserID: "test",
- }).
- reply(&client.ReplayRequest{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- KeyValues: map[string]string{"id": id},
- UserID: "test",
- }).
- queryByKeys(&client.QueryByKeysRequest{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- KeyValues: map[string]string{"id": id},
- }, &resultMap).
- assertEqual(id, resultMap["id"], "ID不一致").
- assertEqual(name, resultMap["name"], "名称不一致").
- assertEqual(now.UnixMilli(), resultMap["time"].(time.Time).Local().UnixMilli(), "时间不一致").
- assertEqual(tableNum, resultMap["table_num"], "表数量不一致")
- }
- func TestEventQuery(t *testing.T) {
- initClient(t, "localhost:30170", "2b78141779ee432295ca371b91c5cac7")
- defer destroyClient(t, "2b78141779ee432295ca371b91c5cac7")
- tablePrefix := "test." + simpleUUID()[0:8]
- id := simpleUUID()
- name := simpleUUID()
- now := time.Now().Local()
- tableNum := rand.New(rand.NewSource(now.Unix())).Intn(10)
- newName := simpleUUID()
- newNow := time.Now().Local()
- newTableNum := rand.New(rand.NewSource(now.Unix())).Intn(10)
- var totalCount int64
- eventInfos := make([]client.EventInfo, 0)
- newToolKit(t).
- autoMigrate(&client.AutoMigrateRequest{
- AutoMigrateItem: client.AutoMigrateItem{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- TableModelDescribe: tableModelDescribe,
- },
- }).
- insert(&client.InsertRequest{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- KeyColumns: []string{"id"},
- TableRow: map[string]any{
- "id": id,
- "name": name,
- "time": now,
- "table_num": tableNum,
- },
- UserID: "test",
- }).
- update(&client.UpdateRequest{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- KeyValues: map[string]string{"id": id},
- NewTableRow: map[string]any{
- "id": id,
- "name": newName,
- "time": newNow,
- "table_num": newTableNum,
- },
- UserID: "test",
- }).
- countEventByKeys(&client.CountEventByKeysRequest{
- TablePrefixWithSchema: tablePrefix,
- KeyValues: []string{id},
- }, &totalCount).
- assertEqual(2, int(totalCount), "总数不一致").
- commonCountEvent(&client.CommonCountEventRequest{
- TablePrefixWithSchema: tablePrefix,
- KeyValues: []string{id},
- Version: "v1",
- Operation: "create",
- CreatorID: "test",
- StartCreatedTime: now.Format(time.DateTime),
- EndCreatedTime: now.Add(time.Second).Format(time.DateTime),
- }, &totalCount).
- assertEqual(1, int(totalCount), "总数不一致").
- commonCountEvent(&client.CommonCountEventRequest{
- TablePrefixWithSchema: tablePrefix,
- KeyValues: []string{id},
- Version: "v1",
- Operation: "update",
- CreatorID: "test",
- StartCreatedTime: now.Format(time.DateTime),
- EndCreatedTime: now.Add(time.Second).Format(time.DateTime),
- }, &totalCount).
- assertEqual(1, int(totalCount), "总数不一致").
- eventQueryByKeys(&client.EventQueryByKeysRequest{
- TablePrefixWithSchema: tablePrefix,
- KeyValues: []string{id},
- PageNo: 0,
- PageSize: 0,
- }, &eventInfos, &totalCount).
- assertEqual(2, int(totalCount), "总数不一致").
- assertEqual(2, len(eventInfos), "事件数量不一致").
- assertEqual(id, eventInfos[0].Key, "关键字段不一致").
- assertEqual("v1", eventInfos[0].Version, "版本不一致").
- assertEqual("create", eventInfos[0].Operation, "操作不一致").
- assertEqual("test", eventInfos[0].CreatorID, "创建者ID不一致").
- assertNotEmpty(eventInfos[0].CreateTime, "创建事件为空").
- assertNotEmpty(eventInfos[0].Value, "值为空不一致").
- assertEqual(id, eventInfos[1].Key, "关键字段不一致").
- assertEqual("v1", eventInfos[1].Version, "版本不一致").
- assertEqual("update", eventInfos[1].Operation, "操作不一致").
- assertEqual("test", eventInfos[1].CreatorID, "创建者ID不一致").
- assertNotEmpty(eventInfos[1].CreateTime, "创建事件为空").
- assertNotEmpty(eventInfos[1].Value, "值为空不一致").
- eventQueryByKeys(&client.EventQueryByKeysRequest{
- TablePrefixWithSchema: tablePrefix,
- KeyValues: []string{id},
- PageNo: 1,
- PageSize: 1,
- }, &eventInfos, &totalCount).
- assertEqual(2, int(totalCount), "总数不一致").
- assertEqual(1, len(eventInfos), "事件数量不一致").
- assertEqual(id, eventInfos[0].Key, "关键字段不一致").
- assertEqual("v1", eventInfos[0].Version, "版本不一致").
- assertEqual("create", eventInfos[0].Operation, "操作不一致").
- assertEqual("test", eventInfos[0].CreatorID, "创建者ID不一致").
- assertNotEmpty(eventInfos[0].CreateTime, "创建事件为空").
- assertNotEmpty(eventInfos[0].Value, "值为空不一致").
- commonEventQuery(&client.CommonEventQueryRequest{
- TablePrefixWithSchema: tablePrefix,
- KeyValues: []string{id},
- Version: "v1",
- Operation: "create",
- CreatorID: "test",
- StartCreatedTime: now.Format(time.DateTime),
- EndCreatedTime: now.Add(time.Second).Format(time.DateTime),
- PageNo: 0,
- PageSize: 0,
- }, &eventInfos, &totalCount).
- assertEqual(1, int(totalCount), "总数不一致").
- assertEqual(1, len(eventInfos), "事件数量不一致").
- assertEqual(id, eventInfos[0].Key, "关键字段不一致").
- assertEqual("v1", eventInfos[0].Version, "版本不一致").
- assertEqual("create", eventInfos[0].Operation, "操作不一致").
- assertEqual("test", eventInfos[0].CreatorID, "创建者ID不一致").
- assertNotEmpty(eventInfos[0].CreateTime, "创建事件为空").
- assertNotEmpty(eventInfos[0].Value, "值为空不一致").
- commonEventQuery(&client.CommonEventQueryRequest{
- TablePrefixWithSchema: tablePrefix,
- KeyValues: []string{id},
- Version: "v1",
- Operation: "update",
- CreatorID: "test",
- StartCreatedTime: now.Format(time.DateTime),
- EndCreatedTime: now.Add(time.Second).Format(time.DateTime),
- PageNo: 0,
- PageSize: 0,
- }, &eventInfos, &totalCount).
- assertEqual(1, int(totalCount), "总数不一致").
- assertEqual(1, len(eventInfos), "事件数量不一致").
- assertEqual(id, eventInfos[0].Key, "关键字段不一致").
- assertEqual("v1", eventInfos[0].Version, "版本不一致").
- assertEqual("update", eventInfos[0].Operation, "操作不一致").
- assertEqual("test", eventInfos[0].CreatorID, "创建者ID不一致").
- assertNotEmpty(eventInfos[0].CreateTime, "创建事件为空").
- assertNotEmpty(eventInfos[0].Value, "值为空不一致").
- delete(&client.DeleteRequest{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- KeyValues: map[string]string{"id": id},
- UserID: "test",
- }).
- countEventHistoryByKeys(&client.CountEventByKeysRequest{
- TablePrefixWithSchema: tablePrefix,
- KeyValues: []string{id},
- }, &totalCount).
- assertEqual(3, int(totalCount), "总数不一致").
- commonCountEventHistory(&client.CommonCountEventRequest{
- TablePrefixWithSchema: tablePrefix,
- KeyValues: []string{id},
- Version: "v1",
- Operation: "create",
- CreatorID: "test",
- StartCreatedTime: now.Format(time.DateTime),
- EndCreatedTime: now.Add(time.Second).Format(time.DateTime),
- }, &totalCount).
- assertEqual(1, int(totalCount), "总数不一致").
- commonCountEventHistory(&client.CommonCountEventRequest{
- TablePrefixWithSchema: tablePrefix,
- KeyValues: []string{id},
- Version: "v1",
- Operation: "update",
- CreatorID: "test",
- StartCreatedTime: now.Format(time.DateTime),
- EndCreatedTime: now.Add(time.Second).Format(time.DateTime),
- }, &totalCount).
- assertEqual(1, int(totalCount), "总数不一致").
- commonCountEventHistory(&client.CommonCountEventRequest{
- TablePrefixWithSchema: tablePrefix,
- KeyValues: []string{id},
- Version: "v1",
- Operation: "delete",
- CreatorID: "test",
- StartCreatedTime: now.Format(time.DateTime),
- EndCreatedTime: now.Add(time.Second).Format(time.DateTime),
- }, &totalCount).
- assertEqual(1, int(totalCount), "总数不一致").
- eventHistoryQueryByKeys(&client.EventQueryByKeysRequest{
- TablePrefixWithSchema: tablePrefix,
- KeyValues: []string{id},
- PageNo: 0,
- PageSize: 0,
- }, &eventInfos, &totalCount).
- assertEqual(3, int(totalCount), "总数不一致").
- assertEqual(3, len(eventInfos), "事件数量不一致").
- assertEqual(id, eventInfos[0].Key, "关键字段不一致").
- assertEqual("v1", eventInfos[0].Version, "版本不一致").
- assertEqual("create", eventInfos[0].Operation, "操作不一致").
- assertEqual("test", eventInfos[0].CreatorID, "创建者ID不一致").
- assertNotEmpty(eventInfos[0].CreateTime, "创建事件为空").
- assertNotEmpty(eventInfos[0].Value, "值为空不一致").
- assertEqual(id, eventInfos[1].Key, "关键字段不一致").
- assertEqual("v1", eventInfos[1].Version, "版本不一致").
- assertEqual("update", eventInfos[1].Operation, "操作不一致").
- assertEqual("test", eventInfos[1].CreatorID, "创建者ID不一致").
- assertNotEmpty(eventInfos[1].CreateTime, "创建事件为空").
- assertNotEmpty(eventInfos[1].Value, "值为空不一致").
- assertEqual(id, eventInfos[2].Key, "关键字段不一致").
- assertEqual("v1", eventInfos[2].Version, "版本不一致").
- assertEqual("delete", eventInfos[2].Operation, "操作不一致").
- assertEqual("test", eventInfos[2].CreatorID, "创建者ID不一致").
- assertNotEmpty(eventInfos[2].CreateTime, "创建事件为空").
- assertEqual("", eventInfos[2].Value, "值为空不一致").
- eventHistoryQueryByKeys(&client.EventQueryByKeysRequest{
- TablePrefixWithSchema: tablePrefix,
- KeyValues: []string{id},
- PageNo: 1,
- PageSize: 1,
- }, &eventInfos, &totalCount).
- assertEqual(3, int(totalCount), "总数不一致").
- assertEqual(1, len(eventInfos), "事件数量不一致").
- assertEqual(id, eventInfos[0].Key, "关键字段不一致").
- assertEqual("v1", eventInfos[0].Version, "版本不一致").
- assertEqual("create", eventInfos[0].Operation, "操作不一致").
- assertEqual("test", eventInfos[0].CreatorID, "创建者ID不一致").
- assertNotEmpty(eventInfos[0].CreateTime, "创建事件为空").
- assertNotEmpty(eventInfos[0].Value, "值为空不一致").
- commonEventHistoryQuery(&client.CommonEventQueryRequest{
- TablePrefixWithSchema: tablePrefix,
- KeyValues: []string{id},
- Version: "v1",
- Operation: "create",
- CreatorID: "test",
- StartCreatedTime: now.Format(time.DateTime),
- EndCreatedTime: now.Add(time.Second).Format(time.DateTime),
- PageNo: 0,
- PageSize: 0,
- }, &eventInfos, &totalCount).
- assertEqual(1, int(totalCount), "总数不一致").
- assertEqual(1, len(eventInfos), "事件数量不一致").
- assertEqual(id, eventInfos[0].Key, "关键字段不一致").
- assertEqual("v1", eventInfos[0].Version, "版本不一致").
- assertEqual("create", eventInfos[0].Operation, "操作不一致").
- assertEqual("test", eventInfos[0].CreatorID, "创建者ID不一致").
- assertNotEmpty(eventInfos[0].CreateTime, "创建事件为空").
- assertNotEmpty(eventInfos[0].Value, "值为空不一致").
- commonEventHistoryQuery(&client.CommonEventQueryRequest{
- TablePrefixWithSchema: tablePrefix,
- KeyValues: []string{id},
- Version: "v1",
- Operation: "update",
- CreatorID: "test",
- StartCreatedTime: now.Format(time.DateTime),
- EndCreatedTime: now.Add(time.Second).Format(time.DateTime),
- PageNo: 0,
- PageSize: 0,
- }, &eventInfos, &totalCount).
- assertEqual(1, int(totalCount), "总数不一致").
- assertEqual(1, len(eventInfos), "事件数量不一致").
- assertEqual(id, eventInfos[0].Key, "关键字段不一致").
- assertEqual("v1", eventInfos[0].Version, "版本不一致").
- assertEqual("update", eventInfos[0].Operation, "操作不一致").
- assertEqual("test", eventInfos[0].CreatorID, "创建者ID不一致").
- assertNotEmpty(eventInfos[0].CreateTime, "创建事件为空").
- assertNotEmpty(eventInfos[0].Value, "值为空不一致").
- commonEventHistoryQuery(&client.CommonEventQueryRequest{
- TablePrefixWithSchema: tablePrefix,
- KeyValues: []string{id},
- Version: "v1",
- Operation: "delete",
- CreatorID: "test",
- StartCreatedTime: now.Format(time.DateTime),
- EndCreatedTime: now.Add(time.Second).Format(time.DateTime),
- PageNo: 0,
- PageSize: 0,
- }, &eventInfos, &totalCount).
- assertEqual(1, int(totalCount), "总数不一致").
- assertEqual(1, len(eventInfos), "事件数量不一致").
- assertEqual(id, eventInfos[0].Key, "关键字段不一致").
- assertEqual("v1", eventInfos[0].Version, "版本不一致").
- assertEqual("delete", eventInfos[0].Operation, "操作不一致").
- assertEqual("test", eventInfos[0].CreatorID, "创建者ID不一致").
- assertNotEmpty(eventInfos[0].CreateTime, "创建事件为空").
- assertEqual("", eventInfos[0].Value, "值为空不一致")
- }
|