Skip to content

Instantly share code, notes, and snippets.

@mehmetcemyucel
Created April 29, 2022 09:16
Show Gist options
  • Save mehmetcemyucel/0838913d1c3b06272a9329cd858699d4 to your computer and use it in GitHub Desktop.
Save mehmetcemyucel/0838913d1c3b06272a9329cd858699d4 to your computer and use it in GitHub Desktop.
package config
import (
"github.com/spf13/viper"
"testing"
)
func TestReadConfig_whenEnvEmpty_thenReadLocalFile(t *testing.T) {
expectedValue := ":8080"
readFromLocalAppYml = func(v *viper.Viper) *viper.Viper {
v.SetDefault("APP-SERVER.PORT", expectedValue)
return v
}
c := ReadConfig(&Config{}, "")
if c.Server.Port != expectedValue {
t.Errorf("Expected server port variable %v actual %v", expectedValue, c.Server.Port)
}
}
func TestReadConfig_whenEnv_thenReadFromEnv(t *testing.T) {
expectedValue := ":8080"
readFromEnv = func(v *viper.Viper) *viper.Viper {
v.SetDefault("APP-SERVER.PORT", expectedValue)
return v
}
c := ReadConfig(&Config{}, "ENV")
if c.Server.Port != expectedValue {
t.Errorf("Expected server port variable %v actual %v", expectedValue, c.Server.Port)
}
}
func TestReadConfig_whenEnvRemote_thenReadFromRemoteConfigServer(t *testing.T) {
expectedValue := ":8080"
readFromConfigServer = func(v *viper.Viper) *viper.Viper {
v.SetDefault("APP-SERVER.PORT", expectedValue)
return v
}
c := ReadConfig(&Config{}, "REMOTE")
if c.Server.Port != expectedValue {
t.Errorf("Expected server port variable %v actual %v", expectedValue, c.Server.Port)
}
}
func TestReadConfig_whenEnvLocal_thenReadLocalFile(t *testing.T) {
expectedValue := ":8080"
readFromLocalAppYml = func(v *viper.Viper) *viper.Viper {
v.SetDefault("APP-SERVER.PORT", expectedValue)
return v
}
c := ReadConfig(&Config{}, "LOCAL")
if c.Server.Port != expectedValue {
t.Errorf("Expected server port variable %v actual %v", expectedValue, c.Server.Port)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment