github.com/zhongdalu/gf@v1.0.0/g/frame/gins/gins_database_test.go (about)

     1  // Copyright 2017 gf Author(https://github.com/zhongdalu/gf). 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/zhongdalu/gf.
     6  
     7  package gins_test
     8  
     9  import (
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/zhongdalu/gf/g/frame/gins"
    14  	"github.com/zhongdalu/gf/g/os/gfile"
    15  	"github.com/zhongdalu/gf/g/test/gtest"
    16  )
    17  
    18  func Test_Database(t *testing.T) {
    19  	config := `
    20  # 模板引擎目录
    21  viewpath = "/home/www/templates/"
    22  test = "v=2"
    23  # MySQL数据库配置
    24  [database]
    25      [[database.default]]
    26          host     = "127.0.0.1"
    27          port     = "3306"
    28          user     = "root"
    29          pass     = ""
    30          # pass     = "12345678"
    31          name     = "test"
    32          type     = "mysql"
    33          role     = "master"
    34  		weight   = "1"
    35          charset  = "utf8"
    36      [[database.test]]
    37          host     = "127.0.0.1"
    38          port     = "3306"
    39          user     = "root"
    40          pass     = ""
    41          # pass     = "12345678"
    42          name     = "test"
    43          type     = "mysql"
    44          role     = "master"
    45  		weight   = "1"
    46          charset  = "utf8"
    47  # Redis数据库配置
    48  [redis]
    49      default = "127.0.0.1:6379,0"
    50      cache = "127.0.0.1:6379,1"
    51  `
    52  	path := "config.toml"
    53  	err := gfile.PutContents(path, config)
    54  	gtest.Assert(err, nil)
    55  	defer gfile.Remove(path)
    56  	defer gins.Config().Clear()
    57  
    58  	// for gfsnotify callbacks to refresh cache of config file
    59  	time.Sleep(500 * time.Millisecond)
    60  
    61  	gtest.Case(t, func() {
    62  		//fmt.Println("gins Test_Database", gins.Config().Get("test"))
    63  
    64  		dbDefault := gins.Database()
    65  		dbTest := gins.Database("test")
    66  		gtest.AssertNE(dbDefault, nil)
    67  		gtest.AssertNE(dbTest, nil)
    68  
    69  		gtest.Assert(dbDefault.PingMaster(), nil)
    70  		gtest.Assert(dbDefault.PingSlave(), nil)
    71  		gtest.Assert(dbTest.PingMaster(), nil)
    72  		gtest.Assert(dbTest.PingSlave(), nil)
    73  	})
    74  }