-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_test.go
More file actions
93 lines (83 loc) · 2.34 KB
/
Copy pathconfig_test.go
File metadata and controls
93 lines (83 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
)
// setEnv is a helper function to set environment variables for a test and clean them up afterwards.
func setEnv(t *testing.T, envs map[string]string) {
for key, value := range envs {
os.Setenv(key, value)
}
t.Cleanup(func() {
for key, _ := range envs {
os.Unsetenv(key)
}
})
}
func TestLoad(t *testing.T) {
t.Run("POSITIVE-ValidEnv", func(t *testing.T) {
setEnv(t, map[string]string{
"DATABASE_HOST": "localhost",
"DATABASE_PORT": "5432",
"DATABASE_USER": "user",
"DATABASE_PASSWORD": "pass",
"DATABASE_NAME": "dbname",
"RABBITMQ_URL": "amqp://guest:guest@localhost:5672/",
"DATABASE_LOG_LEVEL": "info",
})
cfg, err := Load()
assert.NoError(t, err)
assert.NotNil(t, cfg)
assert.Equal(t, "localhost", cfg.Host)
assert.Equal(t, 5432, cfg.Port)
assert.Equal(t, "user", cfg.User)
assert.Equal(t, "pass", cfg.Password)
assert.Equal(t, "dbname", cfg.Name)
assert.NotEmpty(t, cfg.RabbitMQURL)
assert.Equal(t, "info", cfg.LogLevel)
})
t.Run("POSITIVE-DefaultLogLevel", func(t *testing.T) {
setEnv(t, map[string]string{
"DATABASE_HOST": "localhost",
"DATABASE_PORT": "5432",
"DATABASE_USER": "user",
"DATABASE_PASSWORD": "pass",
"DATABASE_NAME": "dbname",
"RABBITMQ_URL": "amqp://guest:guest@localhost:5672/",
// DATABASE_LOG_LEVEL is intentionally not set to test default
})
cfg, err := Load()
assert.NoError(t, err)
assert.NotNil(t, cfg)
assert.Equal(t, "info", cfg.LogLevel)
})
t.Run("NEGATIVE-MissingRequiredEnv", func(t *testing.T) {
setEnv(t, map[string]string{
"DATABASE_HOST": "",
"DATABASE_PORT": "",
"DATABASE_USER": "",
"DATABASE_PASSWORD": "",
"DATABASE_NAME": "",
"RABBITMQ_URL": "",
"DATABASE_LOG_LEVEL": "",
})
cfg, err := Load()
assert.Error(t, err)
assert.Nil(t, cfg)
assert.Contains(t, err.Error(), "should not be empty")
})
}
func TestConfig_DataSourceName(t *testing.T) {
t.Run("POSITIVE-Format", func(t *testing.T) {
cfg := Config{
User: "user",
Password: "pass",
Host: "localhost",
Port: 5432,
Name: "dbname",
}
dsn := cfg.DataSourceName()
assert.Equal(t, "user=user password=pass host=localhost port=5432 dbname=dbname sslmode=disable", dsn)
})
}