adds test for default values of config
This commit is contained in:
parent
da50161a92
commit
8cc2de5c2a
5
config/config-missing-fields.yaml
Normal file
5
config/config-missing-fields.yaml
Normal file
@ -0,0 +1,5 @@
|
||||
app:
|
||||
environment: "missingfield"
|
||||
|
||||
db:
|
||||
type: "typical"
|
@ -1,11 +1,11 @@
|
||||
app:
|
||||
environment: "devel"
|
||||
port: 8080
|
||||
port: 5151
|
||||
|
||||
db:
|
||||
type: "type"
|
||||
host: "host"
|
||||
type: "aoeu"
|
||||
host: "aeihn"
|
||||
port: 1234
|
||||
user: USER
|
||||
password: PASSWORD
|
||||
database: gogmagog
|
||||
database: g2
|
||||
|
@ -29,16 +29,30 @@ type Conf struct {
|
||||
Db DBConfig
|
||||
}
|
||||
|
||||
// GetConf returns config values
|
||||
func GetConf(filename string) *Conf {
|
||||
|
||||
func createDefaultConf() *Conf {
|
||||
cnf := &Conf{
|
||||
App: AppConfig{
|
||||
Environment: "local",
|
||||
Port: "8080",
|
||||
},
|
||||
Db: DBConfig{
|
||||
Type: "postgres",
|
||||
Host: "localhost",
|
||||
Port: "5432",
|
||||
User: "<user>",
|
||||
Password: "<password>",
|
||||
Database: "gogmagog",
|
||||
},
|
||||
}
|
||||
|
||||
return cnf
|
||||
}
|
||||
|
||||
// GetConf returns config values
|
||||
func GetConf(filename string) *Conf {
|
||||
|
||||
cnf := createDefaultConf()
|
||||
|
||||
vv := viper.New()
|
||||
vv.SetConfigName(filename)
|
||||
vv.SetConfigType("yaml")
|
||||
|
@ -1,26 +1,44 @@
|
||||
package config_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
|
||||
"gitea.deepak.science/deepak/gogmagog/config"
|
||||
)
|
||||
|
||||
func TestTrue(t *testing.T) {
|
||||
func TestSample(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
conf := config.GetConf("config-sample")
|
||||
|
||||
appConf := conf.App
|
||||
assert.Equal("devel", appConf.Environment)
|
||||
assert.Equal("8080", appConf.Port)
|
||||
assert.Equal("5151", appConf.Port)
|
||||
|
||||
dbConf := conf.Db
|
||||
assert.Equal("type", dbConf.Type)
|
||||
assert.Equal("host", dbConf.Host)
|
||||
assert.Equal("aoeu", dbConf.Type)
|
||||
assert.Equal("aeihn", dbConf.Host)
|
||||
assert.Equal("1234", dbConf.Port)
|
||||
assert.Equal("USER", dbConf.User)
|
||||
assert.Equal("PASSWORD", dbConf.Password)
|
||||
assert.Equal("g2", dbConf.Database)
|
||||
}
|
||||
|
||||
func TestMissing(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
conf := config.GetConf("config-missing-fields")
|
||||
|
||||
appConf := conf.App
|
||||
assert.Equal("missingfield", appConf.Environment)
|
||||
assert.Equal("8080", appConf.Port)
|
||||
|
||||
dbConf := conf.Db
|
||||
assert.Equal("typical", dbConf.Type)
|
||||
assert.Equal("localhost", dbConf.Host)
|
||||
assert.Equal("5432", dbConf.Port)
|
||||
assert.Equal("<user>", dbConf.User)
|
||||
assert.Equal("<password>", dbConf.Password)
|
||||
assert.Equal("gogmagog", dbConf.Database)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user