github.com/systematiccaos/gorm@v1.22.6/logger/sql_test.go (about) 1 package logger_test 2 3 import ( 4 "database/sql/driver" 5 "encoding/json" 6 "fmt" 7 "regexp" 8 "strings" 9 "testing" 10 11 "github.com/jinzhu/now" 12 "github.com/systematiccaos/gorm/logger" 13 ) 14 15 type JSON json.RawMessage 16 17 func (j JSON) Value() (driver.Value, error) { 18 if len(j) == 0 { 19 return nil, nil 20 } 21 return json.RawMessage(j).MarshalJSON() 22 } 23 24 type ExampleStruct struct { 25 Name string 26 Val string 27 } 28 29 func (s ExampleStruct) Value() (driver.Value, error) { 30 return json.Marshal(s) 31 } 32 33 func format(v []byte, escaper string) string { 34 return escaper + strings.Replace(string(v), escaper, "\\"+escaper, -1) + escaper 35 } 36 37 func TestExplainSQL(t *testing.T) { 38 type role string 39 type password []byte 40 var ( 41 tt = now.MustParse("2020-02-23 11:10:10") 42 myrole = role("admin") 43 pwd = password([]byte("pass")) 44 jsVal = []byte(`{"Name":"test","Val":"test"}`) 45 js = JSON(jsVal) 46 esVal = []byte(`{"Name":"test","Val":"test"}`) 47 es = ExampleStruct{Name: "test", Val: "test"} 48 ) 49 50 results := []struct { 51 SQL string 52 NumericRegexp *regexp.Regexp 53 Vars []interface{} 54 Result string 55 }{ 56 { 57 SQL: "create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", 58 NumericRegexp: nil, 59 Vars: []interface{}{"jinzhu", 1, 999.99, true, []byte("12345"), tt, &tt, nil, "w@g.\"com", myrole, pwd}, 60 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")`, 61 }, 62 { 63 SQL: "create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", 64 NumericRegexp: nil, 65 Vars: []interface{}{"jinzhu?", 1, 999.99, true, []byte("12345"), tt, &tt, nil, "w@g.\"com", myrole, pwd}, 66 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")`, 67 }, 68 { 69 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)", 70 NumericRegexp: regexp.MustCompile(`@p(\d+)`), 71 Vars: []interface{}{"jinzhu", 1, 999.99, true, []byte("12345"), tt, &tt, nil, "w@g.com", myrole, pwd}, 72 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")`, 73 }, 74 { 75 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)", 76 NumericRegexp: regexp.MustCompile(`\$(\d+)`), 77 Vars: []interface{}{999.99, true, "jinzhu", 1, &tt, nil, []byte("12345"), tt, "w@g.com", myrole, pwd}, 78 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")`, 79 }, 80 { 81 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)", 82 NumericRegexp: regexp.MustCompile(`@p(\d+)`), 83 Vars: []interface{}{"jinzhu", 999.99, true, []byte("12345"), tt, &tt, nil, "w@g.com", myrole, pwd, 1}, 84 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")`, 85 }, 86 { 87 SQL: "create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass, json_struct, example_struct) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", 88 NumericRegexp: nil, 89 Vars: []interface{}{"jinzhu", 1, 999.99, true, []byte("12345"), tt, &tt, nil, "w@g.\"com", myrole, pwd, js, es}, 90 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, `"`)), 91 }, 92 { 93 SQL: "create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass, json_struct, example_struct) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", 94 NumericRegexp: nil, 95 Vars: []interface{}{"jinzhu", 1, 999.99, true, []byte("12345"), tt, &tt, nil, "w@g.\"com", myrole, pwd, &js, &es}, 96 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, `"`)), 97 }, 98 } 99 100 for idx, r := range results { 101 if result := logger.ExplainSQL(r.SQL, r.NumericRegexp, `"`, r.Vars...); result != r.Result { 102 t.Errorf("Explain SQL #%v expects %v, but got %v", idx, r.Result, result) 103 } 104 } 105 }