|
|
@@ -0,0 +1,344 @@
|
|
|
+package main
|
|
|
+
|
|
|
+import (
|
|
|
+ "fmt"
|
|
|
+ "git.sxidc.com/go-tools/utils/strutils"
|
|
|
+ "git.sxidc.com/service-supports/ds-sdk/sdk"
|
|
|
+ "git.sxidc.com/service-supports/ds-sdk/sdk/raw_sql_tpl"
|
|
|
+ "math/rand"
|
|
|
+ "strconv"
|
|
|
+ "sync"
|
|
|
+ "testing"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+const (
|
|
|
+ token = "IpTTwAQweh/BP51fz5CzWKQFaXHvZe6ewvk6yOcAOkU="
|
|
|
+ address = "localhost"
|
|
|
+ httpPort = "10000"
|
|
|
+ grpcPort = "10001"
|
|
|
+ namespace = "ns-sdk-demo"
|
|
|
+ dataSource = "ds-sdk-demo"
|
|
|
+ deleteSql = "delete-sdk-demo"
|
|
|
+ goRoutineCount = 100
|
|
|
+ tableName = "test.classes"
|
|
|
+)
|
|
|
+
|
|
|
+var (
|
|
|
+ sqlSpec = sdk.SqlSpec{
|
|
|
+ Clauses: "- DELETE FROM {{ .table_name }} WHERE id = '{{ .id }}'",
|
|
|
+ }
|
|
|
+)
|
|
|
+
|
|
|
+func TestBasic(t *testing.T) {
|
|
|
+ classID := strutils.SimpleUUID()
|
|
|
+ className := strutils.SimpleUUID()
|
|
|
+ studentNum := rand.Int31n(100)
|
|
|
+
|
|
|
+ insertExecuteParams, err := raw_sql_tpl.InsertExecuteParams{
|
|
|
+ TableName: tableName,
|
|
|
+ TableRows: []raw_sql_tpl.TableRow{
|
|
|
+ {
|
|
|
+ Column: "id",
|
|
|
+ Value: "'" + classID + "'",
|
|
|
+ },
|
|
|
+ {
|
|
|
+ Column: "name",
|
|
|
+ Value: "'" + className + "'",
|
|
|
+ },
|
|
|
+ {
|
|
|
+ Column: "student_num",
|
|
|
+ Value: strconv.FormatInt(int64(studentNum), 10),
|
|
|
+ },
|
|
|
+ },
|
|
|
+ }.Map()
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ deleteExecuteParams := map[string]any{
|
|
|
+ "table_name": tableName,
|
|
|
+ "id": classID,
|
|
|
+ }
|
|
|
+
|
|
|
+ err = sdk.InitInstance(token, address, httpPort, grpcPort, namespace, dataSource)
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ defer func() {
|
|
|
+ err := sdk.DestroyInstance()
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ err = sdk.GetInstance().CreateSQL(deleteSql, sqlSpec.ToMap())
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ _, err = sdk.GetInstance().ExecuteRawSql(raw_sql_tpl.InsertTpl, insertExecuteParams)
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ _, err = sdk.GetInstance().ExecuteSql(deleteSql, deleteExecuteParams)
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ wg := sync.WaitGroup{}
|
|
|
+ wg.Add(goRoutineCount)
|
|
|
+
|
|
|
+ start := time.Now()
|
|
|
+
|
|
|
+ for i := 0; i < goRoutineCount; i++ {
|
|
|
+ go func() {
|
|
|
+ defer wg.Done()
|
|
|
+
|
|
|
+ err = sdk.GetInstance().Transaction(func(tx *sdk.Transaction) error {
|
|
|
+ err := tx.ExecuteRawSql(raw_sql_tpl.InsertTpl, insertExecuteParams)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ err = tx.ExecuteSql(deleteSql, deleteExecuteParams)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+ })
|
|
|
+ if err != nil {
|
|
|
+ panic(err)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+ }
|
|
|
+
|
|
|
+ wg.Wait()
|
|
|
+
|
|
|
+ end := time.Now()
|
|
|
+
|
|
|
+ fmt.Println(end.Sub(start).Milliseconds())
|
|
|
+}
|
|
|
+
|
|
|
+func TestRawSqlTemplate(t *testing.T) {
|
|
|
+ classID := strutils.SimpleUUID()
|
|
|
+ className := strutils.SimpleUUID()
|
|
|
+ studentNum := rand.Int31n(100)
|
|
|
+ newClassName := strutils.SimpleUUID()
|
|
|
+ newStudentNum := rand.Int31n(100)
|
|
|
+
|
|
|
+ insertExecuteParams, err := raw_sql_tpl.InsertExecuteParams{
|
|
|
+ TableName: tableName,
|
|
|
+ TableRows: []raw_sql_tpl.TableRow{
|
|
|
+ {
|
|
|
+ Column: "id",
|
|
|
+ Value: "'" + classID + "'",
|
|
|
+ },
|
|
|
+ {
|
|
|
+ Column: "name",
|
|
|
+ Value: "'" + className + "'",
|
|
|
+ },
|
|
|
+ {
|
|
|
+ Column: "student_num",
|
|
|
+ Value: strconv.FormatInt(int64(studentNum), 10),
|
|
|
+ },
|
|
|
+ },
|
|
|
+ }.Map()
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ deleteExecuteParams, err := raw_sql_tpl.DeleteExecuteParams{
|
|
|
+ TableName: tableName,
|
|
|
+ Conditions: []raw_sql_tpl.Condition{
|
|
|
+ {
|
|
|
+ Column: "id",
|
|
|
+ Value: "'" + classID + "'",
|
|
|
+ },
|
|
|
+ },
|
|
|
+ }.Map()
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ updateExecuteParams, err := raw_sql_tpl.UpdateExecuteParams{
|
|
|
+ TableName: tableName,
|
|
|
+ TableRows: []raw_sql_tpl.TableRow{
|
|
|
+ {
|
|
|
+ Column: "name",
|
|
|
+ Value: "'" + newClassName + "'",
|
|
|
+ },
|
|
|
+ {
|
|
|
+ Column: "student_num",
|
|
|
+ Value: strconv.FormatInt(int64(newStudentNum), 10),
|
|
|
+ },
|
|
|
+ },
|
|
|
+ Conditions: []raw_sql_tpl.Condition{
|
|
|
+ {
|
|
|
+ Column: "id",
|
|
|
+ Value: "'" + classID + "'",
|
|
|
+ },
|
|
|
+ },
|
|
|
+ }.Map()
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ queryExecuteParams, err := raw_sql_tpl.QueryExecuteParams{
|
|
|
+ TableName: tableName,
|
|
|
+ Conditions: []raw_sql_tpl.Condition{
|
|
|
+ {
|
|
|
+ Column: "id",
|
|
|
+ Value: "'" + classID + "'",
|
|
|
+ },
|
|
|
+ {
|
|
|
+ Column: "name",
|
|
|
+ Value: "'" + className + "'",
|
|
|
+ },
|
|
|
+ {
|
|
|
+ Column: "student_num",
|
|
|
+ Value: strconv.FormatInt(int64(studentNum), 10),
|
|
|
+ },
|
|
|
+ },
|
|
|
+ Limit: 1,
|
|
|
+ Offset: 0,
|
|
|
+ }.Map()
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ newQueryExecuteParams, err := raw_sql_tpl.QueryExecuteParams{
|
|
|
+ TableName: tableName,
|
|
|
+ Conditions: []raw_sql_tpl.Condition{
|
|
|
+ {
|
|
|
+ Column: "id",
|
|
|
+ Value: "'" + classID + "'",
|
|
|
+ },
|
|
|
+ {
|
|
|
+ Column: "name",
|
|
|
+ Value: "'" + newClassName + "'",
|
|
|
+ },
|
|
|
+ {
|
|
|
+ Column: "student_num",
|
|
|
+ Value: strconv.FormatInt(int64(newStudentNum), 10),
|
|
|
+ },
|
|
|
+ },
|
|
|
+ Limit: 0,
|
|
|
+ Offset: 0,
|
|
|
+ }.Map()
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ countExecuteParams, err := raw_sql_tpl.CountExecuteParams{
|
|
|
+ TableName: tableName,
|
|
|
+ Conditions: []raw_sql_tpl.Condition{
|
|
|
+ {
|
|
|
+ Column: "id",
|
|
|
+ Value: "'" + classID + "'",
|
|
|
+ },
|
|
|
+ {
|
|
|
+ Column: "name",
|
|
|
+ Value: "'" + className + "'",
|
|
|
+ },
|
|
|
+ {
|
|
|
+ Column: "student_num",
|
|
|
+ Value: strconv.FormatInt(int64(studentNum), 10),
|
|
|
+ },
|
|
|
+ },
|
|
|
+ }.Map()
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ newCountExecuteParams, err := raw_sql_tpl.CountExecuteParams{
|
|
|
+ TableName: tableName,
|
|
|
+ Conditions: []raw_sql_tpl.Condition{
|
|
|
+ {
|
|
|
+ Column: "id",
|
|
|
+ Value: "'" + classID + "'",
|
|
|
+ },
|
|
|
+ {
|
|
|
+ Column: "name",
|
|
|
+ Value: "'" + newClassName + "'",
|
|
|
+ },
|
|
|
+ {
|
|
|
+ Column: "student_num",
|
|
|
+ Value: strconv.FormatInt(int64(newStudentNum), 10),
|
|
|
+ },
|
|
|
+ },
|
|
|
+ }.Map()
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ err = sdk.InitInstance(token, address, httpPort, grpcPort, namespace, dataSource)
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ defer func() {
|
|
|
+ err := sdk.DestroyInstance()
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ _, err = sdk.GetInstance().ExecuteRawSql(raw_sql_tpl.InsertTpl, insertExecuteParams)
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ queryResults, err := sdk.GetInstance().ExecuteRawSql(raw_sql_tpl.QueryTpl, queryExecuteParams)
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ countResults, err := sdk.GetInstance().ExecuteRawSql(raw_sql_tpl.CountTpl, countExecuteParams)
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ if float64(len(queryResults)) != countResults[0]["count"].(float64) {
|
|
|
+ t.Fatal("总数不正确")
|
|
|
+ }
|
|
|
+
|
|
|
+ if queryResults[0]["id"].(string) != classID ||
|
|
|
+ queryResults[0]["name"].(string) != className ||
|
|
|
+ queryResults[0]["student_num"].(float64) != float64(studentNum) {
|
|
|
+ t.Fatal("查询数据不正确")
|
|
|
+ }
|
|
|
+
|
|
|
+ _, err = sdk.GetInstance().ExecuteRawSql(raw_sql_tpl.UpdateTpl, updateExecuteParams)
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ queryResults, err = sdk.GetInstance().ExecuteRawSql(raw_sql_tpl.QueryTpl, newQueryExecuteParams)
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ countResults, err = sdk.GetInstance().ExecuteRawSql(raw_sql_tpl.CountTpl, newCountExecuteParams)
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ if float64(len(queryResults)) != countResults[0]["count"].(float64) {
|
|
|
+ t.Fatal("总数不正确")
|
|
|
+ }
|
|
|
+
|
|
|
+ if queryResults[0]["id"].(string) != classID ||
|
|
|
+ queryResults[0]["name"].(string) != newClassName ||
|
|
|
+ queryResults[0]["student_num"].(float64) != float64(newStudentNum) {
|
|
|
+ t.Fatal("查询数据不正确")
|
|
|
+ }
|
|
|
+
|
|
|
+ _, err = sdk.GetInstance().ExecuteRawSql(raw_sql_tpl.DeleteTpl, deleteExecuteParams)
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+}
|