database/api2sql/execute_test.go

164 lines
3.9 KiB
Go
Raw Normal View History

2024-08-23 17:54:48 +08:00
// Package api2sql ...
//
// Description : api2sql ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-08-23 17:36
package api2sql
import (
2024-08-23 18:17:31 +08:00
"context"
"encoding/json"
2024-08-23 18:17:31 +08:00
"fmt"
"git.zhangdeman.cn/zhangdeman/consts"
2024-08-23 17:54:48 +08:00
"git.zhangdeman.cn/zhangdeman/database"
"git.zhangdeman.cn/zhangdeman/database/define"
"testing"
2024-09-06 14:49:36 +08:00
"time"
2024-08-23 17:54:48 +08:00
)
2024-08-30 14:50:51 +08:00
func startTest() {
2024-08-23 17:54:48 +08:00
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())
}
2024-08-23 18:17:31 +08:00
dbClient, _ := clientManager.GetDBClient("TEST_DATABASE")
dbClient.SetTableStructure(map[string][]*define.ColumnConfig{
"project": []*define.ColumnConfig{
&define.ColumnConfig{
Column: "flag",
Alias: "project_flag",
Type: "string",
},
},
})
2024-08-23 17:54:48 +08:00
Exec.SetDatabaseClientManager(clientManager)
2024-08-30 14:50:51 +08:00
}
func Test_execute_List(t *testing.T) {
startTest()
2024-08-23 18:17:31 +08:00
res, err := Exec.List(context.Background(), &define.Api2SqlParam{
2024-08-23 18:23:37 +08:00
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",
},
2024-08-30 14:50:51 +08:00
&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)
2024-09-06 14:49:36 +08:00
fmt.Println(string(byteData), err)
2024-08-30 14:50:51 +08:00
}
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",
},
2024-08-23 18:23:37 +08:00
},
2024-08-23 18:17:31 +08:00
Limit: 0,
Offset: 0,
ForceNoLimit: false,
OrderField: "id",
OrderRule: "desc",
WithCount: false,
ConditionList: nil,
TableColumnConfig: nil,
Tx: nil,
})
byteData, _ := json.Marshal(res)
2024-09-06 14:49:36 +08:00
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))
2024-08-23 17:54:48 +08:00
}