github.com/zhongdalu/gf@v1.0.0/g/os/gcfg/gcfg_z_unit_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 // go test *.go -bench=".*" -benchmem 8 9 package gcfg_test 10 11 import ( 12 "io/ioutil" 13 "os" 14 "testing" 15 16 "github.com/zhongdalu/gf/g" 17 "github.com/zhongdalu/gf/g/encoding/gjson" 18 "github.com/zhongdalu/gf/g/os/gcfg" 19 "github.com/zhongdalu/gf/g/os/gfile" 20 "github.com/zhongdalu/gf/g/test/gtest" 21 ) 22 23 func init() { 24 os.Setenv("GF_GCFG_ERRORPRINT", "false") 25 } 26 27 func Test_Basic1(t *testing.T) { 28 config := ` 29 v1 = 1 30 v2 = "true" 31 v3 = "off" 32 v4 = "1.23" 33 array = [1,2,3] 34 [redis] 35 disk = "127.0.0.1:6379,0" 36 cache = "127.0.0.1:6379,1" 37 ` 38 gtest.Case(t, func() { 39 path := gcfg.DEFAULT_CONFIG_FILE 40 err := gfile.PutContents(path, config) 41 gtest.Assert(err, nil) 42 defer func() { 43 _ = gfile.Remove(path) 44 }() 45 46 c := gcfg.New() 47 gtest.Assert(c.Get("v1"), 1) 48 gtest.AssertEQ(c.GetInt("v1"), 1) 49 gtest.AssertEQ(c.GetInt8("v1"), int8(1)) 50 gtest.AssertEQ(c.GetInt16("v1"), int16(1)) 51 gtest.AssertEQ(c.GetInt32("v1"), int32(1)) 52 gtest.AssertEQ(c.GetInt64("v1"), int64(1)) 53 gtest.AssertEQ(c.GetUint("v1"), uint(1)) 54 gtest.AssertEQ(c.GetUint8("v1"), uint8(1)) 55 gtest.AssertEQ(c.GetUint16("v1"), uint16(1)) 56 gtest.AssertEQ(c.GetUint32("v1"), uint32(1)) 57 gtest.AssertEQ(c.GetUint64("v1"), uint64(1)) 58 59 gtest.AssertEQ(c.GetVar("v1").String(), "1") 60 gtest.AssertEQ(c.GetVar("v1").Bool(), true) 61 gtest.AssertEQ(c.GetVar("v2").String(), "true") 62 gtest.AssertEQ(c.GetVar("v2").Bool(), true) 63 64 gtest.AssertEQ(c.GetString("v1"), "1") 65 gtest.AssertEQ(c.GetFloat32("v4"), float32(1.23)) 66 gtest.AssertEQ(c.GetFloat64("v4"), float64(1.23)) 67 gtest.AssertEQ(c.GetString("v2"), "true") 68 gtest.AssertEQ(c.GetBool("v2"), true) 69 gtest.AssertEQ(c.GetBool("v3"), false) 70 71 gtest.AssertEQ(c.Contains("v1"), true) 72 gtest.AssertEQ(c.Contains("v2"), true) 73 gtest.AssertEQ(c.Contains("v3"), true) 74 gtest.AssertEQ(c.Contains("v4"), true) 75 gtest.AssertEQ(c.Contains("v5"), false) 76 77 gtest.AssertEQ(c.GetInts("array"), []int{1, 2, 3}) 78 gtest.AssertEQ(c.GetStrings("array"), []string{"1", "2", "3"}) 79 gtest.AssertEQ(c.GetArray("array"), []interface{}{1, 2, 3}) 80 gtest.AssertEQ(c.GetInterfaces("array"), []interface{}{1, 2, 3}) 81 gtest.AssertEQ(c.GetMap("redis"), map[string]interface{}{ 82 "disk": "127.0.0.1:6379,0", 83 "cache": "127.0.0.1:6379,1", 84 }) 85 gtest.AssertEQ(c.FilePath(), gfile.Pwd()+gfile.Separator+path) 86 87 }) 88 } 89 90 func Test_Basic2(t *testing.T) { 91 config := `log-path = "logs"` 92 gtest.Case(t, func() { 93 path := gcfg.DEFAULT_CONFIG_FILE 94 err := gfile.PutContents(path, config) 95 gtest.Assert(err, nil) 96 defer func() { 97 _ = gfile.Remove(path) 98 }() 99 100 c := gcfg.New() 101 gtest.Assert(c.Get("log-path"), "logs") 102 }) 103 } 104 105 func Test_Content(t *testing.T) { 106 content := ` 107 v1 = 1 108 v2 = "true" 109 v3 = "off" 110 v4 = "1.23" 111 array = [1,2,3] 112 [redis] 113 disk = "127.0.0.1:6379,0" 114 cache = "127.0.0.1:6379,1" 115 ` 116 gcfg.SetContent(content) 117 defer gcfg.ClearContent() 118 119 gtest.Case(t, func() { 120 c := gcfg.New() 121 gtest.Assert(c.Get("v1"), 1) 122 gtest.AssertEQ(c.GetInt("v1"), 1) 123 gtest.AssertEQ(c.GetInt8("v1"), int8(1)) 124 gtest.AssertEQ(c.GetInt16("v1"), int16(1)) 125 gtest.AssertEQ(c.GetInt32("v1"), int32(1)) 126 gtest.AssertEQ(c.GetInt64("v1"), int64(1)) 127 gtest.AssertEQ(c.GetUint("v1"), uint(1)) 128 gtest.AssertEQ(c.GetUint8("v1"), uint8(1)) 129 gtest.AssertEQ(c.GetUint16("v1"), uint16(1)) 130 gtest.AssertEQ(c.GetUint32("v1"), uint32(1)) 131 gtest.AssertEQ(c.GetUint64("v1"), uint64(1)) 132 133 gtest.AssertEQ(c.GetVar("v1").String(), "1") 134 gtest.AssertEQ(c.GetVar("v1").Bool(), true) 135 gtest.AssertEQ(c.GetVar("v2").String(), "true") 136 gtest.AssertEQ(c.GetVar("v2").Bool(), true) 137 138 gtest.AssertEQ(c.GetString("v1"), "1") 139 gtest.AssertEQ(c.GetFloat32("v4"), float32(1.23)) 140 gtest.AssertEQ(c.GetFloat64("v4"), float64(1.23)) 141 gtest.AssertEQ(c.GetString("v2"), "true") 142 gtest.AssertEQ(c.GetBool("v2"), true) 143 gtest.AssertEQ(c.GetBool("v3"), false) 144 145 gtest.AssertEQ(c.Contains("v1"), true) 146 gtest.AssertEQ(c.Contains("v2"), true) 147 gtest.AssertEQ(c.Contains("v3"), true) 148 gtest.AssertEQ(c.Contains("v4"), true) 149 gtest.AssertEQ(c.Contains("v5"), false) 150 151 gtest.AssertEQ(c.GetInts("array"), []int{1, 2, 3}) 152 gtest.AssertEQ(c.GetStrings("array"), []string{"1", "2", "3"}) 153 gtest.AssertEQ(c.GetArray("array"), []interface{}{1, 2, 3}) 154 gtest.AssertEQ(c.GetInterfaces("array"), []interface{}{1, 2, 3}) 155 gtest.AssertEQ(c.GetMap("redis"), map[string]interface{}{ 156 "disk": "127.0.0.1:6379,0", 157 "cache": "127.0.0.1:6379,1", 158 }) 159 }) 160 } 161 162 func Test_SetFileName(t *testing.T) { 163 config := ` 164 { 165 "array": [ 166 1, 167 2, 168 3 169 ], 170 "redis": { 171 "cache": "127.0.0.1:6379,1", 172 "disk": "127.0.0.1:6379,0" 173 }, 174 "v1": 1, 175 "v2": "true", 176 "v3": "off", 177 "v4": "1.234" 178 } 179 ` 180 gtest.Case(t, func() { 181 path := "config.json" 182 err := gfile.PutContents(path, config) 183 gtest.Assert(err, nil) 184 defer func() { 185 _ = gfile.Remove(path) 186 }() 187 188 c := gcfg.New() 189 c.SetFileName(path) 190 gtest.Assert(c.Get("v1"), 1) 191 gtest.AssertEQ(c.GetInt("v1"), 1) 192 gtest.AssertEQ(c.GetInt8("v1"), int8(1)) 193 gtest.AssertEQ(c.GetInt16("v1"), int16(1)) 194 gtest.AssertEQ(c.GetInt32("v1"), int32(1)) 195 gtest.AssertEQ(c.GetInt64("v1"), int64(1)) 196 gtest.AssertEQ(c.GetUint("v1"), uint(1)) 197 gtest.AssertEQ(c.GetUint8("v1"), uint8(1)) 198 gtest.AssertEQ(c.GetUint16("v1"), uint16(1)) 199 gtest.AssertEQ(c.GetUint32("v1"), uint32(1)) 200 gtest.AssertEQ(c.GetUint64("v1"), uint64(1)) 201 202 gtest.AssertEQ(c.GetVar("v1").String(), "1") 203 gtest.AssertEQ(c.GetVar("v1").Bool(), true) 204 gtest.AssertEQ(c.GetVar("v2").String(), "true") 205 gtest.AssertEQ(c.GetVar("v2").Bool(), true) 206 207 gtest.AssertEQ(c.GetString("v1"), "1") 208 gtest.AssertEQ(c.GetFloat32("v4"), float32(1.234)) 209 gtest.AssertEQ(c.GetFloat64("v4"), float64(1.234)) 210 gtest.AssertEQ(c.GetString("v2"), "true") 211 gtest.AssertEQ(c.GetBool("v2"), true) 212 gtest.AssertEQ(c.GetBool("v3"), false) 213 214 gtest.AssertEQ(c.Contains("v1"), true) 215 gtest.AssertEQ(c.Contains("v2"), true) 216 gtest.AssertEQ(c.Contains("v3"), true) 217 gtest.AssertEQ(c.Contains("v4"), true) 218 gtest.AssertEQ(c.Contains("v5"), false) 219 220 gtest.AssertEQ(c.GetInts("array"), []int{1, 2, 3}) 221 gtest.AssertEQ(c.GetStrings("array"), []string{"1", "2", "3"}) 222 gtest.AssertEQ(c.GetArray("array"), []interface{}{1, 2, 3}) 223 gtest.AssertEQ(c.GetInterfaces("array"), []interface{}{1, 2, 3}) 224 gtest.AssertEQ(c.GetMap("redis"), map[string]interface{}{ 225 "disk": "127.0.0.1:6379,0", 226 "cache": "127.0.0.1:6379,1", 227 }) 228 gtest.AssertEQ(c.FilePath(), gfile.Pwd()+gfile.Separator+path) 229 230 }) 231 } 232 233 func Test_Instance(t *testing.T) { 234 config := ` 235 { 236 "array": [ 237 1, 238 2, 239 3 240 ], 241 "redis": { 242 "cache": "127.0.0.1:6379,1", 243 "disk": "127.0.0.1:6379,0" 244 }, 245 "v1": 1, 246 "v2": "true", 247 "v3": "off", 248 "v4": "1.234" 249 } 250 ` 251 gtest.Case(t, func() { 252 path := gcfg.DEFAULT_CONFIG_FILE 253 err := gfile.PutContents(path, config) 254 gtest.Assert(err, nil) 255 defer func() { 256 gtest.Assert(gfile.Remove(path), nil) 257 }() 258 259 c := gcfg.Instance() 260 gtest.Assert(c.Get("v1"), 1) 261 gtest.AssertEQ(c.GetInt("v1"), 1) 262 gtest.AssertEQ(c.GetInt8("v1"), int8(1)) 263 gtest.AssertEQ(c.GetInt16("v1"), int16(1)) 264 gtest.AssertEQ(c.GetInt32("v1"), int32(1)) 265 gtest.AssertEQ(c.GetInt64("v1"), int64(1)) 266 gtest.AssertEQ(c.GetUint("v1"), uint(1)) 267 gtest.AssertEQ(c.GetUint8("v1"), uint8(1)) 268 gtest.AssertEQ(c.GetUint16("v1"), uint16(1)) 269 gtest.AssertEQ(c.GetUint32("v1"), uint32(1)) 270 gtest.AssertEQ(c.GetUint64("v1"), uint64(1)) 271 272 gtest.AssertEQ(c.GetVar("v1").String(), "1") 273 gtest.AssertEQ(c.GetVar("v1").Bool(), true) 274 gtest.AssertEQ(c.GetVar("v2").String(), "true") 275 gtest.AssertEQ(c.GetVar("v2").Bool(), true) 276 277 gtest.AssertEQ(c.GetString("v1"), "1") 278 gtest.AssertEQ(c.GetFloat32("v4"), float32(1.234)) 279 gtest.AssertEQ(c.GetFloat64("v4"), float64(1.234)) 280 gtest.AssertEQ(c.GetString("v2"), "true") 281 gtest.AssertEQ(c.GetBool("v2"), true) 282 gtest.AssertEQ(c.GetBool("v3"), false) 283 284 gtest.AssertEQ(c.Contains("v1"), true) 285 gtest.AssertEQ(c.Contains("v2"), true) 286 gtest.AssertEQ(c.Contains("v3"), true) 287 gtest.AssertEQ(c.Contains("v4"), true) 288 gtest.AssertEQ(c.Contains("v5"), false) 289 290 gtest.AssertEQ(c.GetInts("array"), []int{1, 2, 3}) 291 gtest.AssertEQ(c.GetStrings("array"), []string{"1", "2", "3"}) 292 gtest.AssertEQ(c.GetArray("array"), []interface{}{1, 2, 3}) 293 gtest.AssertEQ(c.GetInterfaces("array"), []interface{}{1, 2, 3}) 294 gtest.AssertEQ(c.GetMap("redis"), map[string]interface{}{ 295 "disk": "127.0.0.1:6379,0", 296 "cache": "127.0.0.1:6379,1", 297 }) 298 gtest.AssertEQ(c.FilePath(), gfile.Pwd()+gfile.Separator+path) 299 300 }) 301 } 302 303 func TestCfg_New(t *testing.T) { 304 gtest.Case(t, func() { 305 os.Setenv("GF_GCFG_PATH", "config") 306 c := gcfg.New("config.yml") 307 gtest.Assert(c.Get("name"), nil) 308 gtest.Assert(c.GetFileName(), "config.yml") 309 310 configPath := gfile.Pwd() + gfile.Separator + "config" 311 _ = gfile.Mkdir(configPath) 312 defer func() { 313 _ = gfile.Remove(configPath) 314 }() 315 c = gcfg.New("config.yml") 316 gtest.Assert(c.Get("name"), nil) 317 318 _ = os.Unsetenv("GF_GCFG_PATH") 319 c = gcfg.New("config.yml") 320 gtest.Assert(c.Get("name"), nil) 321 }) 322 } 323 324 func TestCfg_SetPath(t *testing.T) { 325 gtest.Case(t, func() { 326 c := gcfg.New("config.yml") 327 err := c.SetPath("tmp") 328 gtest.AssertNE(err, nil) 329 err = c.SetPath("gcfg.go") 330 gtest.AssertNE(err, nil) 331 gtest.Assert(c.Get("name"), nil) 332 }) 333 } 334 335 func TestCfg_SetViolenceCheck(t *testing.T) { 336 gtest.Case(t, func() { 337 c := gcfg.New("config.yml") 338 c.SetViolenceCheck(true) 339 gtest.Assert(c.Get("name"), nil) 340 }) 341 } 342 343 func TestCfg_AddPath(t *testing.T) { 344 gtest.Case(t, func() { 345 c := gcfg.New("config.yml") 346 err := c.AddPath("tmp") 347 gtest.AssertNE(err, nil) 348 err = c.AddPath("gcfg.go") 349 gtest.AssertNE(err, nil) 350 gtest.Assert(c.Get("name"), nil) 351 }) 352 } 353 354 func TestCfg_FilePath(t *testing.T) { 355 gtest.Case(t, func() { 356 c := gcfg.New("config.yml") 357 path := c.FilePath("tmp") 358 gtest.Assert(path, "") 359 path = c.GetFilePath("tmp") 360 gtest.Assert(path, "") 361 }) 362 } 363 364 func TestCfg_Get(t *testing.T) { 365 gtest.Case(t, func() { 366 configPath := gfile.Pwd() + gfile.Separator + "config" 367 _ = gfile.Mkdir(configPath) 368 defer func() { 369 _ = gfile.Remove(configPath) 370 }() 371 _ = ioutil.WriteFile(configPath+gfile.Separator+"config.yml", []byte("wrong config"), 0644) 372 c := gcfg.New("config.yml") 373 gtest.Assert(c.Get("name"), nil) 374 gtest.Assert(c.GetVar("name").Val(), nil) 375 gtest.Assert(c.Contains("name"), false) 376 gtest.Assert(c.GetMap("name"), nil) 377 gtest.Assert(c.GetArray("name"), nil) 378 gtest.Assert(c.GetString("name"), "") 379 gtest.Assert(c.GetStrings("name"), nil) 380 gtest.Assert(c.GetInterfaces("name"), nil) 381 gtest.Assert(c.GetBool("name"), false) 382 gtest.Assert(c.GetFloat32("name"), 0) 383 gtest.Assert(c.GetFloat64("name"), 0) 384 gtest.Assert(c.GetFloats("name"), nil) 385 gtest.Assert(c.GetInt("name"), 0) 386 gtest.Assert(c.GetInt8("name"), 0) 387 gtest.Assert(c.GetInt16("name"), 0) 388 gtest.Assert(c.GetInt32("name"), 0) 389 gtest.Assert(c.GetInt64("name"), 0) 390 gtest.Assert(c.GetInts("name"), nil) 391 gtest.Assert(c.GetUint("name"), 0) 392 gtest.Assert(c.GetUint8("name"), 0) 393 gtest.Assert(c.GetUint16("name"), 0) 394 gtest.Assert(c.GetUint32("name"), 0) 395 gtest.Assert(c.GetUint64("name"), 0) 396 gtest.Assert(c.GetTime("name").Format("2006-01-02"), "0001-01-01") 397 gtest.Assert(c.GetGTime("name"), nil) 398 gtest.Assert(c.GetDuration("name").String(), "0s") 399 name := struct { 400 Name string 401 }{} 402 gtest.Assert(c.GetToStruct("name", &name) == nil, false) 403 404 c.Reload() 405 c.Clear() 406 407 arr, _ := gjson.Encode(g.Map{"name": "gf", "time": "2019-06-12", "person": g.Map{"name": "gf"}, "floats": g.Slice{1, 2, 3}}) 408 _ = ioutil.WriteFile(configPath+gfile.Separator+"config.yml", arr, 0644) 409 gtest.Assert(c.GetTime("time").Format("2006-01-02"), "2019-06-12") 410 gtest.Assert(c.GetGTime("time").Format("Y-m-d"), "2019-06-12") 411 gtest.Assert(c.GetDuration("time").String(), "0s") 412 //t.Log(c.GetString("person")) 413 err := c.GetToStruct("person", &name) 414 gtest.Assert(err, nil) 415 gtest.Assert(name.Name, "gf") 416 gtest.Assert(c.GetFloats("floats") == nil, false) 417 }) 418 } 419 420 func TestCfg_Instance(t *testing.T) { 421 gtest.Case(t, func() { 422 gtest.Assert(gcfg.Instance("gf") != nil, true) 423 }) 424 } 425 426 func TestCfg_Config(t *testing.T) { 427 gtest.Case(t, func() { 428 gcfg.SetContent("gf", "config.yml") 429 gtest.Assert(gcfg.GetContent("config.yml"), "gf") 430 gcfg.SetContent("gf1", "config.yml") 431 gtest.Assert(gcfg.GetContent("config.yml"), "gf1") 432 gcfg.RemoveConfig("config.yml") 433 gcfg.ClearContent() 434 gtest.Assert(gcfg.GetContent("name"), "") 435 }) 436 }