2020-02-23 07:39:26 +03:00
|
|
|
package logger_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/jinzhu/gorm/logger"
|
|
|
|
"github.com/jinzhu/now"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestExplainSQL(t *testing.T) {
|
2020-02-23 08:22:08 +03:00
|
|
|
type role string
|
|
|
|
type password []byte
|
|
|
|
var (
|
|
|
|
tt = now.MustParse("2020-02-23 11:10:10")
|
|
|
|
myrole = role("admin")
|
|
|
|
pwd = password([]byte("pass"))
|
|
|
|
)
|
2020-02-23 07:39:26 +03:00
|
|
|
|
|
|
|
results := []struct {
|
|
|
|
SQL string
|
|
|
|
NumericRegexp *regexp.Regexp
|
|
|
|
Vars []interface{}
|
|
|
|
Result string
|
|
|
|
}{
|
|
|
|
{
|
2020-02-23 08:22:08 +03:00
|
|
|
SQL: "create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
2020-02-23 07:39:26 +03:00
|
|
|
NumericRegexp: nil,
|
2020-02-23 08:22:08 +03:00
|
|
|
Vars: []interface{}{"jinzhu", 1, 999.99, true, []byte("12345"), tt, &tt, nil, "w@g.com", myrole, pwd},
|
|
|
|
Result: `create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass) values ("jinzhu", 1, 999.990000, true, "12345", "2020-02-23 11:10:10", "2020-02-23 11:10:10", NULL, "w@g.com", "admin", "pass")`,
|
2020-02-23 07:39:26 +03:00
|
|
|
},
|
|
|
|
{
|
2020-02-23 08:22:08 +03:00
|
|
|
SQL: "create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass) values (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10)",
|
2020-02-23 07:39:26 +03:00
|
|
|
NumericRegexp: regexp.MustCompile("@p(\\d+)"),
|
2020-02-23 08:22:08 +03:00
|
|
|
Vars: []interface{}{"jinzhu", 1, 999.99, true, []byte("12345"), tt, &tt, nil, "w@g.com", myrole, pwd},
|
|
|
|
Result: `create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass) values ("jinzhu", 1, 999.990000, true, "12345", "2020-02-23 11:10:10", "2020-02-23 11:10:10", NULL, "w@g.com", "admin", "pass")`,
|
2020-02-23 07:39:26 +03:00
|
|
|
},
|
|
|
|
{
|
2020-02-23 08:22:08 +03:00
|
|
|
SQL: "create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass) values ($2, $3, $0, $1, $6, $7, $4, $5, $8, $9, $10)",
|
2020-02-23 07:39:26 +03:00
|
|
|
NumericRegexp: regexp.MustCompile("\\$(\\d+)"),
|
2020-02-23 08:22:08 +03:00
|
|
|
Vars: []interface{}{999.99, true, "jinzhu", 1, &tt, nil, []byte("12345"), tt, "w@g.com", myrole, pwd},
|
|
|
|
Result: `create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass) values ("jinzhu", 1, 999.990000, true, "12345", "2020-02-23 11:10:10", "2020-02-23 11:10:10", NULL, "w@g.com", "admin", "pass")`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SQL: "create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass) values (@p0, @p10, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9)",
|
|
|
|
NumericRegexp: regexp.MustCompile("@p(\\d+)"),
|
|
|
|
Vars: []interface{}{"jinzhu", 999.99, true, []byte("12345"), tt, &tt, nil, "w@g.com", myrole, pwd, 1},
|
|
|
|
Result: `create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass) values ("jinzhu", 1, 999.990000, true, "12345", "2020-02-23 11:10:10", "2020-02-23 11:10:10", NULL, "w@g.com", "admin", "pass")`,
|
2020-02-23 07:39:26 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for idx, r := range results {
|
|
|
|
if result := logger.ExplainSQL(r.SQL, r.NumericRegexp, `"`, r.Vars...); result != r.Result {
|
|
|
|
t.Errorf("Explain SQL #%v expects %v, but got %v", idx, r.Result, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|