-
Notifications
You must be signed in to change notification settings - Fork 26
/
main_test.go
90 lines (78 loc) · 2.5 KB
/
main_test.go
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
package main
import (
"testing"
"github.com/rameshsunkara/go-rest-api-example/internal/models"
"github.com/stretchr/testify/assert"
)
func TestMustEnvConfig(t *testing.T) {
t.Run("MissingEnvVariables", func(t *testing.T) {
resetEnv(t)
// Call MustEnvConfig and expect panic
assert.Panics(t, func() {
_ = MustEnvConfig()
}, "MustEnvConfig did not panic with missing environment variables")
})
t.Run("ValidEnvVariables", func(t *testing.T) {
resetEnv(t)
// Set required environment variables
t.Setenv("environment", "test")
t.Setenv("port", "8080")
t.Setenv("dbName", "testDB")
t.Setenv("MongoVaultSideCar", "/path/to/mongo/sidecar")
t.Setenv("logLevel", "debug")
// Call MustEnvConfig and verify returned configurations
expectedConfig := models.ServiceEnv{
Name: "test",
Port: "8080",
PrintQueries: false, // default value
MongoVaultSideCar: "/path/to/mongo/sidecar",
DisableAuth: false, // default value
DBName: "testDB",
LogLevel: "debug",
}
actualConfig := MustEnvConfig()
assert.Equal(t, expectedConfig, actualConfig,
"MustEnvConfig did not return expected configurations")
})
}
func TestMustEnvConfig_Defaults(t *testing.T) {
t.Run("Default Env Values", func(t *testing.T) {
resetEnv(t)
// Set required environment variables
t.Setenv("environment", "test")
t.Setenv("dbName", "testDB")
t.Setenv("MongoVaultSideCar", "/path/to/mongo/sidecar")
// Call MustEnvConfig and verify returned configurations
expectedConfig := models.ServiceEnv{
Name: "test",
Port: defaultPort,
PrintQueries: false, // default value
MongoVaultSideCar: "/path/to/mongo/sidecar",
DisableAuth: false, // default value
DBName: "testDB",
LogLevel: "info",
}
actualConfig := MustEnvConfig()
assert.Equal(t, expectedConfig, actualConfig,
"Default values are not set correctly in MustEnvConfig")
})
}
func TestMustEnvConfig_FailOnSideCar(t *testing.T) {
t.Run("Fail on MongoSide Car", func(t *testing.T) {
resetEnv(t)
// Set required environment variables
t.Setenv("environment", "test")
t.Setenv("dbName", "testDB")
assert.Panics(t, func() {
_ = MustEnvConfig()
}, "MustEnvConfig did not panic with missing environment variables")
})
}
func resetEnv(t *testing.T) {
t.Setenv("environment", "")
t.Setenv("port", "")
t.Setenv("dbName", "")
t.Setenv("MongoVaultSideCar", "")
t.Setenv("logLevel", "")
t.Setenv("printDBQueries", "")
}