Skip to content

Commit

Permalink
Merge pull request #55 from wenj91/dev
Browse files Browse the repository at this point in the history
u-update yaml package to v2.4.0
  • Loading branch information
wenj91 authored Feb 4, 2021
2 parents 1a1975b + 5b6530f commit 741a89c
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 144 deletions.
50 changes: 0 additions & 50 deletions assert.go

This file was deleted.

41 changes: 21 additions & 20 deletions config_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gobatis

import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)

Expand All @@ -28,16 +29,16 @@ mappers:
dbconf := buildDbConfig(ymlStr)

dbc := dbconf.getDataSourceByName("ds1")
assertTrue(dbc != nil, "test fail: No datasource1")
assertTrue(dbconf.ShowSQL, "test fail: showSql == false")
assertEqual(dbc.DriverName, "mysql", "test fail, actual:"+dbc.DriverName)
assertEqual(dbc.DataSourceName, "root:123456@tcp(127.0.0.1:3306)/test?charset=utf8", "test fail, actual:"+dbc.DataSourceName)
assertEqual(dbc.MaxLifeTime, 120, "test fail, actual:"+fmt.Sprintf("%d", dbc.MaxLifeTime))
assertEqual(dbc.MaxOpenConns, 10, "test fail, actual:"+fmt.Sprintf("%d", dbc.MaxOpenConns))
assertEqual(dbc.MaxIdleConns, 5, "test fail, actual:"+fmt.Sprintf("%d", dbc.MaxIdleConns))
assertTrue(len(dbconf.Mappers) == 2, "len(dbconf.Mappers) != 2")
assertEqual(dbconf.Mappers[0], "userMapper.xml", "test fail, actual:"+dbconf.Mappers[0])
assertEqual(dbconf.Mappers[1], "orderMapper.xml", "test fail, actual:"+dbconf.Mappers[1])
assert.True(t, dbc != nil, "test fail: No datasource1")
assert.True(t, dbconf.ShowSQL, "test fail: showSql == false")
assert.Equal(t, dbc.DriverName, "mysql", "test fail, actual:"+dbc.DriverName)
assert.Equal(t, dbc.DataSourceName, "root:123456@tcp(127.0.0.1:3306)/test?charset=utf8", "test fail, actual:"+dbc.DataSourceName)
assert.Equal(t, dbc.MaxLifeTime, 120, "test fail, actual:"+fmt.Sprintf("%d", dbc.MaxLifeTime))
assert.Equal(t, dbc.MaxOpenConns, 10, "test fail, actual:"+fmt.Sprintf("%d", dbc.MaxOpenConns))
assert.Equal(t, dbc.MaxIdleConns, 5, "test fail, actual:"+fmt.Sprintf("%d", dbc.MaxIdleConns))
assert.True(t, len(dbconf.Mappers) == 2, "len(dbconf.Mappers) != 2")
assert.Equal(t, dbconf.Mappers[0], "userMapper.xml", "test fail, actual:"+dbconf.Mappers[0])
assert.Equal(t, dbconf.Mappers[1], "orderMapper.xml", "test fail, actual:"+dbconf.Mappers[1])
}

