github.com/shoshinnikita/budget-manager@v0.7.1-0.20220131195411-8c46ff1c6778/internal/app/config_test.go (about) 1 package app 2 3 import ( 4 "os" 5 "testing" 6 7 "github.com/stretchr/testify/require" 8 9 "github.com/ShoshinNikita/budget-manager/internal/db" 10 "github.com/ShoshinNikita/budget-manager/internal/db/pg" 11 "github.com/ShoshinNikita/budget-manager/internal/db/sqlite" 12 "github.com/ShoshinNikita/budget-manager/internal/logger" 13 "github.com/ShoshinNikita/budget-manager/internal/web" 14 ) 15 16 func TestParseConfig(t *testing.T) { 17 t.Parallel() 18 19 require := require.New(t) 20 21 envs := []struct{ key, value string }{ 22 {"LOGGER_MODE", "develop"}, 23 {"LOGGER_LEVEL", "fatal"}, 24 {"DB_TYPE", "mongodb"}, 25 {"DB_PG_HOST", "example.com"}, 26 {"DB_PG_PORT", "8888"}, 27 {"DB_PG_USER", "user"}, 28 {"DB_PG_PASSWORD", "qwerty"}, 29 {"DB_PG_DATABASE", "db"}, 30 {"DB_SQLITE_PATH", "./var/db.db"}, 31 {"SERVER_PORT", "6666"}, 32 {"SERVER_USE_EMBED", "false"}, 33 {"SERVER_ENABLE_PROFILING", "true"}, 34 {"SERVER_AUTH_DISABLE", "true"}, 35 {"SERVER_AUTH_BASIC_CREDS", "user:qwerty,admin:admin"}, 36 } 37 for _, env := range envs { 38 os.Setenv(env.key, env.value) 39 } 40 41 want := Config{ 42 Logger: logger.Config{ 43 Level: "fatal", 44 Mode: "develop", 45 }, 46 DB: DBConfig{ 47 Type: db.Unknown, 48 Postgres: pg.Config{ 49 Host: "example.com", 50 Port: 8888, 51 User: "user", 52 Password: "qwerty", 53 Database: "db", 54 }, 55 SQLite: sqlite.Config{ 56 Path: "./var/db.db", 57 }, 58 }, 59 Server: web.Config{ 60 Port: 6666, 61 UseEmbed: false, 62 EnableProfiling: true, 63 Auth: web.AuthConfig{ 64 Disable: true, 65 BasicAuthCreds: web.Credentials{ 66 "user": "qwerty", 67 "admin": "admin", 68 }, 69 }, 70 }, 71 } 72 73 cfg, err := ParseConfig() 74 require.Nil(err) 75 require.Equal(want, cfg) 76 }