gorm/logger/sql_test.go

106 lines
5.6 KiB
Go
Raw Normal View History

2020-02-23 07:39:26 +03:00
package logger_test
import (
2020-09-19 08:48:34 +03:00
"database/sql/driver"
"encoding/json"
"fmt"
2020-02-23 07:39:26 +03:00
"regexp"
2020-09-19 08:48:34 +03:00
"strings"
2020-02-23 07:39:26 +03:00
"testing"
2020-06-02 04:25:55 +03:00
"github.com/jinzhu/now"
2020-06-02 04:16:07 +03:00
"gorm.io/gorm/logger"
2020-02-23 07:39:26 +03:00
)
2020-09-19 08:48:34 +03:00
type JSON json.RawMessage
func (j JSON) Value() (driver.Value, error) {
if len(j) == 0 {
return nil, nil
}
return json.RawMessage(j).MarshalJSON()
}
type ExampleStruct struct {
Name string
Val string
}
func (s ExampleStruct) Value() (driver.Value, error) {
return json.Marshal(s)
}
func format(v []byte, escaper string) string {
return escaper + strings.Replace(string(v), escaper, "\\"+escaper, -1) + escaper
}
2020-02-23 07:39:26 +03:00
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-09-19 08:48:34 +03:00
jsVal = []byte(`{"Name":"test","Val":"test"}`)
js = JSON(jsVal)
esVal = []byte(`{"Name":"test","Val":"test"}`)
es = ExampleStruct{Name: "test", Val: "test"}
2020-02-23 08:22:08 +03:00
)
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 10:16:40 +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-09-14 07:37:16 +03:00
{
SQL: "create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
NumericRegexp: nil,
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-26 14:06:42 +03:00
SQL: "create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass) values (@p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10, @p11)",
2020-07-16 06:27:04 +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-26 14:06:42 +03:00
SQL: "create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass) values ($3, $4, $1, $2, $7, $8, $5, $6, $9, $10, $11)",
2020-07-16 06:27:04 +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")`,
},
{
2020-02-26 14:06:42 +03:00
SQL: "create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass) values (@p1, @p11, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10)",
2020-07-16 06:27:04 +03:00
NumericRegexp: regexp.MustCompile(`@p(\d+)`),
2020-02-23 08:22:08 +03:00
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
},
2020-09-19 08:48:34 +03:00
{
SQL: "create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass, json_struct, example_struct) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
NumericRegexp: nil,
Vars: []interface{}{"jinzhu", 1, 999.99, true, []byte("12345"), tt, &tt, nil, "w@g.\"com", myrole, pwd, js, es},
Result: fmt.Sprintf(`create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass, json_struct, example_struct) 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", %v, %v)`, format(jsVal, `"`), format(esVal, `"`)),
},
{
SQL: "create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass, json_struct, example_struct) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
NumericRegexp: nil,
Vars: []interface{}{"jinzhu", 1, 999.99, true, []byte("12345"), tt, &tt, nil, "w@g.\"com", myrole, pwd, &js, &es},
Result: fmt.Sprintf(`create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass, json_struct, example_struct) 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", %v, %v)`, format(jsVal, `"`), format(esVal, `"`)),
},
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)
}
}
}