func TestDbConfigCodeInit(t *testing.T) {
Expand All @@ -57,14 +58,14 @@ func TestDbConfigCodeInit(t *testing.T) {
Build()

dbc := dbconf.getDataSourceByName("ds1")
assertTrue(dbc != nil, "test fail: No datasource1")
assertTrue(dbconf.ShowSQL, "test fail: showSql == false")
assertEqual(dbc.DriverName, "mysql", "test fail, actual:"+dbc.DriverName)
assertEqual(dbc.DataSourceName, "root:123456@tcp(127.0.0.1:3306)/test?charset=utf8", "test fail, actual:"+dbc.DataSourceName)
assertEqual(dbc.MaxLifeTime, 120, "test fail, actual:"+fmt.Sprintf("%d", dbc.MaxLifeTime))
assertEqual(dbc.MaxOpenConns, 10, "test fail, actual:"+fmt.Sprintf("%d", dbc.MaxOpenConns))
assertEqual(dbc.MaxIdleConns, 5, "test fail, actual:"+fmt.Sprintf("%d", dbc.MaxIdleConns))
assertTrue(len(dbconf.Mappers) == 2, "len(dbconf.Mappers) != 2")
assertEqual(dbconf.Mappers[0], "userMapper.xml", "test fail, actual:"+dbconf.Mappers[0])
assertEqual(dbconf.Mappers[1], "orderMapper.xml", "test fail, actual:"+dbconf.Mappers[1])
assert.True(t, dbc != nil, "test fail: No datasource1")
assert.True(t, dbconf.ShowSQL, "test fail: showSql == false")
assert.Equal(t, dbc.DriverName, "mysql", "test fail, actual:"+dbc.DriverName)
assert.Equal(t, dbc.DataSourceName, "root:123456@tcp(127.0.0.1:3306)/test?charset=utf8", "test fail, actual:"+dbc.DataSourceName)
assert.Equal(t, dbc.MaxLifeTime, 120, "test fail, actual:"+fmt.Sprintf("%d", dbc.MaxLifeTime))
assert.Equal(t, dbc.MaxOpenConns, 10, "test fail, actual:"+fmt.Sprintf("%d", dbc.MaxOpenConns))
assert.Equal(t, dbc.MaxIdleConns, 5, "test fail, actual:"+fmt.Sprintf("%d", dbc.MaxIdleConns))
assert.True(t, len(dbconf.Mappers) == 2, "len(dbconf.Mappers) != 2")
assert.Equal(t, dbconf.Mappers[0], "userMapper.xml", "test fail, actual:"+dbconf.Mappers[0])
assert.Equal(t, dbconf.Mappers[1], "orderMapper.xml", "test fail, actual:"+dbconf.Mappers[1])
}
11 changes: 6 additions & 5 deletions config_mapper_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gobatis

import (
"github.com/stretchr/testify/assert"
"strings"
"testing"
)
Expand Down Expand Up @@ -38,9 +39,9 @@ func TestBuildConfig(t *testing.T) {
`
r := strings.NewReader(xmlStr)
conf := buildMapperConfig(r)
assertNotNil(conf.getMappedStmt("Mapper.findMapById"), "Mapper.findMapById mapped stmt is nil")
assertNotNil(conf.getMappedStmt("Mapper.insertStructsBatch"), "Mapper.insertStructsBatch mapped stmt is nil")
assertNotNil(conf.getMappedStmt("Mapper.updateByStruct"), "Mapper.updateByStruct mapped stmt is nil")
assertNotNil(conf.getMappedStmt("Mapper.deleteById"), "Mapper.deleteById mapped stmt is nil")
assertNotNil(conf.getMappedStmt("Mapper.updateByCond"), "Mapper.deleteById mapped stmt is nil")
assert.NotNil(t, conf.getMappedStmt("Mapper.findMapById"), "Mapper.findMapById mapped stmt is nil")
assert.NotNil(t, conf.getMappedStmt("Mapper.insertStructsBatch"), "Mapper.insertStructsBatch mapped stmt is nil")
assert.NotNil(t, conf.getMappedStmt("Mapper.updateByStruct"), "Mapper.updateByStruct mapped stmt is nil")
assert.NotNil(t, conf.getMappedStmt("Mapper.deleteById"), "Mapper.deleteById mapped stmt is nil")
assert.NotNil(t, conf.getMappedStmt("Mapper.updateByCond"), "Mapper.deleteById mapped stmt is nil")
}
9 changes: 5 additions & 4 deletions expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gobatis

import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)

Expand Down Expand Up @@ -48,15 +49,15 @@ func TestExpr_eval(t *testing.T) {
for i, ex := range expression {
ok := eval(ex, params)
fmt.Printf("Index:%v Expr:%v >>>> Result:%v \n", i, ex, ok)
assertExpr(i, ok, ex)
assertExpr(t, i, ok, ex)
}
}

func assertExpr(i int, ok bool, expr string) {
func assertExpr(t *testing.T, i int, ok bool, expr string) {
switch i {
case 0, 3, 4, 6, 8, 9, 12, 14, 17, 22: // false
assertNotTrue(ok, "Expr:"+expr+" Result:true")
assert.True(t, !ok, "Expr:"+expr+" Result:true")
case 1, 2, 5, 7, 10, 11, 13, 15, 16, 18, 19, 20, 21: // true
assertTrue(ok, "Expr:"+expr+" Result:false")
assert.True(t, ok, "Expr:"+expr+" Result:false")
}
}
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.12
require (
github.com/antonmedv/expr v1.1.4
github.com/go-sql-driver/mysql v1.4.1
gopkg.in/yaml.v2 v2.0.0-00010101000000-000000000000
gopkg.in/yaml.v2 v2.4.0
github.com/stretchr/testify v1.7.0
)

replace gopkg.in/yaml.v2 => github.com/go-yaml/yaml v2.1.0+incompatible
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
github.com/antonmedv/expr v1.1.4 h1:ReeidbMJQZsujmBaYdaCEtfAvSH5bd/HHApjhyorBlE=
github.com/antonmedv/expr v1.1.4/go.mod h1:AdVomDZyjUit6l23+Owki7ZHDtvJDjsaP4YmqB44OJA=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZpNwpA=
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-yaml/yaml v2.1.0+incompatible h1:RYi2hDdss1u4YE7GwixGzWwVo47T8UQwnTLB6vQiq+o=
github.com/go-yaml/yaml v2.1.0+incompatible/go.mod h1:w2MrLa16VYP0jy6N7M5kHaCkaLENm+P+Tv+MfurjSw0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
28 changes: 14 additions & 14 deletions params_test.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
package gobatis

import (
"github.com/stretchr/testify/assert"
"reflect"
"testing"
)

type TestStruct struct {

}

func TestVal(t *testing.T) {
func TestVal(t *testing.T) {
paramsInt := 1
v := reflect.ValueOf(paramsInt)
assertTrue(v.Kind() == reflect.Int, "test fail: params is not int")
assert.True(t, v.Kind() == reflect.Int, "test fail: params is not int")

paramsInt64 := int64(1)
v = reflect.ValueOf(paramsInt64)
assertTrue(v.Kind() == reflect.Int64, "test fail: params is not int64")
assert.True(t, v.Kind() == reflect.Int64, "test fail: params is not int64")

paramsString := ""
v = reflect.ValueOf(paramsString)
assertTrue(v.Kind() == reflect.String, "test fail: params is not string")
assert.True(t, v.Kind() == reflect.String, "test fail: params is not string")

paramsSlice := []int{1, 2, 3}
v = reflect.ValueOf(paramsSlice)
assertTrue(v.Kind() == reflect.Slice, "test fail: params is not slice")
assert.True(t, v.Kind() == reflect.Slice, "test fail: params is not slice")

paramsStruct := TestStruct{}
v = reflect.ValueOf(paramsStruct)
assertTrue(v.Kind() == reflect.Struct, "test fail: params is not struct")
assert.True(t, v.Kind() == reflect.Struct, "test fail: params is not struct")

paramsPtr := &TestStruct{}
v = reflect.ValueOf(paramsPtr)
assertTrue(v.Kind() == reflect.Ptr, "test fail: params is not ptr")
assert.True(t, v.Kind() == reflect.Ptr, "test fail: params is not ptr")
v = v.Elem()
assertTrue(v.Kind() == reflect.Struct, "test fail: params is not struct")
assert.True(t, v.Kind() == reflect.Struct, "test fail: params is not struct")

paramsStructs := []*TestStruct{{},{},{}}
paramsStructs := []*TestStruct{{}, {}, {}}
v = reflect.ValueOf(paramsStructs)
assertTrue(v.Kind() == reflect.Slice, "test fail: params is not slice")
assertTrue(v.Len() == 3, "test fail: params len != 3")
assert.True(t, v.Kind() == reflect.Slice, "test fail: params is not slice")
assert.True(t, v.Len() == 3, "test fail: params len != 3")
v0 := v.Index(0)
assertTrue(v0.Kind() == reflect.Ptr, "test fail: ele is not ptr")
assert.True(t, v0.Kind() == reflect.Ptr, "test fail: ele is not ptr")
v0 = v0.Elem()
assertTrue(v0.Kind() == reflect.Struct, "test fail: ele is not struct")
assert.True(t, v0.Kind() == reflect.Struct, "test fail: ele is not struct")
}
2 changes: 1 addition & 1 deletion parser_xml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ func TestXmlNode_parse(t *testing.T) {
`
r := strings.NewReader(xmlStr)
rn := parse(r)
assertNotNil(rn, "Parse xml result is nil")
assert.NotNil(t, rn, "Parse xml result is nil")
}
34 changes: 17 additions & 17 deletions proc_params_test.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
package gobatis

import (
"github.com/stretchr/testify/assert"
"testing"
"time"
)

type TestStruct2 struct {
T NullTime
S NullString
T NullTime
S NullString
Id int64
}

func TestParams(t *testing.T) {
func TestParams(t *testing.T) {
paramB := ""
res := paramProcess(paramB)
assertNotNil(res["0"], "test fail: res[0] == nil")
assertEqual(res["0"], "", "test fail: res[0] != ''")

assert.NotNil(t, res["0"], "test fail: res[0] == nil")
assert.Equal(t, res["0"], "", "test fail: res[0] != ''")

paramM := map[string]interface{}{
"id": nil,
"name":"wenj91",
"id": nil,
"name": "wenj91",
}
res = paramProcess(paramM)
assertNil(res["id"], "test fail: res['id'] != nil")
assertNotNil(res["name"], "test fail: res['name'] == nil")
assertEqual(res["name"], "wenj91", "test fail: res['name'] != 'wenj91'")
assert.Nil(t, res["id"], "test fail: res['id'] != nil")
assert.NotNil(t, res["name"], "test fail: res['name'] == nil")
assert.Equal(t, res["name"], "wenj91", "test fail: res['name'] != 'wenj91'")

paramNil := NullString{"str", true}
res = paramProcess(paramNil)
assertNotNil(res["0"], "test fail: res['0'] == nil")
assertEqual(res["0"], "str", "test fail: res['0'] != 'str'")
assert.NotNil(t, res["0"], "test fail: res['0'] == nil")
assert.Equal(t, res["0"], "str", "test fail: res['0'] != 'str'")

tt, _ := time.Parse("2006-01-02 15:04:05", "2006-01-02 15:04:05")
paramS := &TestStruct2{
T: NullTime{tt, true},
}
res = paramProcess(paramS)
assertNotNil(res["T"], "test fail: res['T'] == nil")
assertEqual(res["T"], "2006-01-02 15:04:05", "test fail: res['T'] != '2006-01-02 15:04:05'")
assertNil(res["S"], "test fail: res['S'] != nil")
assertEqual(res["Id"], int64(0), "test fail: res['Id'] != 0")
assert.NotNil(t, res["T"], "test fail: res['T'] == nil")
assert.Equal(t, res["T"], "2006-01-02 15:04:05", "test fail: res['T'] != '2006-01-02 15:04:05'")
assert.Nil(t, res["S"], "test fail: res['S'] != nil")
assert.Equal(t, res["Id"], int64(0), "test fail: res['Id'] != 0")
}
17 changes: 9 additions & 8 deletions sql_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gobatis

import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)

Expand All @@ -17,9 +18,9 @@ func TestStaticSqlSource_getBoundSql(t *testing.T) {
})

