mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
add example/unit testing cases for package gdb
This commit is contained in:
@ -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]
|
||||
|
||||
14
.example/database/gdb/mysql/gdb_ctx.go
Normal file
14
.example/database/gdb/mysql/gdb_ctx.go
Normal 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)
|
||||
}
|
||||
}
|
||||
14
.example/database/gdb/mysql/gdb_ctx_model.go
Normal file
14
.example/database/gdb/mysql/gdb_ctx_model.go
Normal 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)
|
||||
}
|
||||
}
|
||||
@ -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)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user