-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgonfig_test.go
100 lines (95 loc) · 3.38 KB
/
gonfig_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
package gonfig_test
import (
. "github.com/Nomon/gonfig"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Gonfig", func() {
Describe("Config struct", func() {
var cfg *Gonfig
BeforeEach(func() {
cfg = NewConfig(nil)
})
Describe("config.Default", func() {
It("Should automatically create memory config for defaults", func() {
defaults := cfg.Defaults
Expect(defaults).ToNot(BeNil())
memconf := NewMemoryConfig()
memconf.Set("a", "b")
cfg.Defaults.Reset(memconf.All())
Expect(cfg.Get("a")).To(Equal("b"))
Expect(cfg.Defaults.Get("a")).To(Equal("b"))
})
})
It("Should use memory store to set and get by default", func() {
cfg.Set("test_a", "10")
Ω(cfg.Get("test_a")).Should(Equal(cfg.Get("test_a")))
})
It("Should return nil when key is non-existing", func() {
Expect(cfg.Get("some-key")).To(Equal(""))
})
It("Should return and use Defaults", func() {
cfg.Defaults.Set("test_var", "abc")
Ω(cfg.Defaults.Get("test_var")).Should(Equal("abc"))
cfg.Set("test_var", "bca")
Ω(cfg.Defaults.Get("test_var")).Should(Equal("abc"), "Setting to memory should not override defaults")
Ω(cfg.Get("test_var")).Should(Equal("bca"), "Set to config should set in memory and use it over defaults")
})
It("Should reset everything else but Defaults() on reset", func() {
cfg.Defaults.Set("test_var", "abc")
Ω(cfg.Defaults.Get("test_var")).Should(Equal("abc"))
cfg.Set("test_var", "bca")
Ω(cfg.Defaults.Get("test_var")).Should(Equal("abc"), "Setting to memory should not override defaults")
Ω(cfg.Get("test_var")).Should(Equal("bca"), "Set to config should set in memory and use it over defaults")
cfg.Reset()
Ω(cfg.Get("test_var")).Should(Equal("abc"), "Set to config should set in memory and use it over defaults")
})
It("Should load & save all relevant sources", func() {
cfg.Use("json1", NewJsonConfig("./config_test_1.json"))
cfg.Use("json2", NewJsonConfig("./config_test_2.json"))
cfg.Use("json2").Set("asd", "123")
cfg.Use("json1").Set("asd", "321")
err := cfg.Save()
Expect(err).ToNot(HaveOccurred())
cfg.Reset()
Expect(len(cfg.Use("json1").All()) == 0).To(BeTrue())
err = cfg.Load()
Expect(err).ToNot(HaveOccurred())
Expect(cfg.Use("json1").Get("asd")).To(Equal("321"))
Expect(cfg.Use("json2").Get("asd")).To(Equal("123"))
})
It("Should return all values from all storages", func() {
cfg.Use("mem1", NewMemoryConfig())
cfg.Use("mem2", NewMemoryConfig())
cfg.Set("asd", "123456")
cfg.Use("mem1").Set("das", "654321")
cfg.Use("mem2").Set("sad", "654321")
i := 0
for key, value := range cfg.All() {
Expect(cfg.Get(key)).To(Equal(value))
i++
}
Expect(i == 3).To(BeTrue())
})
It("Should be able to use Config objects in the hierarchy", func() {
cfg.Use("test", NewConfig(nil))
cfg.Set("test_123", "321test")
Expect(cfg.Use("test").Get("test_123")).To(Equal(""))
})
It("should prefere using defaults deeprer in hierarchy (reverse order to normal fetch.)", func() {
deeper := NewConfig(nil)
deeper.Defaults.Reset(map[string]string{
"test": "123",
"testb": "321",
})
cfg.Use("test", deeper)
cfg.Defaults.Reset(map[string]string{
"test": "333",
})
Expect(cfg.Get("test")).To(Equal("123"))
Expect(cfg.Get("testb")).To(Equal("321"))
cfg.Set("testb", "1")
Expect(cfg.Get("testb")).To(Equal("1"))
})
})
})