-
Notifications
You must be signed in to change notification settings - Fork 0
/
yamlconfig_test.go
101 lines (83 loc) · 2.13 KB
/
yamlconfig_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
91
92
93
94
95
96
97
98
99
100
101
package yamlconfig
import (
"log"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
var exampleFileStr = `
defaults: &defaults
adapter: postgresql
encoding: unicode
pool: 5
port: 5432
host: localhost
development:
<<: *defaults
database: development
username: development
password: development
test:
<<: *defaults
database: test
username: test
password: test
`
func TestParsingYamlFile(t *testing.T) {
r, err := New([]byte(exampleFileStr))
if err != nil {
log.Fatal(err)
}
Convey("Get", t, func() {
Convey("It returns value set for env", func() {
val, err := r.Get("database")
So(err, ShouldBeNil)
So(val, ShouldEqual, "development")
})
Convey("It returns error for arg that's not defined", func() {
_, err := r.Get("random")
So(err, ShouldEqual, ErrKeyNotFound)
})
Convey("It falls back on default if key not defined in env", func() {
val, err := r.Get("adapter")
So(err, ShouldBeNil)
So(val, ShouldEqual, "postgresql")
})
})
Convey("GetString", t, func() {
Convey("It returns string value set for env", func() {
val, err := r.GetString("database")
So(err, ShouldBeNil)
So(val, ShouldEqual, "development")
So(val, ShouldHaveSameTypeAs, "")
})
})
Convey("MustGet", t, func() {
Convey("It returns value set for env", func() {
So(r.MustGet("database"), ShouldEqual, "development")
})
Convey("It panics if key is not found", func() {
So(func() { r.MustGet("nonexistent") }, ShouldPanic)
})
})
Convey("MustGetString", t, func() {
Convey("It returns value set for env", func() {
So(r.MustGetString("database"), ShouldEqual, "development")
})
Convey("It panics if key is not found", func() {
So(func() { r.MustGetString("nonexistent") }, ShouldPanic)
})
})
Convey("SetEnv", t, func() {
Convey("It returns Development as default if env is not set", func() {
So(r.GetEnv(), ShouldEqual, Development)
})
Convey("It returns Test env after Test is set", func() {
r.SetEnv(Test)
So(r.GetEnv(), ShouldEqual, Test)
val, err := r.Get("database")
So(err, ShouldBeNil)
So(val, ShouldEqual, "test")
r.SetEnv(Development) // cleanup
})
})
}