-
Notifications
You must be signed in to change notification settings - Fork 6
/
config_test.go
53 lines (42 loc) · 1.18 KB
/
config_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
package main
import (
"bytes"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"log"
"os"
"testing"
)
func TestDefaultConfig(t *testing.T) {
vip = viper.New()
var buf bytes.Buffer
log.SetOutput(&buf)
defer func() {
log.SetOutput(os.Stderr)
}()
loadConfig("")
assert.Equal(t, len(vip.GetStringMap("sensors")), 0, "Should initialize empty sensor list")
assert.Contains(t, buf.String(), "Not Found")
}
func TestConfigWithSensors(t *testing.T) {
vip = viper.New()
var buf bytes.Buffer
log.SetOutput(&buf)
defer func() {
log.SetOutput(os.Stderr)
}()
loadSampleConfig()
sensors := vip.GetStringMap("sensors")
assert.Equal(t, len(sensors), 2, "Should initialize sensor list from config file")
assert.NotNil(t, sensors["91"], "Sensor 91 exists in config")
assert.Nil(t, sensors["0815"], "Sensor 0185 does not exist")
assert.Equal(t, vip.GetString("sensors.1235.location"), "kitchen", "Sensor location is kitchen")
assert.Equal(t, vip.GetString("sensors.1235.maeh"), "", "Sensor location is kitchen")
assert.Equal(t, buf.String(), "")
}
func loadSampleConfig() {
vip = viper.New()
initConfig("")
vip.SetConfigName("sample-weather-station")
readConfig()
}