-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore_test.go
301 lines (279 loc) · 6.75 KB
/
store_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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Created by Yakka (https://theyakka.com)
//
// Copyright (c) 2020 Yakka LLC.
// All rights reserved.
// See the LICENSE file for licensing details and requirements.
package ystore_test
import (
"errors"
"fmt"
"path/filepath"
"testing"
"github.com/theyakka/ystore"
)
func TestStoreOrEmpty(t *testing.T) {
store := ystore.NewStore()
store.Set("substore", map[string]interface{}{
"first": "hello",
"second": 1234,
"third": 12.34,
"fourth": []int{1, 2, 3, 4},
})
store.Set("simple", 55)
substore := store.StoreFromMapOrEmpty("nonexisting")
if substore == nil {
t.Error("substore should not be nil")
return
}
substore = store.StoreFromMap("somethingnothere")
if substore != nil {
t.Error("substore should be nil")
return
}
substore = store.StoreFromMapOrEmpty("simple")
if substore == nil {
t.Error("substore should not be nil")
return
}
if substore.Len() > 0 {
t.Error("store should be empty")
return
}
}
func TestPassingSimpleMap(t *testing.T) {
testMap := map[string]interface{}{
"first": "hello",
"second": 1234,
"third": 12.34,
"fourth": []int{1, 2, 3, 4},
}
testStore := ystore.NewStoreFromMap(testMap)
if testStore.Len() != len(testMap) {
t.Fail()
}
}
func BenchmarkPassingSimpleMap(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
testMap := map[string]interface{}{
"first": "hello",
"second": 1234,
"third": 12.34,
"fourth": []int{1, 2, 3, 4},
}
ystore.NewStoreFromMap(testMap)
}
}
func TestPassingNestedMap(t *testing.T) {
nestedMap2 := map[string]interface{}{
"color1": "red",
"color2": "green",
"color3": "blue",
"color4": "purple",
}
nestedMap1 := map[string]interface{}{
"nested1": "nestedhello",
"nested2": 4321,
"colors": nestedMap2,
}
testMap := map[string]interface{}{
"first": "hello",
"second": nestedMap1,
}
testStore := ystore.NewStoreFromMap(testMap)
if testStore.Len() != len(testMap) {
t.Fail()
}
substore1 := testStore.StoreFromMapOrEmpty("second")
if substore1.Len() != len(nestedMap1) {
t.Fail()
}
substore2 := testStore.StoreFromMapOrEmpty("second.colors")
if substore2.Len() != len(nestedMap2) {
t.Fail()
}
}
func TestFailWhenPassingNonExistentDir(t *testing.T) {
baseDir, dirErr := filepath.Abs("./_doesnotexist")
if dirErr != nil {
t.Error(dirErr)
}
store := ystore.NewStore()
storeErr := store.ReadDir(baseDir)
if storeErr != nil {
return // should fail
}
t.Error("This should have failed because the directory doesn't exist")
}
func TestFailWhenPassingFileToReadAll(t *testing.T) {
baseDir, dirErr := filepath.Abs("./_testdata/first.toml")
if dirErr != nil {
t.Error(dirErr)
}
store := ystore.NewStore()
storeErr := store.ReadDir(baseDir)
if storeErr != nil {
return // should fail
}
t.Error("This should have failed because the directory doesn't exist")
}
func TestParseAllInDirectory(t *testing.T) {
baseDir, dirErr := filepath.Abs("./_testdata")
if dirErr != nil {
t.Error(dirErr)
return
}
store := ystore.NewStore()
storeErr := store.ReadDir(baseDir)
if storeErr != nil {
t.Error(storeErr)
return
}
}
func TestParseSingleFile(t *testing.T) {
filename, dirErr := filepath.Abs("./_testdata/first.toml")
if dirErr != nil {
t.Error(dirErr)
return
}
store := ystore.NewStore()
if storeErr := store.ReadFile(filename); storeErr != nil {
t.Error(storeErr)
return
}
}
func TestParseSingleBadYamlFile(t *testing.T) {
filename, dirErr := filepath.Abs("./_badtestdata/bad.yaml")
if dirErr != nil {
t.Error(dirErr)
return
}
store := ystore.NewStore()
if storeErr := store.ReadFile(filename); storeErr == nil {
t.Error(errors.New("file is bad and should have failed"))
return
}
}
func TestParseNonExistingFile(t *testing.T) {
store := ystore.NewStore()
if storeErr := store.ReadFile("./_badtestdata/nonexistent.yaml"); storeErr == nil {
t.Error(errors.New("file is non-existent and should have failed"))
return
}
}
func TestParseMultipleFiles(t *testing.T) {
var originalFilenames = []string{
"./_testdata/first.toml",
"./_testdata/third.json",
}
var filenames []string
for _, filename := range originalFilenames {
absFilename, dirErr := filepath.Abs(filename)
if dirErr != nil {
t.Error(dirErr)
return
}
filenames = append(filenames, absFilename)
}
store := ystore.NewStore()
if storeErr := store.ReadFiles(filenames...); storeErr != nil {
t.Error(storeErr)
return
}
}
func TestStoreMerging(t *testing.T) {
store1 := ystore.NewStoreFromMap(map[string]interface{}{
"item1": "item 1",
"item2": 567,
"item3": []string{"string 1", "string 2"},
})
store2 := ystore.NewStoreFromMap(map[string]interface{}{
"item4": 234.567,
"item5": "item 5",
})
mergedStore := ystore.MergeStores(store1, store2)
if store1.Len() != 3 {
t.Fail()
return
}
if store2.Len() != 2 {
t.Fail()
return
}
if mergedStore.Len() != 5 {
t.Fail()
return
}
}
func TestStoreInstanceMerging(t *testing.T) {
store1 := ystore.NewStoreFromMap(map[string]interface{}{
"item1": "item 1",
"item2": 567,
"item3": []string{"string 1", "string 2"},
})
store2 := ystore.NewStoreFromMap(map[string]interface{}{
"item4": 234.567,
"item5": "item 5",
})
mergedStore := store1.MergeWith(store2)
if store1.Len() != 3 {
t.Fail()
return
}
if store2.Len() != 2 {
t.Fail()
return
}
if mergedStore.Len() != 5 {
t.Fail()
return
}
}
func ExampleStore() {
store := ystore.NewStore()
store.Set("color", "red")
store.Set("length", 100)
fmt.Printf("The item is %s and the length is %d.\n", store.GetString("color"), store.GetInt("length"))
// Output: The item is red and the length is 100.
}
func ExampleStore_map() {
store := ystore.NewStoreFromMap(map[string]interface{}{
"color": "green",
"length": 80,
})
fmt.Printf("The item is %s and the length is %d.\n", store.GetString("color"), store.GetInt("length"))
// Output: The item is green and the length is 80.
}
func ExampleStore_file() {
filename, fileErr := filepath.Abs("./_testdata/first.toml")
if fileErr != nil {
// handle the path error
return
}
store := ystore.NewStore()
if readErr := store.ReadFile(filename); readErr != nil {
// handle store load error
return
}
name := store.GetString("firstdata.name")
numbers := store.GetIntSlice("firstdata.numbers")
fmt.Printf("The item name is '%s' and the numbers slice has %d element(s).\n", name, len(numbers))
// Output: The item name is 'First Item' and the numbers slice has 5 element(s).
}
func BenchmarkMergeStores(b *testing.B) {
store1 := ystore.NewStoreFromMap(map[string]interface{}{
"item1": "item 1",
"item2": 567,
"item3": []string{"string 1", "string 2"},
})
store2 := ystore.NewStoreFromMap(map[string]interface{}{
"item4": 234.567,
"item5": "item 5",
})
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
store1.MergeWith(store2)
}
}