1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108 |
- package instance
- import (
- "fmt"
- "git.sxidc.com/service-supports/dps-sdk/client"
- uuid "github.com/satori/go.uuid"
- "math/rand"
- "strings"
- "testing"
- "time"
- )
- func getUUID() string {
- return uuid.NewV4().String()
- }
- func simpleUUID() string {
- return strings.ReplaceAll(getUUID(), "-", "")
- }
- var tableModelDescribe = map[string]string{
- "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.AutoMigrateItem{
- {
- 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.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)
- 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)
- 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)
- return nil
- }).
- countWhere(&client.CountWhereRequest{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- Where: map[string][]any{
- "id = ?": {id},
- },
- }, &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.AutoMigrateItem{
- {
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- TableModelDescribe: tableModelDescribe,
- },
- }).
- transaction(func(tx client.Transaction) error {
- statement, err := tx.InsertBatchTx(&client.InsertBatchRequest{
- Items: []client.InsertTableRowItem{
- {
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- KeyColumns: []string{"id"},
- TableRows: []map[string]any{
- {
- "id": id1,
- "name": name1,
- "time": now1,
- "table_num": tableNum1,
- },
- {
- "id": id2,
- "name": name2,
- "time": now2,
- "table_num": tableNum2,
- },
- },
- },
- },
- })
- if err != nil {
- return err
- }
- fmt.Println(statement)
- 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.DeleteTableRowItem{
- {
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- KeyValues: []map[string]string{
- {"id": id1},
- {"id": id2},
- },
- },
- },
- UserID: "test",
- })
- if err != nil {
- return err
- }
- fmt.Println(statement)
- return nil
- }).
- countWhere(&client.CountWhereRequest{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- Where: map[string][]any{
- "id = ?": {id1},
- },
- }, &count).
- assertEqual(int64(0), count, "数量不一致").
- countWhere(&client.CountWhereRequest{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- Where: map[string][]any{
- "id = ?": {id2},
- },
- }, &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.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.AutoMigrateItem{
- {
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- TableModelDescribe: tableModelDescribe,
- },
- }).
- insertBatch(&client.InsertBatchRequest{
- Items: []client.InsertTableRowItem{
- {
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- KeyColumns: []string{"id"},
- TableRows: []map[string]any{
- {
- "id": id1,
- "name": name1,
- "time": now1,
- "table_num": tableNum1,
- },
- {
- "id": id2,
- "name": name2,
- "time": now2,
- "table_num": tableNum2,
- },
- },
- },
- },
- }).
- queryByWhereAndOrderBy(&client.QueryByWhereAndOrderByRequest{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- Where: map[string][]any{
- "id = ? AND name = ? AND table_num = ?": {id1, name1, tableNum1},
- },
- 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: map[string][]any{
- "id = ? AND name = ? AND table_num = ?": {id2, name2, tableNum2},
- },
- 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"], "表数量不一致").
- queryOnlyByWhereAndOrderBy(&client.QueryByWhereAndOrderByRequest{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- Where: map[string][]any{
- "id = ? AND name = ? AND table_num = ?": {id1, name1, tableNum1},
- },
- PageNo: 1,
- PageSize: 1,
- }, &resultsMap).
- 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"], "表数量不一致").
- commonQueryOnly(&client.CommonQueryRequest{
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- Where: map[string][]any{
- "id = ? AND name = ? AND table_num = ?": {id2, name2, tableNum2},
- },
- PageNo: 1,
- PageSize: 1,
- }, &resultsMap).
- 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.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.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: map[string][]any{
- "id = ?": {id},
- },
- }, &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.AutoMigrateItem{
- {
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- TableModelDescribe: tableModelDescribe,
- },
- }).
- insertBatch(&client.InsertBatchRequest{
- Items: []client.InsertTableRowItem{
- {
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- KeyColumns: []string{"id"},
- TableRows: []map[string]any{
- {
- "id": id1,
- "name": name1,
- "time": now1,
- "table_num": tableNum1,
- },
- {
- "id": id2,
- "name": name2,
- "time": now2,
- "table_num": tableNum2,
- },
- },
- },
- },
- }).
- deleteBatch(&client.DeleteBatchRequest{
- Items: []client.DeleteTableRowItem{
- {
- TablePrefixWithSchema: tablePrefix,
- Version: "v1",
- KeyValues: []map[string]string{
- {"id": id1},
- {"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.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.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, "值为空不一致").
- eventQueryOnlyByKeys(&client.EventQueryByKeysRequest{
- TablePrefixWithSchema: tablePrefix,
- KeyValues: []string{id},
- PageNo: 0,
- PageSize: 0,
- }, &eventInfos).
- 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, "值为空不一致").
- eventQueryOnlyByKeys(&client.EventQueryByKeysRequest{
- TablePrefixWithSchema: tablePrefix,
- KeyValues: []string{id},
- PageNo: 1,
- PageSize: 1,
- }, &eventInfos).
- 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, "值为空不一致").
- commonEventQueryOnly(&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).
- 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, "值为空不一致").
- commonEventQueryOnly(&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).
- 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, "值为空不一致").
- eventHistoryQueryOnlyByKeys(&client.EventQueryByKeysRequest{
- TablePrefixWithSchema: tablePrefix,
- KeyValues: []string{id},
- PageNo: 0,
- PageSize: 0,
- }, &eventInfos).
- 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, "值为空不一致").
- eventHistoryQueryOnlyByKeys(&client.EventQueryByKeysRequest{
- TablePrefixWithSchema: tablePrefix,
- KeyValues: []string{id},
- PageNo: 1,
- PageSize: 1,
- }, &eventInfos).
- 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, "值为空不一致").
- commonEventHistoryQueryOnly(&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).
- 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, "值为空不一致").
- commonEventHistoryQueryOnly(&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).
- 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, "值为空不一致").
- commonEventHistoryQueryOnly(&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).
- 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, "值为空不一致")
- }
|