Skip to content
Snippets Groups Projects
Commit 5ecd088d authored by OZGCloud's avatar OZGCloud
Browse files

OZG-6747 update/add unittests for config

parent a11b8dc0
No related branches found
No related tags found
No related merge requests found
...@@ -35,7 +35,8 @@ import ( ...@@ -35,7 +35,8 @@ import (
const ( const (
defaultConfig = "config.yml" defaultConfig = "config.yml"
testFilePath = "testdata/test_config.yml" testFilePath = "testdata/"
APP_ENV_NAME = "APP_ENV"
) )
type Config struct { type Config struct {
...@@ -63,7 +64,7 @@ func LoadConfig(configFilePath ...string) Config { ...@@ -63,7 +64,7 @@ func LoadConfig(configFilePath ...string) Config {
configFile := defaultConfig configFile := defaultConfig
env := os.Getenv("APP_ENV") env := os.Getenv(APP_ENV_NAME)
if env != "" { if env != "" {
configFile = fmt.Sprintf("config-%s.yml", env) configFile = fmt.Sprintf("config-%s.yml", env)
} }
...@@ -73,7 +74,7 @@ func LoadConfig(configFilePath ...string) Config { ...@@ -73,7 +74,7 @@ func LoadConfig(configFilePath ...string) Config {
if len(configFilePath) > 0 { if len(configFilePath) > 0 {
fp = configFilePath[0] fp = configFilePath[0]
} else if testing.Testing() { } else if testing.Testing() {
fp = testFilePath fp = testFilePath+configFile
} }
file, err := os.ReadFile(fp) file, err := os.ReadFile(fp)
......
...@@ -28,10 +28,11 @@ package config ...@@ -28,10 +28,11 @@ package config
import ( import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"os"
) )
func TestLoadConfig(t *testing.T) { func TestLoadConfig(t *testing.T) {
t.Run("should load config from file", func(t *testing.T) { t.Run("should load default config from file", func(t *testing.T) {
config := LoadConfig() config := LoadConfig()
expectedConfig := Config{} expectedConfig := Config{}
...@@ -44,4 +45,22 @@ func TestLoadConfig(t *testing.T) { ...@@ -44,4 +45,22 @@ func TestLoadConfig(t *testing.T) {
assert.Equal(t, expectedConfig, config) assert.Equal(t, expectedConfig, config)
}) })
t.Run("should load local config", func(t *testing.T) {
os.Setenv(APP_ENV_NAME, "local")
defer os.Unsetenv(APP_ENV_NAME)
config := LoadConfig()
assert.NoError(t, os.Unsetenv(APP_ENV_NAME), "Unsetenv "+APP_ENV_NAME+" should not return an error")
expectedConfig := Config{}
expectedConfig.Grpc.Server.Mock = true
expectedConfig.Http.Server.Port = 8082
expectedConfig.Grpc.Server.Port = 50052
expectedConfig.Grpc.Router.Port = 50051
expectedConfig.Logging.Level = "INFO"
assert.Equal(t, expectedConfig, config)
})
} }
http:
server:
port: 8082
grpc:
server:
mock: true
port: 50052
router:
port: 50051
logging:
level: "INFO"
\ No newline at end of file
File moved
File moved
...@@ -7,4 +7,4 @@ grpc: ...@@ -7,4 +7,4 @@ grpc:
router: router:
port: 50051 port: 50051
logging: logging:
level: "DEBUG" level: "INFO"
\ No newline at end of file \ No newline at end of file
File moved
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment