1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-24 02:40:55 +00:00
Beego/core/config/toml/toml_test.go

380 lines
7.5 KiB
Go
Raw Normal View History

2020-10-11 15:22:19 +00:00
// Copyright 2020
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package toml
import (
"fmt"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/astaxie/beego/core/config"
)
func TestConfig_Parse(t *testing.T) {
// file not found
cfg := &Config{}
_, err := cfg.Parse("invalid_file_name.txt")
assert.NotNil(t, err)
}
func TestConfig_ParseData(t *testing.T) {
data := `
name="Tom"
`
cfg := &Config{}
c, err := cfg.ParseData([]byte(data))
assert.Nil(t, err)
assert.NotNil(t, c)
}
func TestConfigContainer_Bool(t *testing.T) {
data := `
Man=true
Woman="true"
`
cfg := &Config{}
c, err := cfg.ParseData([]byte(data))
assert.Nil(t, err)
assert.NotNil(t, c)
2020-10-13 14:32:41 +00:00
val, err := c.Bool("Man")
2020-10-11 15:22:19 +00:00
assert.Nil(t, err)
assert.True(t, val)
2020-10-13 14:32:41 +00:00
_, err = c.Bool("Woman")
2020-10-11 15:22:19 +00:00
assert.NotNil(t, err)
assert.Equal(t, config.InvalidValueTypeError, err)
}
func TestConfigContainer_DefaultBool(t *testing.T) {
data := `
Man=true
Woman="false"
`
cfg := &Config{}
c, err := cfg.ParseData([]byte(data))
assert.Nil(t, err)
assert.NotNil(t, c)
2020-10-13 14:32:41 +00:00
val := c.DefaultBool("Man11", true)
2020-10-11 15:22:19 +00:00
assert.True(t, val)
2020-10-13 14:32:41 +00:00
val = c.DefaultBool("Man", false)
2020-10-11 15:22:19 +00:00
assert.True(t, val)
2020-10-13 14:32:41 +00:00
val = c.DefaultBool("Woman", true)
2020-10-11 15:22:19 +00:00
assert.True(t, val)
}
func TestConfigContainer_DefaultFloat(t *testing.T) {
data := `
Price=12.3
PriceInvalid="12.3"
`
cfg := &Config{}
c, err := cfg.ParseData([]byte(data))
assert.Nil(t, err)
assert.NotNil(t, c)
2020-10-13 14:32:41 +00:00
val := c.DefaultFloat("Price", 11.2)
2020-10-11 15:22:19 +00:00
assert.Equal(t, 12.3, val)
2020-10-13 14:32:41 +00:00
val = c.DefaultFloat("Price11", 11.2)
2020-10-11 15:22:19 +00:00
assert.Equal(t, 11.2, val)
2020-10-13 14:32:41 +00:00
val = c.DefaultFloat("PriceInvalid", 11.2)
2020-10-11 15:22:19 +00:00
assert.Equal(t, 11.2, val)
}
func TestConfigContainer_DefaultInt(t *testing.T) {
data := `
Age=12
AgeInvalid="13"
`
cfg := &Config{}
c, err := cfg.ParseData([]byte(data))
assert.Nil(t, err)
assert.NotNil(t, c)
2020-10-13 14:32:41 +00:00
val := c.DefaultInt("Age", 11)
2020-10-11 15:22:19 +00:00
assert.Equal(t, 12, val)
2020-10-13 14:32:41 +00:00
val = c.DefaultInt("Price11", 11)
2020-10-11 15:22:19 +00:00
assert.Equal(t, 11, val)
2020-10-13 14:32:41 +00:00
val = c.DefaultInt("PriceInvalid", 11)
2020-10-11 15:22:19 +00:00
assert.Equal(t, 11, val)
}
func TestConfigContainer_DefaultString(t *testing.T) {
data := `
Name="Tom"
NameInvalid=13
`
cfg := &Config{}
c, err := cfg.ParseData([]byte(data))
assert.Nil(t, err)
assert.NotNil(t, c)
2020-10-13 14:32:41 +00:00
val := c.DefaultString("Name", "Jerry")
2020-10-11 15:22:19 +00:00
assert.Equal(t, "Tom", val)
2020-10-13 14:32:41 +00:00
val = c.DefaultString("Name11", "Jerry")
2020-10-11 15:22:19 +00:00
assert.Equal(t, "Jerry", val)
2020-10-13 14:32:41 +00:00
val = c.DefaultString("NameInvalid", "Jerry")
2020-10-11 15:22:19 +00:00
assert.Equal(t, "Jerry", val)
}
func TestConfigContainer_DefaultStrings(t *testing.T) {
data := `
Name=["Tom", "Jerry"]
NameInvalid="Tom"
`
cfg := &Config{}
c, err := cfg.ParseData([]byte(data))
assert.Nil(t, err)
assert.NotNil(t, c)
2020-10-13 14:32:41 +00:00
val := c.DefaultStrings("Name", []string{"Jerry"})
2020-10-11 15:22:19 +00:00
assert.Equal(t, []string{"Tom", "Jerry"}, val)
2020-10-13 14:32:41 +00:00
val = c.DefaultStrings("Name11", []string{"Jerry"})
2020-10-11 15:22:19 +00:00
assert.Equal(t, []string{"Jerry"}, val)
2020-10-13 14:32:41 +00:00
val = c.DefaultStrings("NameInvalid", []string{"Jerry"})
2020-10-11 15:22:19 +00:00
assert.Equal(t, []string{"Jerry"}, val)
}
func TestConfigContainer_DIY(t *testing.T) {
data := `
Name=["Tom", "Jerry"]
`
cfg := &Config{}
c, err := cfg.ParseData([]byte(data))
assert.Nil(t, err)
assert.NotNil(t, c)
2020-10-13 14:32:41 +00:00
_, err = c.DIY("Name")
2020-10-11 15:22:19 +00:00
assert.Nil(t, err)
}
func TestConfigContainer_Float(t *testing.T) {
data := `
Price=12.3
PriceInvalid="12.3"
`
cfg := &Config{}
c, err := cfg.ParseData([]byte(data))
assert.Nil(t, err)
assert.NotNil(t, c)
2020-10-13 14:32:41 +00:00
val, err := c.Float("Price")
2020-10-11 15:22:19 +00:00
assert.Nil(t, err)
assert.Equal(t, 12.3, val)
2020-10-13 14:32:41 +00:00
_, err = c.Float("Price11")
2020-10-11 15:22:19 +00:00
assert.Equal(t, config.KeyNotFoundError, err)
2020-10-13 14:32:41 +00:00
_, err = c.Float("PriceInvalid")
2020-10-11 15:22:19 +00:00
assert.Equal(t, config.InvalidValueTypeError, err)
}
func TestConfigContainer_Int(t *testing.T) {
data := `
Age=12
AgeInvalid="13"
`
cfg := &Config{}
c, err := cfg.ParseData([]byte(data))
assert.Nil(t, err)
assert.NotNil(t, c)
2020-10-13 14:32:41 +00:00
val, err := c.Int("Age")
2020-10-11 15:22:19 +00:00
assert.Nil(t, err)
assert.Equal(t, 12, val)
2020-10-13 14:32:41 +00:00
_, err = c.Int("Age11")
2020-10-11 15:22:19 +00:00
assert.Equal(t, config.KeyNotFoundError, err)
2020-10-13 14:32:41 +00:00
_, err = c.Int("AgeInvalid")
2020-10-11 15:22:19 +00:00
assert.Equal(t, config.InvalidValueTypeError, err)
}
func TestConfigContainer_GetSection(t *testing.T) {
data := `
[servers]
# You can indent as you please. Tabs or spaces. TOML don't care.
[servers.alpha]
ip = "10.0.0.1"
dc = "eqdc10"
[servers.beta]
ip = "10.0.0.2"
dc = "eqdc10"
`
cfg := &Config{}
c, err := cfg.ParseData([]byte(data))
assert.Nil(t, err)
assert.NotNil(t, c)
2020-10-13 14:32:41 +00:00
m, err := c.GetSection("servers")
2020-10-11 15:22:19 +00:00
assert.Nil(t, err)
assert.NotNil(t, m)
assert.Equal(t, 2, len(m))
}
func TestConfigContainer_String(t *testing.T) {
data := `
Name="Tom"
NameInvalid=13
[Person]
Name="Jerry"
`
cfg := &Config{}
c, err := cfg.ParseData([]byte(data))
assert.Nil(t, err)
assert.NotNil(t, c)
2020-10-13 14:32:41 +00:00
val, err := c.String("Name")
2020-10-11 15:22:19 +00:00
assert.Nil(t, err)
assert.Equal(t, "Tom", val)
2020-10-13 14:32:41 +00:00
_, err = c.String("Name11")
2020-10-11 15:22:19 +00:00
assert.Equal(t, config.KeyNotFoundError, err)
2020-10-13 14:32:41 +00:00
_, err = c.String("NameInvalid")
2020-10-11 15:22:19 +00:00
assert.Equal(t, config.InvalidValueTypeError, err)
2020-10-13 14:32:41 +00:00
val, err = c.String("Person.Name")
2020-10-11 15:22:19 +00:00
assert.Nil(t, err)
assert.Equal(t, "Jerry", val)
}
func TestConfigContainer_Strings(t *testing.T) {
data := `
Name=["Tom", "Jerry"]
NameInvalid="Tom"
`
cfg := &Config{}
c, err := cfg.ParseData([]byte(data))
assert.Nil(t, err)
assert.NotNil(t, c)
2020-10-13 14:32:41 +00:00
val, err := c.Strings("Name")
2020-10-11 15:22:19 +00:00
assert.Nil(t, err)
assert.Equal(t, []string{"Tom", "Jerry"}, val)
2020-10-13 14:32:41 +00:00
_, err = c.Strings("Name11")
2020-10-11 15:22:19 +00:00
assert.Equal(t, config.KeyNotFoundError, err)
2020-10-13 14:32:41 +00:00
_, err = c.Strings("NameInvalid")
2020-10-11 15:22:19 +00:00
assert.Equal(t, config.InvalidValueTypeError, err)
}
func TestConfigContainer_Set(t *testing.T) {
data := `
Name=["Tom", "Jerry"]
NameInvalid="Tom"
`
cfg := &Config{}
c, err := cfg.ParseData([]byte(data))
assert.Nil(t, err)
assert.NotNil(t, c)
2020-10-13 14:32:41 +00:00
err = c.Set("Age", "11")
2020-10-11 15:22:19 +00:00
assert.Nil(t, err)
2020-10-13 14:32:41 +00:00
age, err := c.String("Age")
2020-10-11 15:22:19 +00:00
assert.Nil(t, err)
assert.Equal(t, "11", age)
}
func TestConfigContainer_SubAndMushall(t *testing.T) {
data := `
[servers]
# You can indent as you please. Tabs or spaces. TOML don't care.
[servers.alpha]
ip = "10.0.0.1"
dc = "eqdc10"
[servers.beta]
ip = "10.0.0.2"
dc = "eqdc10"
`
cfg := &Config{}
c, err := cfg.ParseData([]byte(data))
assert.Nil(t, err)
assert.NotNil(t, c)
2020-10-13 14:32:41 +00:00
sub, err := c.Sub("servers")
2020-10-11 15:22:19 +00:00
assert.Nil(t, err)
assert.NotNil(t, sub)
2020-10-13 14:32:41 +00:00
sub, err = sub.Sub("alpha")
2020-10-11 15:22:19 +00:00
assert.Nil(t, err)
assert.NotNil(t, sub)
2020-10-13 14:32:41 +00:00
ip, err := sub.String("ip")
2020-10-11 15:22:19 +00:00
assert.Nil(t, err)
assert.Equal(t, "10.0.0.1", ip)
svr := &Server{}
2020-10-13 14:32:41 +00:00
err = sub.Unmarshaler("", svr)
2020-10-11 15:22:19 +00:00
assert.Nil(t, err)
assert.Equal(t, "10.0.0.1", svr.Ip)
svr = &Server{}
2020-10-13 14:32:41 +00:00
err = c.Unmarshaler("servers.alpha", svr)
2020-10-11 15:22:19 +00:00
assert.Nil(t, err)
assert.Equal(t, "10.0.0.1", svr.Ip)
}
func TestConfigContainer_SaveConfigFile(t *testing.T) {
filename := "test_config.toml"
path := os.TempDir() + string(os.PathSeparator) + filename
data := `
[servers]
# You can indent as you please. Tabs or spaces. TOML don't care.
[servers.alpha]
ip = "10.0.0.1"
dc = "eqdc10"
[servers.beta]
ip = "10.0.0.2"
dc = "eqdc10"
`
cfg := &Config{}
c, err := cfg.ParseData([]byte(data))
fmt.Println(path)
assert.Nil(t, err)
assert.NotNil(t, c)
2020-10-13 14:32:41 +00:00
sub, err := c.Sub("servers")
2020-10-11 15:22:19 +00:00
assert.Nil(t, err)
2020-10-13 14:32:41 +00:00
err = sub.SaveConfigFile(path)
2020-10-11 15:22:19 +00:00
assert.Nil(t, err)
}
type Server struct {
Ip string `toml:"ip"`
}