github.com/gogf/gf@v1.16.9/frame/gins/gins_z_unit_database_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gins_test 8 9 import ( 10 "github.com/gogf/gf/debug/gdebug" 11 "github.com/gogf/gf/frame/gins" 12 "github.com/gogf/gf/os/gtime" 13 "testing" 14 "time" 15 16 "github.com/gogf/gf/os/gfile" 17 "github.com/gogf/gf/test/gtest" 18 ) 19 20 func Test_Database(t *testing.T) { 21 databaseContent := gfile.GetContents( 22 gdebug.TestDataPath("database", "config.toml"), 23 ) 24 gtest.C(t, func(t *gtest.T) { 25 var err error 26 dirPath := gfile.TempDir(gtime.TimestampNanoStr()) 27 err = gfile.Mkdir(dirPath) 28 t.Assert(err, nil) 29 defer gfile.Remove(dirPath) 30 31 name := "config.toml" 32 err = gfile.PutContents(gfile.Join(dirPath, name), databaseContent) 33 t.Assert(err, nil) 34 35 err = gins.Config().AddPath(dirPath) 36 t.Assert(err, nil) 37 38 defer gins.Config().Clear() 39 40 // for gfsnotify callbacks to refresh cache of config file 41 time.Sleep(500 * time.Millisecond) 42 43 //fmt.Println("gins Test_Database", Config().Get("test")) 44 45 dbDefault := gins.Database() 46 dbTest := gins.Database("test") 47 t.AssertNE(dbDefault, nil) 48 t.AssertNE(dbTest, nil) 49 50 t.Assert(dbDefault.PingMaster(), nil) 51 t.Assert(dbDefault.PingSlave(), nil) 52 t.Assert(dbTest.PingMaster(), nil) 53 t.Assert(dbTest.PingSlave(), nil) 54 }) 55 }