gf/frame/gins/gins_z_unit_database_test.go

56 lines
1.4 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// 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.
package gins_test
import (
"github.com/gogf/gf/debug/gdebug"
"github.com/gogf/gf/frame/gins"
"github.com/gogf/gf/os/gtime"
2019-07-06 16:14:45 +08:00
"testing"
"time"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/os/gfile"
"github.com/gogf/gf/test/gtest"
)
func Test_Database(t *testing.T) {
2020-03-20 08:56:17 +08:00
databaseContent := gfile.GetContents(
gdebug.TestDataPath("database", "config.toml"),
2020-03-20 08:56:17 +08:00
)
2020-03-19 23:53:03 +08:00
gtest.C(t, func(t *gtest.T) {
var err error
dirPath := gfile.TempDir(gtime.TimestampNanoStr())
2020-03-19 23:53:03 +08:00
err = gfile.Mkdir(dirPath)
t.Assert(err, nil)
defer gfile.Remove(dirPath)
name := "config.toml"
err = gfile.PutContents(gfile.Join(dirPath, name), databaseContent)
t.Assert(err, nil)
err = gins.Config().AddPath(dirPath)
t.Assert(err, nil)
2020-03-20 08:56:17 +08:00
defer gins.Config().Clear()
2020-03-20 08:56:17 +08:00
// for gfsnotify callbacks to refresh cache of config file
time.Sleep(500 * time.Millisecond)
2019-03-14 23:28:56 +08:00
//fmt.Println("gins Test_Database", Config().Get("test"))
dbDefault := gins.Database()
dbTest := gins.Database("test")
2020-03-19 22:56:12 +08:00
t.AssertNE(dbDefault, nil)
t.AssertNE(dbTest, nil)
2020-03-19 22:56:12 +08:00
t.Assert(dbDefault.PingMaster(), nil)
t.Assert(dbDefault.PingSlave(), nil)
t.Assert(dbTest.PingMaster(), nil)
t.Assert(dbTest.PingSlave(), nil)
2019-06-19 09:06:52 +08:00
})
}