gf/g/os/gcfg/gcfg_z_unit_test.go

411 lines
12 KiB
Go
Raw Normal View History

2019-03-19 13:58:18 +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.
// go test *.go -bench=".*" -benchmem
package gcfg_test
import (
"github.com/gogf/gf/g"
"github.com/gogf/gf/g/encoding/gjson"
"github.com/gogf/gf/g/os/gcfg"
"github.com/gogf/gf/g/os/gfile"
"github.com/gogf/gf/g/test/gtest"
"io/ioutil"
"os"
"testing"
2019-03-19 13:58:18 +08:00
)
func init() {
os.Setenv("GF_GCFG_ERRORPRINT", "false")
}
2019-03-19 13:58:18 +08:00
func Test_Basic(t *testing.T) {
config := `
2019-03-19 13:58:18 +08:00
v1 = 1
v2 = "true"
v3 = "off"
v4 = "1.23"
array = [1,2,3]
[redis]
disk = "127.0.0.1:6379,0"
cache = "127.0.0.1:6379,1"
`
gtest.Case(t, func() {
path := gcfg.DEFAULT_CONFIG_FILE
err := gfile.PutContents(path, config)
gtest.Assert(err, nil)
defer gfile.Remove(path)
c := gcfg.New()
gtest.Assert(c.Get("v1"), 1)
gtest.AssertEQ(c.GetInt("v1"), 1)
gtest.AssertEQ(c.GetInt8("v1"), int8(1))
gtest.AssertEQ(c.GetInt16("v1"), int16(1))
gtest.AssertEQ(c.GetInt32("v1"), int32(1))
gtest.AssertEQ(c.GetInt64("v1"), int64(1))
gtest.AssertEQ(c.GetUint("v1"), uint(1))
gtest.AssertEQ(c.GetUint8("v1"), uint8(1))
gtest.AssertEQ(c.GetUint16("v1"), uint16(1))
gtest.AssertEQ(c.GetUint32("v1"), uint32(1))
gtest.AssertEQ(c.GetUint64("v1"), uint64(1))
gtest.AssertEQ(c.GetVar("v1").String(), "1")
gtest.AssertEQ(c.GetVar("v1").Bool(), true)
gtest.AssertEQ(c.GetVar("v2").String(), "true")
gtest.AssertEQ(c.GetVar("v2").Bool(), true)
gtest.AssertEQ(c.GetString("v1"), "1")
gtest.AssertEQ(c.GetFloat32("v4"), float32(1.23))
gtest.AssertEQ(c.GetFloat64("v4"), float64(1.23))
gtest.AssertEQ(c.GetString("v2"), "true")
gtest.AssertEQ(c.GetBool("v2"), true)
gtest.AssertEQ(c.GetBool("v3"), false)
gtest.AssertEQ(c.Contains("v1"), true)
gtest.AssertEQ(c.Contains("v2"), true)
gtest.AssertEQ(c.Contains("v3"), true)
gtest.AssertEQ(c.Contains("v4"), true)
gtest.AssertEQ(c.Contains("v5"), false)
gtest.AssertEQ(c.GetInts("array"), []int{1, 2, 3})
gtest.AssertEQ(c.GetStrings("array"), []string{"1", "2", "3"})
gtest.AssertEQ(c.GetArray("array"), []interface{}{"1", "2", "3"})
gtest.AssertEQ(c.GetInterfaces("array"), []interface{}{"1", "2", "3"})
gtest.AssertEQ(c.GetMap("redis"), map[string]interface{}{
"disk": "127.0.0.1:6379,0",
"cache": "127.0.0.1:6379,1",
})
gtest.AssertEQ(c.FilePath(), gfile.Pwd()+gfile.Separator+path)
})
2019-03-19 13:58:18 +08:00
}
func Test_Content(t *testing.T) {
content := `
v1 = 1
v2 = "true"
v3 = "off"
v4 = "1.23"
array = [1,2,3]
[redis]
disk = "127.0.0.1:6379,0"
cache = "127.0.0.1:6379,1"
`
gcfg.SetContent(content)
defer gcfg.ClearContent()
gtest.Case(t, func() {
c := gcfg.New()
gtest.Assert(c.Get("v1"), 1)
gtest.AssertEQ(c.GetInt("v1"), 1)
gtest.AssertEQ(c.GetInt8("v1"), int8(1))
gtest.AssertEQ(c.GetInt16("v1"), int16(1))
gtest.AssertEQ(c.GetInt32("v1"), int32(1))
gtest.AssertEQ(c.GetInt64("v1"), int64(1))
gtest.AssertEQ(c.GetUint("v1"), uint(1))
gtest.AssertEQ(c.GetUint8("v1"), uint8(1))
gtest.AssertEQ(c.GetUint16("v1"), uint16(1))
gtest.AssertEQ(c.GetUint32("v1"), uint32(1))
gtest.AssertEQ(c.GetUint64("v1"), uint64(1))
gtest.AssertEQ(c.GetVar("v1").String(), "1")
gtest.AssertEQ(c.GetVar("v1").Bool(), true)
gtest.AssertEQ(c.GetVar("v2").String(), "true")
gtest.AssertEQ(c.GetVar("v2").Bool(), true)
gtest.AssertEQ(c.GetString("v1"), "1")
gtest.AssertEQ(c.GetFloat32("v4"), float32(1.23))
gtest.AssertEQ(c.GetFloat64("v4"), float64(1.23))
gtest.AssertEQ(c.GetString("v2"), "true")
gtest.AssertEQ(c.GetBool("v2"), true)
gtest.AssertEQ(c.GetBool("v3"), false)
gtest.AssertEQ(c.Contains("v1"), true)
gtest.AssertEQ(c.Contains("v2"), true)
gtest.AssertEQ(c.Contains("v3"), true)
gtest.AssertEQ(c.Contains("v4"), true)
gtest.AssertEQ(c.Contains("v5"), false)
gtest.AssertEQ(c.GetInts("array"), []int{1, 2, 3})
gtest.AssertEQ(c.GetStrings("array"), []string{"1", "2", "3"})
gtest.AssertEQ(c.GetArray("array"), []interface{}{"1", "2", "3"})
gtest.AssertEQ(c.GetInterfaces("array"), []interface{}{"1", "2", "3"})
gtest.AssertEQ(c.GetMap("redis"), map[string]interface{}{
"disk": "127.0.0.1:6379,0",
"cache": "127.0.0.1:6379,1",
})
})
2019-04-03 09:59:15 +08:00
}
func Test_SetFileName(t *testing.T) {
config := `
2019-04-03 09:59:15 +08:00
{
"array": [
1,
2,
3
],
"redis": {
"cache": "127.0.0.1:6379,1",
"disk": "127.0.0.1:6379,0"
},
"v1": 1,
"v2": "true",
"v3": "off",
"v4": "1.234"
}
`
gtest.Case(t, func() {
path := "config.json"
err := gfile.PutContents(path, config)
gtest.Assert(err, nil)
defer gfile.Remove(path)
c := gcfg.New()
c.SetFileName(path)
gtest.Assert(c.Get("v1"), 1)
gtest.AssertEQ(c.GetInt("v1"), 1)
gtest.AssertEQ(c.GetInt8("v1"), int8(1))
gtest.AssertEQ(c.GetInt16("v1"), int16(1))
gtest.AssertEQ(c.GetInt32("v1"), int32(1))
gtest.AssertEQ(c.GetInt64("v1"), int64(1))
gtest.AssertEQ(c.GetUint("v1"), uint(1))
gtest.AssertEQ(c.GetUint8("v1"), uint8(1))
gtest.AssertEQ(c.GetUint16("v1"), uint16(1))
gtest.AssertEQ(c.GetUint32("v1"), uint32(1))
gtest.AssertEQ(c.GetUint64("v1"), uint64(1))
gtest.AssertEQ(c.GetVar("v1").String(), "1")
gtest.AssertEQ(c.GetVar("v1").Bool(), true)
gtest.AssertEQ(c.GetVar("v2").String(), "true")
gtest.AssertEQ(c.GetVar("v2").Bool(), true)
gtest.AssertEQ(c.GetString("v1"), "1")
gtest.AssertEQ(c.GetFloat32("v4"), float32(1.234))
gtest.AssertEQ(c.GetFloat64("v4"), float64(1.234))
gtest.AssertEQ(c.GetString("v2"), "true")
gtest.AssertEQ(c.GetBool("v2"), true)
gtest.AssertEQ(c.GetBool("v3"), false)
gtest.AssertEQ(c.Contains("v1"), true)
gtest.AssertEQ(c.Contains("v2"), true)
gtest.AssertEQ(c.Contains("v3"), true)
gtest.AssertEQ(c.Contains("v4"), true)
gtest.AssertEQ(c.Contains("v5"), false)
gtest.AssertEQ(c.GetInts("array"), []int{1, 2, 3})
gtest.AssertEQ(c.GetStrings("array"), []string{"1", "2", "3"})
gtest.AssertEQ(c.GetArray("array"), []interface{}{"1", "2", "3"})
gtest.AssertEQ(c.GetInterfaces("array"), []interface{}{"1", "2", "3"})
gtest.AssertEQ(c.GetMap("redis"), map[string]interface{}{
"disk": "127.0.0.1:6379,0",
"cache": "127.0.0.1:6379,1",
})
gtest.AssertEQ(c.FilePath(), gfile.Pwd()+gfile.Separator+path)
})
2019-04-03 09:59:15 +08:00
}
func Test_Instance(t *testing.T) {
config := `
2019-04-03 09:59:15 +08:00
{
"array": [
1,
2,
3
],
"redis": {
"cache": "127.0.0.1:6379,1",
"disk": "127.0.0.1:6379,0"
},
"v1": 1,
"v2": "true",
"v3": "off",
"v4": "1.234"
}
`
gtest.Case(t, func() {
path := gcfg.DEFAULT_CONFIG_FILE
err := gfile.PutContents(path, config)
gtest.Assert(err, nil)
defer gfile.Remove(path)
c := gcfg.Instance()
gtest.Assert(c.Get("v1"), 1)
gtest.AssertEQ(c.GetInt("v1"), 1)
gtest.AssertEQ(c.GetInt8("v1"), int8(1))
gtest.AssertEQ(c.GetInt16("v1"), int16(1))
gtest.AssertEQ(c.GetInt32("v1"), int32(1))
gtest.AssertEQ(c.GetInt64("v1"), int64(1))
gtest.AssertEQ(c.GetUint("v1"), uint(1))
gtest.AssertEQ(c.GetUint8("v1"), uint8(1))
gtest.AssertEQ(c.GetUint16("v1"), uint16(1))
gtest.AssertEQ(c.GetUint32("v1"), uint32(1))
gtest.AssertEQ(c.GetUint64("v1"), uint64(1))
gtest.AssertEQ(c.GetVar("v1").String(), "1")
gtest.AssertEQ(c.GetVar("v1").Bool(), true)
gtest.AssertEQ(c.GetVar("v2").String(), "true")
gtest.AssertEQ(c.GetVar("v2").Bool(), true)
gtest.AssertEQ(c.GetString("v1"), "1")
gtest.AssertEQ(c.GetFloat32("v4"), float32(1.234))
gtest.AssertEQ(c.GetFloat64("v4"), float64(1.234))
gtest.AssertEQ(c.GetString("v2"), "true")
gtest.AssertEQ(c.GetBool("v2"), true)
gtest.AssertEQ(c.GetBool("v3"), false)
gtest.AssertEQ(c.Contains("v1"), true)
gtest.AssertEQ(c.Contains("v2"), true)
gtest.AssertEQ(c.Contains("v3"), true)
gtest.AssertEQ(c.Contains("v4"), true)
gtest.AssertEQ(c.Contains("v5"), false)
gtest.AssertEQ(c.GetInts("array"), []int{1, 2, 3})
gtest.AssertEQ(c.GetStrings("array"), []string{"1", "2", "3"})
gtest.AssertEQ(c.GetArray("array"), []interface{}{"1", "2", "3"})
gtest.AssertEQ(c.GetInterfaces("array"), []interface{}{"1", "2", "3"})
gtest.AssertEQ(c.GetMap("redis"), map[string]interface{}{
"disk": "127.0.0.1:6379,0",
"cache": "127.0.0.1:6379,1",
})
gtest.AssertEQ(c.FilePath(), gfile.Pwd()+gfile.Separator+path)
})
}
func TestCfg_New(t *testing.T) {
gtest.Case(t, func() {
os.Setenv("GF_GCFG_PATH", "config")
c := gcfg.New("config.yml")
gtest.Assert(c.Get("name"), nil)
gtest.Assert(c.GetFileName(), "config.yml")
configPath := gfile.Pwd() + gfile.Separator + "config"
gfile.Mkdir(configPath)
defer gfile.Remove(configPath)
c = gcfg.New("config.yml")
gtest.Assert(c.Get("name"), nil)
os.Unsetenv("GF_GCFG_PATH")
c = gcfg.New("config.yml")
gtest.Assert(c.Get("name"), nil)
})
}
func TestCfg_SetPath(t *testing.T) {
gtest.Case(t, func() {
c := gcfg.New("config.yml")
err := c.SetPath("tmp")
gtest.AssertNE(err, nil)
err = c.SetPath("gcfg.go")
gtest.AssertNE(err, nil)
gtest.Assert(c.Get("name"), nil)
})
}
func TestCfg_SetViolenceCheck(t *testing.T) {
gtest.Case(t, func() {
c := gcfg.New("config.yml")
c.SetViolenceCheck(true)
gtest.Assert(c.Get("name"), nil)
})
}
func TestCfg_AddPath(t *testing.T) {
gtest.Case(t, func() {
c := gcfg.New("config.yml")
err := c.AddPath("tmp")
gtest.AssertNE(err, nil)
err = c.AddPath("gcfg.go")
gtest.AssertNE(err, nil)
gtest.Assert(c.Get("name"), nil)
})
}
func TestCfg_FilePath(t *testing.T) {
gtest.Case(t, func() {
c := gcfg.New("config.yml")
path := c.FilePath("tmp")
gtest.Assert(path, "")
path = c.GetFilePath("tmp")
gtest.Assert(path, "")
})
}
func TestCfg_Get(t *testing.T) {
gtest.Case(t, func() {
configPath := gfile.Pwd() + gfile.Separator + "config"
gfile.Mkdir(configPath)
defer gfile.Remove(configPath)
ioutil.WriteFile(configPath+gfile.Separator+"config.yml", []byte("wrong config"), 0644)
c := gcfg.New("config.yml")
gtest.Assert(c.Get("name"), nil)
gtest.Assert(c.GetVar("name").Val(), nil)
gtest.Assert(c.Contains("name"), false)
gtest.Assert(c.GetMap("name"), nil)
gtest.Assert(c.GetArray("name"), nil)
gtest.Assert(c.GetString("name"), "")
gtest.Assert(c.GetStrings("name"), nil)
gtest.Assert(c.GetInterfaces("name"), nil)
gtest.Assert(c.GetBool("name"), false)
gtest.Assert(c.GetFloat32("name"), 0)
gtest.Assert(c.GetFloat64("name"), 0)
gtest.Assert(c.GetFloats("name"), nil)
gtest.Assert(c.GetInt("name"), 0)
gtest.Assert(c.GetInt8("name"), 0)
gtest.Assert(c.GetInt16("name"), 0)
gtest.Assert(c.GetInt32("name"), 0)
gtest.Assert(c.GetInt64("name"), 0)
gtest.Assert(c.GetInts("name"), nil)
gtest.Assert(c.GetUint("name"), 0)
gtest.Assert(c.GetUint8("name"), 0)
gtest.Assert(c.GetUint16("name"), 0)
gtest.Assert(c.GetUint32("name"), 0)
gtest.Assert(c.GetUint64("name"), 0)
gtest.Assert(c.GetTime("name").Format("2006-01-02"), "0001-01-01")
gtest.Assert(c.GetGTime("name"), nil)
gtest.Assert(c.GetDuration("name").String(), "0s")
name := struct {
Name string
}{}
gtest.Assert(c.GetToStruct("name", &name) == nil, false)
c.Reload()
c.Clear()
arr, _ := gjson.Encode(g.Map{"name": "gf", "time": "2019-06-12", "person": g.Map{"name": "gf"}, "floats": g.Slice{1, 2, 3}})
ioutil.WriteFile(configPath+gfile.Separator+"config.yml", arr, 0644)
gtest.Assert(c.GetTime("time").Format("2006-01-02"), "2019-06-12")
gtest.Assert(c.GetGTime("time").Format("Y-m-d"), "2019-06-12")
gtest.Assert(c.GetDuration("time").String(), "0s")
//t.Log(c.GetString("person"))
err := c.GetToStruct("person", &name)
gtest.Assert(err, nil)
gtest.Assert(name.Name, "gf")
gtest.Assert(c.GetFloats("floats") == nil, false)
})
}
func TestCfg_Instance(t *testing.T) {
gtest.Case(t, func() {
gtest.Assert(gcfg.Instance("gf") != nil, true)
})
}
func TestCfg_Config(t *testing.T) {
gtest.Case(t, func() {
gcfg.SetContent("gf", "config.yml")
gtest.Assert(gcfg.GetContent("config.yml"), "gf")
gcfg.SetContent("gf1", "config.yml")
gtest.Assert(gcfg.GetContent("config.yml"), "gf1")
gcfg.RemoveConfig("config.yml")
gcfg.ClearContent()
gtest.Assert(gcfg.GetContent("name"), "")
})
}