print entire sql statements in logx if necessary (#704)

This commit is contained in:
Kevin Wan
2021-05-20 16:14:44 +08:00
committed by GitHub
parent 73906f996d
commit aaa39e17a3
5 changed files with 154 additions and 68 deletions

View File

@@ -14,6 +14,7 @@ var errMockedPlaceholder = errors.New("placeholder")
func TestStmt_exec(t *testing.T) {
tests := []struct {
name string
query string
args []interface{}
delay bool
hasError bool
@@ -23,18 +24,28 @@ func TestStmt_exec(t *testing.T) {
}{
{
name: "normal",
query: "select user from users where id=?",
args: []interface{}{1},
lastInsertId: 1,
rowsAffected: 2,
},
{
name: "exec error",
query: "select user from users where id=?",
args: []interface{}{1},
hasError: true,
err: errors.New("exec"),
},
{
name: "exec more args error",
query: "select user from users where id=? and name=?",
args: []interface{}{1},
hasError: true,
err: errors.New("exec"),
},
{
name: "slowcall",
query: "select user from users where id=?",
args: []interface{}{1},
delay: true,
lastInsertId: 1,
@@ -51,7 +62,7 @@ func TestStmt_exec(t *testing.T) {
rowsAffected: test.rowsAffected,
err: test.err,
delay: test.delay,
}, "select user from users where id=?", args...)
}, test.query, args...)
},
func(args ...interface{}) (sql.Result, error) {
return execStmt(&mockedStmtConn{
@@ -59,7 +70,7 @@ func TestStmt_exec(t *testing.T) {
rowsAffected: test.rowsAffected,
err: test.err,
delay: test.delay,
}, args...)
}, test.query, args...)
},
}
@@ -89,23 +100,34 @@ func TestStmt_exec(t *testing.T) {
func TestStmt_query(t *testing.T) {
tests := []struct {
name string
query string
args []interface{}
delay bool
hasError bool
err error
}{
{
name: "normal",
args: []interface{}{1},
name: "normal",
query: "select user from users where id=?",
args: []interface{}{1},
},
{
name: "query error",
query: "select user from users where id=?",
args: []interface{}{1},
hasError: true,
err: errors.New("exec"),
},
{
name: "query more args error",
query: "select user from users where id=? and name=?",
args: []interface{}{1},
hasError: true,
err: errors.New("exec"),
},
{
name: "slowcall",
query: "select user from users where id=?",
args: []interface{}{1},
delay: true,
},
@@ -120,7 +142,7 @@ func TestStmt_query(t *testing.T) {
delay: test.delay,
}, func(rows *sql.Rows) error {
return nil
}, "select user from users where id=?", args...)
}, test.query, args...)
},
func(args ...interface{}) error {
return queryStmt(&mockedStmtConn{
@@ -128,7 +150,7 @@ func TestStmt_query(t *testing.T) {
delay: test.delay,
}, func(rows *sql.Rows) error {
return nil
}, args...)
}, test.query, args...)
},
}
@@ -143,7 +165,7 @@ func TestStmt_query(t *testing.T) {
return
}
assert.Equal(t, errMockedPlaceholder, err)
assert.NotNil(t, err)
})
}
}