-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathenv_test.go
More file actions
159 lines (130 loc) · 4.19 KB
/
env_test.go
File metadata and controls
159 lines (130 loc) · 4.19 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package env
import (
"appengine_internal"
c "github.com/smartystreets/goconvey/convey"
"testing"
)
const TESTING_ENV_NAME = "App-Test"
func TestLoad(t *testing.T) {
c.Convey("With a json file, it will load it into memory", t, func() {
fileLocation := "./sample-environment.json"
err := Load(fileLocation)
c.So(err, c.ShouldBeNil)
c.So(Env.emap, c.ShouldNotBeNil)
c.So(Env.raw, c.ShouldNotBeNil)
})
c.Convey("With a json file missing mappings it errors", t, func() {
fileLocation := "./mappings-missing.json"
err := Load(fileLocation)
c.So(err, c.ShouldNotBeNil)
})
}
func TestGet(t *testing.T) {
ctx := ContextMock{}
ctx.SetAppId(TESTING_ENV_NAME)
c.Convey("Before loading the json file", t, func() {
_, ok := GetOk(ctx, "message")
c.So(ok, c.ShouldBeFalse)
})
c.Convey("With a complete loaded json file", t, func() {
fileLocation := "./sample-environment.json"
err := Load(fileLocation)
c.So(err, c.ShouldBeNil)
c.Convey("It retrieves the correct vars", func() {
msg, ok := GetOk(ctx, "message")
c.So(ok, c.ShouldBeTrue)
c.So(msg, c.ShouldEqual, "I am a testing Msg")
msg, ok = GetOk(ctx, "tolerance")
c.So(ok, c.ShouldBeTrue)
c.So(msg, c.ShouldEqual, 0.14)
msg, ok = GetOk(ctx, "acceptableRank")
c.So(ok, c.ShouldBeTrue)
c.So(msg, c.ShouldResemble, []interface{}{"8","9","10"})
})
c.Convey("ok is false if no var is in json", func() {
_, ok := GetOk(ctx, "UnknownVar")
c.So(ok, c.ShouldBeFalse)
})
c.Convey("It retrieves a default var if the app is unknown", func() {
ctx.SetAppId("Unknown-AppId")
msg, ok := GetOk(ctx, "message")
c.So(ok, c.ShouldBeTrue)
c.So(msg, c.ShouldEqual, "I am a default Msg")
})
c.Convey("It uses default vars if there is no match in the current env", func() {
ctx.SetAppId(TESTING_ENV_NAME)
msg, ok := GetOk(ctx, "greeting")
c.So(ok, c.ShouldBeTrue)
c.So(msg, c.ShouldEqual, "default greeting")
})
})
c.Convey("With a json file missing default properties", t, func() {
ctx.SetAppId(TESTING_ENV_NAME)
fileLocation := "./missing-default.json"
err := Load(fileLocation)
c.So(err, c.ShouldBeNil)
c.Convey("It retrieves an available var", func() {
msg, ok := GetOk(ctx, "message")
c.So(ok, c.ShouldBeTrue)
c.So(msg, c.ShouldEqual, "I am a testing Msg")
})
c.Convey("It errors if both the environment is unknown and no default vars are present", func() {
ctx.SetAppId("Unknown-AppId")
_, err := GetOk(ctx, "Message")
c.So(err, c.ShouldNotBeNil)
})
})
}
func TestName(t *testing.T) {
ctx := ContextMock{}
ctx.SetAppId(TESTING_ENV_NAME)
fileLocation := "./sample-environment.json"
c.Convey("With a complete json file and mapping it retrieves the correct env name", t, func() {
err := Load(fileLocation)
c.So(err, c.ShouldBeNil)
name := Name(ctx)
c.So(name, c.ShouldEqual, "test")
})
c.Convey("With an unknown project id it returns the default environment", t, func() {
err := Load(fileLocation)
c.So(err, c.ShouldBeNil)
ctx.SetAppId("Unknown-AppId")
name := Name(ctx)
c.So(name, c.ShouldEqual, "default")
})
}
func TestIs(t *testing.T) {
ctx := ContextMock{}
ctx.SetAppId(TESTING_ENV_NAME)
Load("./sample-environment.json")
c.Convey("it returns accurate environment name mappings", t, func() {
c.So(Is(ctx, "test"), c.ShouldBeTrue)
})
}
func TestMustLoad(t *testing.T) {
defer func(){
r := recover()
if r == nil {
t.Fail()
}
}()
MustLoad("./nonexistent.json")
}
type ContextMock struct {
OverrideAppId string
}
func (env ContextMock) Debugf(format string, args ...interface{}) {}
func (env ContextMock) Infof(format string, args ...interface{}) {}
func (env ContextMock) Warningf(format string, args ...interface{}) {}
func (env ContextMock) Errorf(format string, args ...interface{}) {}
func (env ContextMock) Criticalf(format string, args ...interface{}) {}
func (env ContextMock) Call(service, method string, in, out appengine_internal.ProtoMessage, opts *appengine_internal.CallOptions) error {
return nil
}
func (env ContextMock) Request() interface{} { return nil }
func (env ContextMock) FullyQualifiedAppID() string {
return env.OverrideAppId
}
func (env *ContextMock) SetAppId(appId string) {
env.OverrideAppId = appId
}