improve unit testing case for driver feature og package gdb

This commit is contained in:
John 2020-03-08 12:16:44 +08:00
parent 356f4cd701
commit 5b7576430f

View File

@ -15,13 +15,18 @@ import (
) )
// MyDriver is a custom database driver, which is used for testing only. // MyDriver is a custom database driver, which is used for testing only.
// For simplifying the unit testing case purpose, MyDriver struct inherits the mysql driver
// gdb.DriverMysql and overwrites its function HandleSqlBeforeExec.
// So if there's any sql execution, it goes through MyDriver.HandleSqlBeforeExec firstly and
// then gdb.DriverMysql.HandleSqlBeforeExec.
// You can call it sql "HOOK" or "HiJack" as your will.
type MyDriver struct { type MyDriver struct {
*gdb.DriverMysql *gdb.DriverMysql
} }
var ( var (
customDriverName = "MyDriver" customDriverName = "MyDriver"
lastSqlString = gtype.NewString() // For unit testing only. latestSqlString = gtype.NewString() // For simplifying unit testing only.
) )
// New creates and returns a database object for mysql. // New creates and returns a database object for mysql.
@ -37,11 +42,13 @@ func (d *MyDriver) New(core *gdb.Core, node *gdb.ConfigNode) (gdb.DB, error) {
// HandleSqlBeforeExec handles the sql before posts it to database. // HandleSqlBeforeExec handles the sql before posts it to database.
// It here overwrites the same method of gdb.DriverMysql and makes some custom changes. // It here overwrites the same method of gdb.DriverMysql and makes some custom changes.
func (d *MyDriver) HandleSqlBeforeExec(sql string) string { func (d *MyDriver) HandleSqlBeforeExec(sql string) string {
lastSqlString.Set(sql) latestSqlString.Set(sql)
return d.DriverMysql.HandleSqlBeforeExec(sql) return d.DriverMysql.HandleSqlBeforeExec(sql)
} }
func init() { func init() {
// It here registers my custom driver in package initialization function "init".
// You can later using this type in the configuration.
gdb.Register(customDriverName, &MyDriver{}) gdb.Register(customDriverName, &MyDriver{})
} }
@ -56,10 +63,10 @@ func Test_Custom_Driver(t *testing.T) {
Role: "master", Role: "master",
Charset: "utf8", Charset: "utf8",
}) })
gtest.Assert(lastSqlString.Val(), "") gtest.Assert(latestSqlString.Val(), "")
sqlString := "select 10000" sqlString := "select 10000"
value, err := g.DB("driver-test").GetValue(sqlString) value, err := g.DB("driver-test").GetValue(sqlString)
gtest.Assert(err, nil) gtest.Assert(err, nil)
gtest.Assert(value, 10000) gtest.Assert(value, 10000)
gtest.Assert(lastSqlString.Val(), sqlString) gtest.Assert(latestSqlString.Val(), sqlString)
} }