164 lines
3.9 KiB
Go
164 lines
3.9 KiB
Go
// Package api2sql ...
|
|
//
|
|
// Description : api2sql ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 2024-08-23 17:36
|
|
package api2sql
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"git.zhangdeman.cn/zhangdeman/consts"
|
|
"git.zhangdeman.cn/zhangdeman/database"
|
|
"git.zhangdeman.cn/zhangdeman/database/define"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func startTest() {
|
|
clientManager := database.NewWrapperClient()
|
|
if err := clientManager.AddWithConfig("TEST_DATABASE", nil, &define.Database{
|
|
Master: &define.Driver{
|
|
DBType: "sqlite3",
|
|
Host: "/tmp/gateway.db",
|
|
},
|
|
Slave: &define.Driver{
|
|
DBType: "sqlite3",
|
|
Host: "/tmp/gateway.db",
|
|
},
|
|
}, []string{}); nil != err {
|
|
panic(err.Error())
|
|
}
|
|
dbClient, _ := clientManager.GetDBClient("TEST_DATABASE")
|
|
|
|
dbClient.SetTableStructure(map[string][]*define.ColumnConfig{
|
|
"project": []*define.ColumnConfig{
|
|
&define.ColumnConfig{
|
|
Column: "flag",
|
|
Alias: "project_flag",
|
|
Type: "string",
|
|
},
|
|
},
|
|
})
|
|
Exec.SetDatabaseClientManager(clientManager)
|
|
}
|
|
|
|
func Test_execute_List(t *testing.T) {
|
|
startTest()
|
|
res, err := Exec.List(context.Background(), &define.Api2SqlParam{
|
|
DatabaseFlag: "TEST_DATABASE",
|
|
Table: "project",
|
|
ForceMaster: false,
|
|
InputSql: "",
|
|
TableSplitConfig: define.TableSplitConfig{},
|
|
SqlType: consts.SqlTypeList,
|
|
ColumnList: []*define.ColumnConfig{
|
|
&define.ColumnConfig{
|
|
Column: "flag",
|
|
Alias: "project_flag",
|
|
Type: "string",
|
|
},
|
|
&define.ColumnConfig{
|
|
Column: "connect_timeout",
|
|
Alias: "timeout",
|
|
Type: "string",
|
|
},
|
|
},
|
|
Limit: 0,
|
|
Offset: 0,
|
|
ForceNoLimit: false,
|
|
OrderField: "id",
|
|
OrderRule: "desc",
|
|
WithCount: false,
|
|
ConditionList: nil,
|
|
TableColumnConfig: nil,
|
|
Tx: nil,
|
|
})
|
|
byteData, _ := json.Marshal(res)
|
|
fmt.Println(string(byteData), err)
|
|
}
|
|
|
|
func Test_execute_Detail(t *testing.T) {
|
|
startTest()
|
|
res, err := Exec.Detail(context.Background(), &define.Api2SqlParam{
|
|
DatabaseFlag: "TEST_DATABASE",
|
|
Table: "project",
|
|
ForceMaster: false,
|
|
InputSql: "",
|
|
TableSplitConfig: define.TableSplitConfig{},
|
|
SqlType: consts.SqlTypeList,
|
|
ColumnList: []*define.ColumnConfig{
|
|
&define.ColumnConfig{
|
|
Column: "flag",
|
|
Alias: "project_flag",
|
|
Type: "string",
|
|
},
|
|
&define.ColumnConfig{
|
|
Column: "description",
|
|
Alias: "desc",
|
|
Type: "string",
|
|
},
|
|
},
|
|
Limit: 0,
|
|
Offset: 0,
|
|
ForceNoLimit: false,
|
|
OrderField: "id",
|
|
OrderRule: "desc",
|
|
WithCount: false,
|
|
ConditionList: nil,
|
|
TableColumnConfig: nil,
|
|
Tx: nil,
|
|
})
|
|
byteData, _ := json.Marshal(res)
|
|
fmt.Println(res, err, string(byteData))
|
|
}
|
|
|
|
func Test_execute_Insert(t *testing.T) {
|
|
startTest()
|
|
res, err := Exec.Insert(context.Background(), &define.Api2SqlParam{
|
|
DatabaseFlag: "TEST_DATABASE",
|
|
Table: "project",
|
|
ForceMaster: false,
|
|
InputSql: "",
|
|
TableSplitConfig: define.TableSplitConfig{},
|
|
SqlType: consts.SqlTypeList,
|
|
ColumnList: []*define.ColumnConfig{
|
|
&define.ColumnConfig{
|
|
Column: "flag",
|
|
Alias: "project_flag",
|
|
Type: "string",
|
|
},
|
|
&define.ColumnConfig{
|
|
Column: "description",
|
|
Alias: "desc",
|
|
Type: "string",
|
|
},
|
|
},
|
|
Limit: 0,
|
|
Offset: 0,
|
|
ForceNoLimit: false,
|
|
OrderField: "id",
|
|
OrderRule: "desc",
|
|
WithCount: false,
|
|
ConditionList: []define.SqlCondition{
|
|
define.SqlCondition{
|
|
Column: "flag",
|
|
Operate: "",
|
|
Value: fmt.Sprintf("unit_test_flag_%v", time.Now().Unix()),
|
|
},
|
|
define.SqlCondition{
|
|
Column: "description",
|
|
Operate: "",
|
|
Value: "单元测试生成项目",
|
|
},
|
|
},
|
|
TableColumnConfig: nil,
|
|
Tx: nil,
|
|
})
|
|
byteData, _ := json.Marshal(res)
|
|
fmt.Println(res, err, string(byteData))
|
|
}
|