github.com/gogf/gf/v2@v2.7.4/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  	"testing"
    11  	"time"
    12  
    13  	"github.com/gogf/gf/v2/frame/gins"
    14  	"github.com/gogf/gf/v2/os/gcfg"
    15  	"github.com/gogf/gf/v2/os/gfile"
    16  	"github.com/gogf/gf/v2/os/gtime"
    17  	"github.com/gogf/gf/v2/test/gtest"
    18  )
    19  
    20  func Test_Database(t *testing.T) {
    21  	databaseContent := gfile.GetContents(
    22  		gtest.DataPath("database", "config.toml"),
    23  	)
    24  	gtest.C(t, func(t *gtest.T) {
    25  		var err error
    26  		dirPath := gfile.Temp(gtime.TimestampNanoStr())
    27  		err = gfile.Mkdir(dirPath)
    28  		t.AssertNil(err)
    29  		defer gfile.Remove(dirPath)
    30  
    31  		name := "config.toml"
    32  		err = gfile.PutContents(gfile.Join(dirPath, name), databaseContent)
    33  		t.AssertNil(err)
    34  
    35  		err = gins.Config().GetAdapter().(*gcfg.AdapterFile).AddPath(dirPath)
    36  		t.AssertNil(err)
    37  
    38  		defer gins.Config().GetAdapter().(*gcfg.AdapterFile).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  		var (
    45  			db        = gins.Database()
    46  			dbDefault = gins.Database("default")
    47  		)
    48  		t.AssertNE(db, nil)
    49  		t.AssertNE(dbDefault, nil)
    50  
    51  		t.Assert(db.PingMaster(), nil)
    52  		t.Assert(db.PingSlave(), nil)
    53  		t.Assert(dbDefault.PingMaster(), nil)
    54  		t.Assert(dbDefault.PingSlave(), nil)
    55  	})
    56  }