2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2020-11-29 23:47:57 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
2022-05-10 15:38:08 +08:00
|
|
|
package mysql_test
|
2020-11-29 23:47:57 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/database/gdb"
|
2022-07-07 21:16:26 +08:00
|
|
|
"github.com/gogf/gf/v2/os/glog"
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/test/gtest"
|
2020-11-29 23:47:57 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_Ctx(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
db, err := gdb.Instance()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-11-29 23:47:57 +08:00
|
|
|
|
|
|
|
err1 := db.PingMaster()
|
|
|
|
err2 := db.PingSlave()
|
|
|
|
t.Assert(err1, nil)
|
|
|
|
t.Assert(err2, nil)
|
|
|
|
|
|
|
|
newDb := db.Ctx(context.Background())
|
|
|
|
t.AssertNE(newDb, nil)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Ctx_Query(t *testing.T) {
|
2022-07-07 21:16:26 +08:00
|
|
|
db.GetLogger().(*glog.Logger).SetCtxKeys("SpanId", "TraceId")
|
2020-11-29 23:47:57 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
db.SetDebug(true)
|
|
|
|
defer db.SetDebug(false)
|
|
|
|
ctx := context.WithValue(context.Background(), "TraceId", "12345678")
|
|
|
|
ctx = context.WithValue(ctx, "SpanId", "0.1")
|
2021-09-29 20:39:02 +08:00
|
|
|
db.Query(ctx, "select 1")
|
2020-11-29 23:47:57 +08:00
|
|
|
})
|
2020-11-29 23:50:16 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
db.SetDebug(true)
|
|
|
|
defer db.SetDebug(false)
|
2021-09-29 20:39:02 +08:00
|
|
|
db.Query(ctx, "select 2")
|
2020-11-29 23:50:16 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Ctx_Model(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
2022-07-07 21:16:26 +08:00
|
|
|
db.GetLogger().(*glog.Logger).SetCtxKeys("SpanId", "TraceId")
|
2020-11-29 23:50:16 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
db.SetDebug(true)
|
|
|
|
defer db.SetDebug(false)
|
|
|
|
ctx := context.WithValue(context.Background(), "TraceId", "12345678")
|
|
|
|
ctx = context.WithValue(ctx, "SpanId", "0.1")
|
|
|
|
db.Model(table).Ctx(ctx).All()
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
db.SetDebug(true)
|
|
|
|
defer db.SetDebug(false)
|
|
|
|
db.Model(table).All()
|
|
|
|
})
|
2020-11-29 23:47:57 +08:00
|
|
|
}
|