作者 yangfu

refactor: add test case

... ... @@ -188,11 +188,13 @@ func ValueToType(value string, sqlType string) (interface{}, error) {
if err != nil {
err = fmt.Errorf("[%v]不是有效的日期类型", value)
}
toTypeVal = value
case Datetime.ToString():
toTypeVal, err = xtime.Parse(value)
if err != nil {
err = fmt.Errorf("[%v]不是有效的时间类型", value)
}
toTypeVal = value
default:
return nil, fmt.Errorf("unknow sql type :%v", sqlType)
}
... ...
... ... @@ -156,10 +156,6 @@ func (c Condition) InArgs(args interface{}) string {
func (c Condition) Arg(args interface{}) string {
bytes := make([]byte, 0)
v := reflect.ValueOf(args)
if v.Kind() == reflect.Int || v.Kind() == reflect.Int64 || v.Kind() == reflect.Float64 {
bytes = append(bytes, []byte(AssertString(args))...)
return string(bytes)
}
bytes = appendValue(bytes, v)
return string(bytes)
}
... ... @@ -192,10 +188,10 @@ func appendValue(b []byte, v reflect.Value) []byte {
return append(b, "NULL"...)
}
if v.Kind() == reflect.Int || v.Kind() == reflect.Int64 || v.Kind() == reflect.Float64 {
return append(b, []byte(v.String())...)
return append(b, []byte(AssertString(v.Interface()))...)
}
b = append(b, []byte("'")...)
b = append(b, []byte(v.String())...)
b = append(b, []byte(AssertString(v.Interface()))...)
b = append(b, []byte("'")...)
return b
}
... ... @@ -221,7 +217,6 @@ func WrapQueryFuncWithDB(db *gorm.DB) func(QueryOptions) (*sql.Rows, error) {
}
if params.Context != nil {
query.Where(fmt.Sprintf("context->>'companyId'='%v'", params.Context.CompanyId))
//query.Where("context->>'companyId'='?'", params.Context.CompanyId)
}
rows, err := query.Rows()
if err != nil {
... ... @@ -269,7 +264,6 @@ func WrapQueryCountWithDB(params QueryOptions, db *gorm.DB) func() (int64, error
queryWithoutLimitOffset(query, params)
if params.Context != nil {
query.Where(fmt.Sprintf("context->>'companyId'='%v'", params.Context.CompanyId))
//query.Where("context->>'companyId'='?'", params.Context.CompanyId)
}
query.Count(&total)
return total, query.Error
... ...
package starrocks
import (
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestInArgs(t *testing.T) {
now := time.Date(2000, 1, 1, 0, 0, 0, 0, time.Local)
var table = []struct {
args []interface{}
want string
}{
{
args: []interface{}{1, 0, 2, 4},
want: "(1,0,2,4)",
},
{
args: []interface{}{1.0, 0, 2.1, 4.0},
want: "(1,0,2.1,4)",
},
{
args: []interface{}{"1", "0", "2", "4"},
want: "('1','0','2','4')",
},
{
args: []interface{}{now, now},
want: "('2000-01-01 00:00:00','2000-01-01 00:00:00')",
},
}
c := &Condition{}
for _, input := range table {
got := c.InArgs(input.args)
assert.Equal(t, input.want, got)
}
}
func TestArg(t *testing.T) {
now := time.Date(2000, 1, 1, 0, 0, 0, 0, time.Local)
var table = []struct {
args []interface{}
want string
}{
{
args: []interface{}{1, 0, 2, 4},
want: "1",
},
{
args: []interface{}{1.0, 0, 2.1, 4.0},
want: "1",
},
{
args: []interface{}{"1", "0", "2", "4"},
want: "'1'",
},
{
args: []interface{}{now, now},
want: "'2000-01-01 00:00:00'",
},
}
c := &Condition{}
for _, input := range table {
got := c.Arg(input.args[0])
assert.Equal(t, input.want, got)
}
}
... ...
package utils
import (
"fmt"
"github.com/beego/beego/v2/core/config"
"github.com/beego/beego/v2/server/web"
"os"
... ... @@ -86,6 +87,14 @@ func (c BeegoAppConfigurator) DefaultFloat(key string, defaultVal float64) float
}
func NewConfig(adapterName, filename string) Configurator {
if _, err := os.Stat(filename); err != nil {
fmt.Println(err.Error())
config, err := config.NewConfigData(adapterName, []byte{})
if err != nil {
panic(err)
}
return config
}
config, err := config.NewConfig(adapterName, filename)
if err != nil {
panic(err)
... ...