作者 yangfu

refactor: add test case

@@ -188,11 +188,13 @@ func ValueToType(value string, sqlType string) (interface{}, error) { @@ -188,11 +188,13 @@ func ValueToType(value string, sqlType string) (interface{}, error) {
188 if err != nil { 188 if err != nil {
189 err = fmt.Errorf("[%v]不是有效的日期类型", value) 189 err = fmt.Errorf("[%v]不是有效的日期类型", value)
190 } 190 }
  191 + toTypeVal = value
191 case Datetime.ToString(): 192 case Datetime.ToString():
192 toTypeVal, err = xtime.Parse(value) 193 toTypeVal, err = xtime.Parse(value)
193 if err != nil { 194 if err != nil {
194 err = fmt.Errorf("[%v]不是有效的时间类型", value) 195 err = fmt.Errorf("[%v]不是有效的时间类型", value)
195 } 196 }
  197 + toTypeVal = value
196 default: 198 default:
197 return nil, fmt.Errorf("unknow sql type :%v", sqlType) 199 return nil, fmt.Errorf("unknow sql type :%v", sqlType)
198 } 200 }
@@ -156,10 +156,6 @@ func (c Condition) InArgs(args interface{}) string { @@ -156,10 +156,6 @@ func (c Condition) InArgs(args interface{}) string {
156 func (c Condition) Arg(args interface{}) string { 156 func (c Condition) Arg(args interface{}) string {
157 bytes := make([]byte, 0) 157 bytes := make([]byte, 0)
158 v := reflect.ValueOf(args) 158 v := reflect.ValueOf(args)
159 - if v.Kind() == reflect.Int || v.Kind() == reflect.Int64 || v.Kind() == reflect.Float64 {  
160 - bytes = append(bytes, []byte(AssertString(args))...)  
161 - return string(bytes)  
162 - }  
163 bytes = appendValue(bytes, v) 159 bytes = appendValue(bytes, v)
164 return string(bytes) 160 return string(bytes)
165 } 161 }
@@ -192,10 +188,10 @@ func appendValue(b []byte, v reflect.Value) []byte { @@ -192,10 +188,10 @@ func appendValue(b []byte, v reflect.Value) []byte {
192 return append(b, "NULL"...) 188 return append(b, "NULL"...)
193 } 189 }
194 if v.Kind() == reflect.Int || v.Kind() == reflect.Int64 || v.Kind() == reflect.Float64 { 190 if v.Kind() == reflect.Int || v.Kind() == reflect.Int64 || v.Kind() == reflect.Float64 {
195 - return append(b, []byte(v.String())...) 191 + return append(b, []byte(AssertString(v.Interface()))...)
196 } 192 }
197 b = append(b, []byte("'")...) 193 b = append(b, []byte("'")...)
198 - b = append(b, []byte(v.String())...) 194 + b = append(b, []byte(AssertString(v.Interface()))...)
199 b = append(b, []byte("'")...) 195 b = append(b, []byte("'")...)
200 return b 196 return b
201 } 197 }
@@ -221,7 +217,6 @@ func WrapQueryFuncWithDB(db *gorm.DB) func(QueryOptions) (*sql.Rows, error) { @@ -221,7 +217,6 @@ func WrapQueryFuncWithDB(db *gorm.DB) func(QueryOptions) (*sql.Rows, error) {
221 } 217 }
222 if params.Context != nil { 218 if params.Context != nil {
223 query.Where(fmt.Sprintf("context->>'companyId'='%v'", params.Context.CompanyId)) 219 query.Where(fmt.Sprintf("context->>'companyId'='%v'", params.Context.CompanyId))
224 - //query.Where("context->>'companyId'='?'", params.Context.CompanyId)  
225 } 220 }
226 rows, err := query.Rows() 221 rows, err := query.Rows()
227 if err != nil { 222 if err != nil {
@@ -269,7 +264,6 @@ func WrapQueryCountWithDB(params QueryOptions, db *gorm.DB) func() (int64, error @@ -269,7 +264,6 @@ func WrapQueryCountWithDB(params QueryOptions, db *gorm.DB) func() (int64, error
269 queryWithoutLimitOffset(query, params) 264 queryWithoutLimitOffset(query, params)
270 if params.Context != nil { 265 if params.Context != nil {
271 query.Where(fmt.Sprintf("context->>'companyId'='%v'", params.Context.CompanyId)) 266 query.Where(fmt.Sprintf("context->>'companyId'='%v'", params.Context.CompanyId))
272 - //query.Where("context->>'companyId'='?'", params.Context.CompanyId)  
273 } 267 }
274 query.Count(&total) 268 query.Count(&total)
275 return total, query.Error 269 return total, query.Error
  1 +package starrocks
  2 +
  3 +import (
  4 + "github.com/stretchr/testify/assert"
  5 + "testing"
  6 + "time"
  7 +)
  8 +
  9 +func TestInArgs(t *testing.T) {
  10 + now := time.Date(2000, 1, 1, 0, 0, 0, 0, time.Local)
  11 + var table = []struct {
  12 + args []interface{}
  13 + want string
  14 + }{
  15 + {
  16 + args: []interface{}{1, 0, 2, 4},
  17 + want: "(1,0,2,4)",
  18 + },
  19 + {
  20 + args: []interface{}{1.0, 0, 2.1, 4.0},
  21 + want: "(1,0,2.1,4)",
  22 + },
  23 + {
  24 + args: []interface{}{"1", "0", "2", "4"},
  25 + want: "('1','0','2','4')",
  26 + },
  27 + {
  28 + args: []interface{}{now, now},
  29 + want: "('2000-01-01 00:00:00','2000-01-01 00:00:00')",
  30 + },
  31 + }
  32 + c := &Condition{}
  33 + for _, input := range table {
  34 + got := c.InArgs(input.args)
  35 + assert.Equal(t, input.want, got)
  36 + }
  37 +}
  38 +func TestArg(t *testing.T) {
  39 + now := time.Date(2000, 1, 1, 0, 0, 0, 0, time.Local)
  40 + var table = []struct {
  41 + args []interface{}
  42 + want string
  43 + }{
  44 + {
  45 + args: []interface{}{1, 0, 2, 4},
  46 + want: "1",
  47 + },
  48 + {
  49 + args: []interface{}{1.0, 0, 2.1, 4.0},
  50 + want: "1",
  51 + },
  52 + {
  53 + args: []interface{}{"1", "0", "2", "4"},
  54 + want: "'1'",
  55 + },
  56 + {
  57 + args: []interface{}{now, now},
  58 + want: "'2000-01-01 00:00:00'",
  59 + },
  60 + }
  61 + c := &Condition{}
  62 + for _, input := range table {
  63 + got := c.Arg(input.args[0])
  64 + assert.Equal(t, input.want, got)
  65 + }
  66 +}
1 package utils 1 package utils
2 2
3 import ( 3 import (
  4 + "fmt"
4 "github.com/beego/beego/v2/core/config" 5 "github.com/beego/beego/v2/core/config"
5 "github.com/beego/beego/v2/server/web" 6 "github.com/beego/beego/v2/server/web"
6 "os" 7 "os"
@@ -86,6 +87,14 @@ func (c BeegoAppConfigurator) DefaultFloat(key string, defaultVal float64) float @@ -86,6 +87,14 @@ func (c BeegoAppConfigurator) DefaultFloat(key string, defaultVal float64) float
86 } 87 }
87 88
88 func NewConfig(adapterName, filename string) Configurator { 89 func NewConfig(adapterName, filename string) Configurator {
  90 + if _, err := os.Stat(filename); err != nil {
  91 + fmt.Println(err.Error())
  92 + config, err := config.NewConfigData(adapterName, []byte{})
  93 + if err != nil {
  94 + panic(err)
  95 + }
  96 + return config
  97 + }
89 config, err := config.NewConfig(adapterName, filename) 98 config, err := config.NewConfig(adapterName, filename)
90 if err != nil { 99 if err != nil {
91 panic(err) 100 panic(err)