2
0
config/config_test.go

136 lines
3.8 KiB
Go

/*
* Copyright (C) 2017 eschao <esc.chao@gmail.com>
*
* 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 config
import (
"encoding"
"log/slog"
"os"
"path/filepath"
"reflect"
"runtime"
"strconv"
"testing"
"time"
"git.mousesoft.ru/ms/config/test"
"github.com/stretchr/testify/assert"
)
func TestDefaultValueConfig(t *testing.T) {
conf := test.DefValueConfig{}
assert := assert.New(t)
assert.NoError(ParseDefault(&conf))
assert.Equal(true, conf.BoolValue)
assert.Equal(123, conf.IntValue)
assert.Equal(float64(123.4567), conf.Float64Value)
assert.Equal("default-string", conf.StrValue)
assert.Equal(3, len(conf.SliceValue))
assert.Equal("xx", conf.SliceValue[0])
assert.Equal("yy", conf.SliceValue[1])
assert.Equal("zz", conf.SliceValue[2])
assert.Equal(time.Second*15, conf.Interval)
assert.Equal(slog.LevelInfo, conf.LogLevel)
assert.Equal("", conf.NoDefValue)
}
func TestEnvConfig(t *testing.T) {
var err error
dbLogPrefix := "LOG_"
assert := assert.New(t)
err = os.Setenv("HOST", DB_HOST)
assert.NoError(err)
err = os.Setenv("PORT", strconv.Itoa(DB_PORT))
assert.NoError(err)
err = os.Setenv("USER", DB_USER)
assert.NoError(err)
err = os.Setenv("PASSWORD", DB_PASSWORD)
assert.NoError(err)
err = os.Setenv(dbLogPrefix+"PATH", DB_LOG_PATH)
assert.NoError(err)
err = os.Setenv(dbLogPrefix+"LEVEL", DB_LOG_LEVEL)
assert.NoError(err)
defer func() { _ = os.Unsetenv("HOST") }()
defer func() { _ = os.Unsetenv("PORT") }()
defer func() { _ = os.Unsetenv("USER") }()
defer func() { _ = os.Unsetenv("PASSWORD") }()
defer func() { _ = os.Unsetenv(dbLogPrefix + "PATH") }()
defer func() { _ = os.Unsetenv(dbLogPrefix + "LEVEL") }()
conf := test.DBConfig{}
assert.NoError(ParseEnv(&conf))
assert.Equal(DB_HOST, conf.Host)
assert.Equal(DB_PORT, conf.Port)
assert.Equal(DB_USER, conf.User)
assert.Equal(DB_PASSWORD, conf.Password)
assert.Equal(DB_LOG_PATH, conf.Log.Path)
assert.Equal(DB_LOG_LEVEL, conf.Log.Level)
}
func TestJSONConfigFile(t *testing.T) {
_, curTestFile, _, _ := runtime.Caller(0)
path := filepath.Dir(curTestFile)
conf := test.DBConfig{}
assert := assert.New(t)
assert.NoError(ParseConfigFile(&conf, path+"/test/config.json"))
assert.Equal(DB_HOST, conf.Host)
assert.Equal(DB_PORT, conf.Port)
assert.Equal(DB_USER, conf.User)
assert.Equal(DB_PASSWORD, conf.Password)
assert.Equal(DB_LOG_PATH, conf.Log.Path)
assert.Equal(DB_LOG_LEVEL, conf.Log.Level)
}
func TestYamlConfigFile(t *testing.T) {
_, curTestFile, _, _ := runtime.Caller(0)
path := filepath.Dir(curTestFile)
conf := test.DBConfig{}
assert := assert.New(t)
assert.NoError(ParseConfigFile(&conf, path+"/test/config.yaml"))
assert.Equal(DB_HOST, conf.Host)
assert.Equal(DB_PORT, conf.Port)
assert.Equal(DB_USER, conf.User)
assert.Equal(DB_PASSWORD, conf.Password)
assert.Equal(DB_LOG_PATH, conf.Log.Path)
assert.Equal(DB_LOG_LEVEL, conf.Log.Level)
}
func TestTextUnmarshal(t *testing.T) {
var level slog.Level
assert := assert.New(t)
value := reflect.ValueOf(&level)
unmarshalerType := reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
assert.True(value.Type().Implements(unmarshalerType))
valueDecoder, ok := value.Interface().(encoding.TextUnmarshaler)
assert.True(ok)
assert.NotNil(valueDecoder)
assert.NoError(valueDecoder.UnmarshalText([]byte("INFO")))
assert.Equal(slog.LevelInfo, level)
}