Skip to content

Commit

Permalink
refactor(config): make dialect non-configurable
Browse files Browse the repository at this point in the history
Test all env vars
  • Loading branch information
sundowndev committed Sep 1, 2020
1 parent 4fed9dc commit 5405e00
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
6 changes: 3 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ type ipfsConfig struct {
}

type dbConfig struct {
Dialect string `yaml:"dialect" json:"dialect" default:"postgres" env:"DB_DIALECT"`
Dialect string `yaml:"-" json:"-" default:"postgres"`
Host string `yaml:"host" json:"host" default:"localhost" env:"DB_HOST"`
Port string `yaml:"port" json:"port" default:"5432" env:"DB_PORT"`
User string `yaml:"user" json:"user" default:"postgres" env:"DB_USER"`
Password string `yaml:"password" json:"password" default:"secret" env:"DB_PASSWORD"`
Password string `yaml:"password" json:"password" default:"" env:"DB_PASSWORD"`
Database string `yaml:"db_name" json:"db_name" default:"gilfoyle" env:"DB_NAME"`
}

type redisConfig struct {
Host string `yaml:"host" json:"host" default:"localhost" env:"REDIS_HOST"`
Database string `yaml:"database" json:"database" default:"localhost" env:"REDIS_DATABASE"`
Database string `yaml:"database" json:"database" default:"0" env:"REDIS_DB"`
Port string `yaml:"port" json:"port" default:"6379" env:"REDIS_PORT"`
Password string `yaml:"password" json:"password" default:"" env:"REDIS_PASSWORD"`
}
Expand Down
36 changes: 26 additions & 10 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestConfig(t *testing.T) {
err := New()
assert.Nil(err)

assert.Equal(GetConfig(), &Config{
assert.Equal(&Config{
Services: servicesConfig{
IPFS: ipfsConfig{
Gateway: "gateway.ipfs.io",
Expand All @@ -23,39 +23,55 @@ func TestConfig(t *testing.T) {
Host: "localhost",
Port: "5432",
User: "postgres",
Password: "secret",
Password: "",
Database: "gilfoyle",
},
Redis: redisConfig{
Host: "localhost",
Port: "6379",
Database: "0",
Password: "",
},
},
Settings: settingsConfig{
ServeDocs: true,
MaxFileSize: "50mb",
},
}, "should be equal")
}, GetConfig(), "should be equal")
})

t.Run("should set values from env vars", func(t *testing.T) {
_ = os.Setenv("DB_DIALECT", "dialect")
defer os.Clearenv()

_ = os.Setenv("DB_HOST", "host")
_ = os.Setenv("DB_PORT", "port")
_ = os.Setenv("DB_USER", "user")
_ = os.Setenv("DB_PASSWORD", "password")
_ = os.Setenv("DB_NAME", "database")

_ = os.Setenv("REDIS_HOST", "redis_host")
_ = os.Setenv("REDIS_DB", "redis_db")
_ = os.Setenv("REDIS_PORT", "redis_port")
_ = os.Setenv("REDIS_PASSWORD", "redis_pass")

_ = os.Setenv("IPFS_GATEWAY", "ipfs_gateway")

err := New()
assert.Nil(err)

assert.Equal(GetConfig().Services.DB.Dialect, "dialect")
assert.Equal(GetConfig().Services.DB.Host, "host")
assert.Equal(GetConfig().Services.DB.Port, "port")
assert.Equal(GetConfig().Services.DB.User, "user")
assert.Equal(GetConfig().Services.DB.Database, "database")
assert.Equal(GetConfig().Services.DB.Password, "password")
assert.Equal("postgres", GetConfig().Services.DB.Dialect)
assert.Equal("host", GetConfig().Services.DB.Host)
assert.Equal("port", GetConfig().Services.DB.Port)
assert.Equal("user", GetConfig().Services.DB.User)
assert.Equal("database", GetConfig().Services.DB.Database)
assert.Equal("password", GetConfig().Services.DB.Password)

assert.Equal("redis_db", GetConfig().Services.Redis.Database)
assert.Equal("redis_pass", GetConfig().Services.Redis.Password)
assert.Equal("redis_port", GetConfig().Services.Redis.Port)
assert.Equal("redis_host", GetConfig().Services.Redis.Host)

assert.Equal("ipfs_gateway", GetConfig().Services.IPFS.Gateway)
})

t.Run("should not return error on bad file path", func(t *testing.T) {
Expand Down

0 comments on commit 5405e00

Please sign in to comment.