| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- package dpsapi
- import (
- "git.sxidc.com/go-tools/api_binding/http_binding"
- "git.sxidc.com/go-tools/api_binding/http_binding/response"
- "github.com/go-resty/resty/v2"
- "testing"
- )
- func TestApiV1OperateParse(t *testing.T) {
- http_binding.Init("test", "10086")
- defer http_binding.Destroy()
- 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, "localhost:30170")
- parsed := operateParse(t, parseSqlSelect)
- 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()
- binding := http_binding.NewBinding("v1")
- ApiV1(binding, "localhost:30170")
- operate(t, sqlSelect)
- }
- 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: "ee2d7dabe56646ce835d80873348ee0e",
- 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)
- }
- }
|