expc := "select * from t_gap where id = ? and gap = ?"
assertEqual(bs.sqlStr, expc, "test failed, actual:"+bs.sqlStr)
assertEqual(bs.params["id"], 1, "test failed, actual:"+fmt.Sprintf("%d", bs.params["id"]))
assertEqual(bs.params["gap"], 10, "test failed, actual:"+fmt.Sprintf("%d", bs.params["gap"]))
assert.Equal(t, bs.sqlStr, expc, "test failed, actual:"+bs.sqlStr)
assert.Equal(t, bs.params["id"], 1, "test failed, actual:"+fmt.Sprintf("%d", bs.params["id"]))
assert.Equal(t, bs.params["gap"], 10, "test failed, actual:"+fmt.Sprintf("%d", bs.params["gap"]))
}

func TestDynamicSqlSource_getBoundSql(t *testing.T) {
Expand Down Expand Up @@ -94,9 +95,9 @@ func TestDynamicSqlSource_getBoundSql(t *testing.T) {
bs := ds.getBoundSql(params)

expc := "select 1 from t_gap where age = ? and name = ? and code = 'cctv' and id in ( ? , ? , ? )"
assertEqual(bs.sqlStr, expc, "test failed, actual:"+bs.sqlStr)
assertEqual(bs.params["name"], "Sean", "test failed, actual:"+fmt.Sprintf("%d", bs.params["id"]))
assertEqual(bs.extParams["_ls_item_p_item0.A"], "aa", "test failed, actual:"+fmt.Sprintf("%s", bs.extParams["_ls_item_p_item0.A"]))
assertEqual(bs.extParams["_ls_item_p_item1.A"], "bb", "test failed, actual:"+fmt.Sprintf("%s", bs.extParams["_ls_item_p_item1.A"]))
assertEqual(bs.extParams["_ls_item_p_item2.A"], "cc", "test failed, actual:"+fmt.Sprintf("%s", bs.extParams["_ls_item_p_item2.A"]))
assert.Equal(t, bs.sqlStr, expc, "test failed, actual:"+bs.sqlStr)
assert.Equal(t, bs.params["name"], "Sean", "test failed, actual:"+fmt.Sprintf("%d", bs.params["id"]))
assert.Equal(t, bs.extParams["_ls_item_p_item0.A"], "aa", "test failed, actual:"+fmt.Sprintf("%s", bs.extParams["_ls_item_p_item0.A"]))
assert.Equal(t, bs.extParams["_ls_item_p_item1.A"], "bb", "test failed, actual:"+fmt.Sprintf("%s", bs.extParams["_ls_item_p_item1.A"]))
assert.Equal(t, bs.extParams["_ls_item_p_item2.A"], "cc", "test failed, actual:"+fmt.Sprintf("%s", bs.extParams["_ls_item_p_item2.A"]))
}
Loading

0 comments on commit 741a89c

Please sign in to comment.