2019-03-12 23:26:10 +08:00
|
|
|
// Copyright 2017 gf Author(https://github.com/gogf/gf). 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.
|
|
|
|
|
2020-03-16 22:47:39 +08:00
|
|
|
package gins_test
|
2019-03-12 23:26:10 +08:00
|
|
|
|
|
|
|
import (
|
2020-03-16 22:47:39 +08:00
|
|
|
"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"
|
2019-03-12 23:26:10 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_Database(t *testing.T) {
|
2020-03-16 22:47:39 +08:00
|
|
|
databaseContent := gfile.GetContents(gfile.Join(gdebug.TestDataPath(), "database", "config.toml"))
|
|
|
|
|
|
|
|
var err error
|
|
|
|
dirPath := gfile.Join(gfile.TempDir(), gtime.TimestampNanoStr())
|
|
|
|
err = gfile.Mkdir(dirPath)
|
|
|
|
gtest.Assert(err, nil)
|
|
|
|
defer gfile.Remove(dirPath)
|
|
|
|
|
|
|
|
name := "config.toml"
|
|
|
|
err = gfile.PutContents(gfile.Join(dirPath, name), databaseContent)
|
2019-06-19 09:06:52 +08:00
|
|
|
gtest.Assert(err, nil)
|
2020-03-16 22:47:39 +08:00
|
|
|
|
|
|
|
err = gins.Config().AddPath(dirPath)
|
|
|
|
gtest.Assert(err, nil)
|
|
|
|
|
|
|
|
defer gins.Config().Clear()
|
2019-03-12 23:26:10 +08:00
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
// for gfsnotify callbacks to refresh cache of config file
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
2019-03-14 23:28:56 +08:00
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
gtest.Case(t, func() {
|
2019-11-28 23:19:37 +08:00
|
|
|
//fmt.Println("gins Test_Database", Config().Get("test"))
|
2019-03-14 00:23:46 +08:00
|
|
|
|
2020-03-16 22:47:39 +08:00
|
|
|
dbDefault := gins.Database()
|
|
|
|
dbTest := gins.Database("test")
|
2019-06-19 09:06:52 +08:00
|
|
|
gtest.AssertNE(dbDefault, nil)
|
|
|
|
gtest.AssertNE(dbTest, nil)
|
2019-03-12 23:26:10 +08:00
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
gtest.Assert(dbDefault.PingMaster(), nil)
|
|
|
|
gtest.Assert(dbDefault.PingSlave(), nil)
|
|
|
|
gtest.Assert(dbTest.PingMaster(), nil)
|
|
|
|
gtest.Assert(dbTest.PingSlave(), nil)
|
|
|
|
})
|
2019-03-12 23:26:10 +08:00
|
|
|
}
|