add example/unit testing cases for package gdb

This commit is contained in:
John Guo
2020-12-29 00:01:27 +08:00
parent d25a3909d1
commit 4828ddcdd7
4 changed files with 56 additions and 3 deletions

View File

@ -1,9 +1,13 @@
# MySQL.
[database]
debug = true
link = "mysql:root:12345678@tcp(127.0.0.1:3306)/test?parseTime=true"
MaxOpen = 100
[database.logger]
Level = "all"
Stdout = true
CtxKeys = ["Trace-Id"]
[database.default]
link = "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
debug = true
# Redis.
[redis]

View File

@ -0,0 +1,14 @@
package main
import (
"context"
"github.com/gogf/gf/frame/g"
)
func main() {
ctx := context.WithValue(context.Background(), "Trace-Id", "123456789")
_, err := g.DB().Ctx(ctx).Query("SELECT 1")
if err != nil {
panic(err)
}
}

View File

@ -0,0 +1,14 @@
package main
import (
"context"
"github.com/gogf/gf/frame/g"
)
func main() {
ctx := context.WithValue(context.Background(), "Trace-Id", "123456789")
_, err := g.DB().Model("user").Ctx(ctx).All()
if err != nil {
panic(err)
}
}

View File

@ -7,9 +7,11 @@
package gdb_test
import (
"context"
"fmt"
"github.com/gogf/gf/container/garray"
"github.com/gogf/gf/encoding/gparser"
"github.com/gogf/gf/text/gstr"
"testing"
"time"
@ -1468,3 +1470,22 @@ func Test_DB_UpdateCounter(t *testing.T) {
t.Assert(one["views"].Int(), 0)
})
}
func Test_DB_Ctx(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
_, err := db.Ctx(ctx).Query("SELECT SLEEP(10)")
t.Assert(gstr.Contains(err.Error(), "deadline"), true)
})
}
func Test_DB_Ctx_Logger(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
defer db.SetDebug(db.GetDebug())
db.SetDebug(true)
ctx := context.WithValue(context.Background(), "Trace-Id", "123456789")
_, err := db.Ctx(ctx).Query("SELECT 1")
t.Assert(err, nil)
})
}