-
Notifications
You must be signed in to change notification settings - Fork 0
/
env_test.go
88 lines (83 loc) · 2.46 KB
/
env_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
package env
import (
"fmt"
"github.com/stretchr/testify/assert"
"os"
"strconv"
"testing"
)
func TestGetEnvWithDefaultAsStringWithEnvValue(t *testing.T) {
testCases := []struct {
envKey string
envValue string
defaultVal string
expected string
}{
{envKey: "PORT", envValue: "8080", defaultVal: "", expected: "8080"},
{envKey: "Env", envValue: "prod", defaultVal: "", expected: "prod"},
}
for _, tC := range testCases {
t.Run(fmt.Sprintf("'%s' should return '%s'", tC.envValue, tC.expected), func(t *testing.T) {
err := os.Setenv(tC.envKey, tC.envValue)
assert.Nil(t, err)
val := GetEnvWithDefaultAsString(tC.envKey, "")
assert.Equal(t, tC.envValue, val)
})
}
}
func TestGetEnvWithDefaultAsStringWithoutEnvValue(t *testing.T) {
testCases := []struct {
envKey string
defaultVal string
expected string
}{
{envKey: "HTTP_PORT", defaultVal: "8080", expected: "8080"},
{envKey: "Environment", defaultVal: "prod", expected: "prod"},
}
for _, tC := range testCases {
t.Run(fmt.Sprintf("'%s' should return '%s'", tC.defaultVal, tC.expected), func(t *testing.T) {
val := GetEnvWithDefaultAsString(tC.envKey, tC.defaultVal)
assert.Equal(t, tC.defaultVal, val)
})
}
}
func TestGetEnvWithDefaultAsIntWithEnvValue(t *testing.T) {
testCases := []struct {
envKey string
envValue string
defaultVal interface{}
expected int
}{
{envKey: "PORT", envValue: "8080", defaultVal: nil, expected: 8080},
{envKey: "Env", envValue: "1", defaultVal: 0, expected: 1},
{envKey: "Pais", envValue: "Brasil", defaultVal: nil, expected: 0},
}
for _, tC := range testCases {
t.Run(fmt.Sprintf("'%s' should return '%d'", tC.envValue, tC.expected), func(t *testing.T) {
err := os.Setenv(tC.envKey, tC.envValue)
assert.Nil(t, err)
val := GetEnvWithDefaultAsInt(tC.envKey, 0)
value, _ := strconv.Atoi(tC.envValue)
assert.Equal(t, value, val)
})
}
}
func TestGetEnvWithDefaultAsIntWithoutEnvValue(t *testing.T) {
testCases := []struct {
envKey string
defaultVal string
expected int
}{
{envKey: "PORT", defaultVal: "8080", expected: 8080},
{envKey: "Env", defaultVal: "1", expected: 1},
}
for _, tC := range testCases {
t.Run(fmt.Sprintf("'%s' should return '%d'", tC.defaultVal, tC.expected), func(t *testing.T) {
err := os.Setenv(tC.envKey, tC.defaultVal)
assert.Nil(t, err)
val := GetEnvWithDefaultAsInt(tC.envKey, 0)
value, _ := strconv.Atoi(tC.defaultVal)
assert.Equal(t, value, val)
})
}
}