-
Notifications
You must be signed in to change notification settings - Fork 3
/
config_test.go
97 lines (83 loc) · 2.14 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
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
package lochness_test
import (
"errors"
"testing"
"github.com/mistifyio/lochness/internal/tests/common"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
func TestConfig(t *testing.T) {
suite.Run(t, new(ConfigSuite))
}
type ConfigSuite struct {
common.Suite
}
func (s *ConfigSuite) TestGetConfig() {
_ = s.Context.SetConfig("TestGetConfig", "foo")
_ = s.Context.SetConfig("TestGetConfigNested/foo", "bar")
tests := []struct {
description string
key string
value string
expectedErr bool
}{
{"empty key", "", "", true},
{"missing key", "bar", "", true},
{"key present", "TestGetConfig", "foo", false},
{"nested key present", "TestGetConfigNested/foo", "bar", false},
}
for _, test := range tests {
msg := s.Messager(test.description)
val, err := s.Context.GetConfig(test.key)
s.Equal(test.value, val, msg("values should match"))
if test.expectedErr {
s.Error(err, msg("should error"))
} else {
s.NoError(err, msg("should not error"))
}
}
}
func (s *ConfigSuite) TestSetConfig() {
tests := []struct {
description string
key string
value string
expectedErr bool
}{
{"empty key", "", "bar", true},
{"empty value", "bar", "", false},
{"key and value", "foo", "bar", false},
{"already set", "foo", "baz", false},
{"nested key", "foobar/baz", "bang", false},
}
for _, test := range tests {
err := s.Context.SetConfig(test.key, test.value)
if test.expectedErr {
s.Error(err, test.description)
} else {
s.NoError(err, test.description)
}
}
}
func (s *ConfigSuite) TestForEachConfig() {
keyValues := map[string]string{
"TestForEachConfig": "foo",
"TestGetConfigNested/foo": "bar",
}
for key, value := range keyValues {
_ = s.Context.SetConfig(key, value)
}
resultKeyValues := make(map[string]string)
err := s.Context.ForEachConfig(func(k, v string) error {
resultKeyValues[k] = v
return nil
})
s.NoError(err)
s.True(assert.ObjectsAreEqual(keyValues, resultKeyValues))
returnErr := errors.New("an error")
err = s.Context.ForEachConfig(func(k, v string) error {
return returnErr
})
s.Error(err)
s.Equal(returnErr, err)
}