package dpsapi import ( "fmt" "git.sxidc.com/go-tools/api_binding/http_binding" "git.sxidc.com/go-tools/api_binding/http_binding/binding_context" "git.sxidc.com/go-tools/api_binding/http_binding/response" "git.sxidc.com/service-supports/dps-sdk" "git.sxidc.com/service-supports/dps-sdk/client" "github.com/go-resty/resty/v2" "math/rand" "testing" "time" ) func TestApiV1OperateParse(t *testing.T) { http_binding.Init("test", "10086") defer http_binding.Destroy() operatorID := simpleUUID() tableName := "students" exceptedKeyColumns := []string{"id", "name", "age", "rate", "time", "is_right"} exceptedTableRows := map[string]any{ "id": "aaa", "name": "yjp", "age": float64(5), "rate": 92.5, "time": "2024-01-01T00:00:00+08:00", "is_right": false, } binding := http_binding.NewBinding("v1") ApiV1(binding, dpsAddress, func(c *binding_context.Context) (string, error) { return operatorID, nil }) parsed := operateParse(t, parseSqlInsert) if parsed["table"].(string) != tableName { t.Fatal("表名不正确") } for i, keyColumn := range parsed["key_columns"].([]any) { if exceptedKeyColumns[i] != keyColumn { t.Fatal("没有关键列数值或顺序不正确") } } for columnName, value := range parsed["table_rows"].(map[string]any) { if exceptedTableRows[columnName] != value { t.Fatal("行数据不正确") } } } func TestApiV1Operate(t *testing.T) { http_binding.Init("test", "10086") defer http_binding.Destroy() operatorID := simpleUUID() tablePrefix := "test.a" + simpleUUID()[0:7] id := simpleUUID() name := simpleUUID() now := time.Now().Local() tableNum := rand.Intn(10) + 1 autoMigrate(t, []client.AutoMigrateItem{ { TablePrefixWithSchema: tablePrefix, Version: "v1", TableModelDescribe: tableModelDescribe, }, }) binding := http_binding.NewBinding("v1") ApiV1(binding, dpsAddress, func(c *binding_context.Context) (string, error) { return operatorID, nil }) operate(t, fmt.Sprintf(sqlInsertFormat, tablePrefix, id, name, now.Format(time.DateTime), tableNum)) } func autoMigrate(t *testing.T, items []client.AutoMigrateItem) { dpsClient, err := dps.NewClient(dpsAddress, "v1", testDatabaseID) if err != nil { t.Fatal(err) } err = dpsClient.AutoMigrate(&client.AutoMigrateRequest{Items: items}) if err != nil { t.Fatal(err) } } func operateParse(t *testing.T, sql string) map[string]any { result := new(struct { response.MsgResponse Parsed map[string]any `json:"parsed"` }) resp, err := resty.New().R(). SetBody(&OperateParseRequest{ SQL: sql, }). SetResult(result). Post("http://localhost:10086/test/api/v1/dpsv1/database/operate/parse") if err != nil { t.Fatal(err) } if resp.IsError() { t.Fatal(resp.Status()) } if !result.Success { t.Fatal(result.Msg) } return result.Parsed } func operate(t *testing.T, sql string) { result := new(response.MsgResponse) resp, err := resty.New().R(). SetBody(&OperateRequest{ DatabaseID: testDatabaseID, Version: "v1", SQL: sql, }). SetResult(result). Post("http://localhost:10086/test/api/v1/dpsv1/database/operate") if err != nil { t.Fatal(err) } if resp.IsError() { t.Fatal(resp.Status()) } if !result.Success { t.Fatal(result.Msg) } }