-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmutations_store_test.go
More file actions
266 lines (207 loc) · 6.38 KB
/
mutations_store_test.go
File metadata and controls
266 lines (207 loc) · 6.38 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
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
package figtree
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestPersist_MapFromString(t *testing.T) {
os.Args = []string{os.Args[0]}
figs := With(Options{Germinate: true, Tracking: false})
figs.NewMap("attrs", map[string]string{"key1": "value1"}, "usage")
assert.NoError(t, figs.Parse())
// Manually store the map value as a raw string to force the string path in persist
tree := figs.(*figTree)
tree.mu.Lock()
v := &Value{
Value: "key2=value2,key3=value3",
Mutagensis: tMap,
}
tree.values.Store("attrs", v)
tree.mu.Unlock()
// Now Store through the normal path — persist will read the raw string flesh
// and attempt to parse it via SplitN. With limit 1 the map will be empty.
figs.StoreMap("attrs", map[string]string{"key4": "value4"})
result := *figs.Map("attrs")
assert.Contains(t, result, "key4", "key4 should be present after StoreMap")
// This will fail with the SplitN bug because the old value parsed from string
// produces an empty map, making the changed check unreliable
assert.Equal(t, map[string]string{"key4": "value4"}, result)
}
func TestPersist_MapFromStringMutation(t *testing.T) {
os.Args = []string{os.Args[0]}
figs := With(Options{Germinate: true, Tracking: true})
figs.NewMap("attrs", map[string]string{"key1": "value1"}, "usage")
assert.NoError(t, figs.Parse())
tree := figs.(*figTree)
tree.mu.Lock()
tree.values.Store("attrs", &Value{
Value: "key2=value2,key3=value3",
Mutagensis: tMap,
})
tree.mu.Unlock()
mutationFired := make(chan Mutation, 1)
go func() {
select {
case m, ok := <-figs.Mutations():
if ok {
mutationFired <- m
}
case <-time.After(500 * time.Millisecond):
close(mutationFired)
}
}()
figs.StoreMap("attrs", map[string]string{"key2": "value2", "key3": "value3"})
mutation, fired := <-mutationFired
if fired {
t.Logf("mutation fired: old=%v new=%v", mutation.Old, mutation.New)
assert.Fail(t, "mutation should not have fired — value did not change")
}
}
func TestTree_StoreString(t *testing.T) {
const k, u = "test-store-string", "usage"
// new fig tree with string
figs := With(Options{Germinate: true})
figs.NewString(k, "default", u)
assert.Nil(t, figs.Parse())
// get a string k
s := *figs.String(k)
assert.Equal(t, "default", s)
// store a new string in k
figs.StoreString(k, "new")
// verify new string in k
s = *figs.String(k)
assert.Equal(t, "new", s)
}
func TestTree_StoreBool(t *testing.T) {
const k, u = "test-store-bool", "usage"
// new fig tree with bool
figs := With(Options{Germinate: true})
figs.NewBool(k, false, u)
assert.Nil(t, figs.Parse())
// get a bool s
s := *figs.Bool(k)
assert.Equal(t, false, s)
// store a new bool in k
figs.StoreBool(k, true)
// verify new bool in k
s = *figs.Bool(k)
assert.Equal(t, true, s)
}
func TestTree_StoreInt(t *testing.T) {
const k, u = "test-store-int", "usage"
// new fig tree with int
figs := With(Options{Germinate: true})
figs.NewInt(k, 17, u)
assert.Nil(t, figs.Parse())
// get an int s
s := *figs.Int(k)
assert.Equal(t, 17, s)
// store a new int in k
figs.StoreInt(k, 76)
// verify new int in k
s = *figs.Int(k)
assert.Equal(t, 76, s)
}
func TestTree_StoreInt64(t *testing.T) {
const k, u = "test-store-int64", "usage"
// new fig tree with int64
figs := With(Options{Germinate: true})
figs.NewInt64(k, int64(17), u)
assert.Nil(t, figs.Parse())
// get an int64 s
s := *figs.Int64(k)
assert.Equal(t, int64(17), s)
// store a new int64 in k
figs.StoreInt64(k, 76)
// verify new int64 in k
s = *figs.Int64(k)
assert.Equal(t, int64(76), s)
}
func TestTree_StoreFloat64(t *testing.T) {
const k, u = "test-store-float64", "usage"
// new fig tree with float64
figs := With(Options{Germinate: true})
figs.NewFloat64(k, float64(17), u)
assert.Nil(t, figs.Parse())
// get a float64 s
s := *figs.Float64(k)
assert.Equal(t, float64(17), s)
// store a new float64 in k
figs.StoreFloat64(k, 76)
// verify new float64 in k
s = *figs.Float64(k)
assert.Equal(t, float64(76), s)
}
func TestTree_StoreDuration(t *testing.T) {
const k, u = "test-stoe-duration", "usage"
// new fig tree with time.Duration
figs := With(Options{Germinate: true})
figs.NewDuration(k, time.Duration(17), u)
assert.Nil(t, figs.Parse())
// get a time.Duration s from k
s := *figs.Duration(k)
assert.Equal(t, time.Duration(17), s)
// store a new time.Duration in k
figs.StoreDuration(k, time.Duration(76))
// verify time.Duration in k
s = *figs.Duration(k)
assert.Equal(t, time.Duration(76), s)
}
func TestTree_StoreUnitDuration(t *testing.T) {
const k, u = "test-store-unit-duration", "usage"
// new fig tree with unit duration
figs := With(Options{Germinate: true})
figs.NewUnitDuration(k, time.Duration(17), time.Second, u)
assert.Nil(t, figs.Parse())
// get a time.Duration with units s from k
s := *figs.UnitDuration(k)
assert.Equal(t, 17*time.Second, s)
// store a new time.Duration with units in k
figs.StoreUnitDuration(k, time.Duration(76), time.Minute)
// verify new time.Duration in k
s = *figs.UnitDuration(k)
assert.Equal(t, 76*time.Minute, s)
}
func TestTree_StoreList(t *testing.T) {
const k, u = "test-store-list", "usage"
// new fig tree with a list
figs := With(Options{Germinate: true})
figs = figs.NewList(k, []string{"yah", "i am", "yahuah"}, u)
assert.Nil(t, figs.Parse())
// get the list from k as s
s := *figs.List(k)
assert.Equal(t, 3, len(s))
assert.Equal(t, []string{"i am", "yah", "yahuah"}, s)
// store a new list in k
figs = figs.StoreList(k, []string{"yah", "its", "true", "he", "is"})
// verify the new list
s = *figs.List(k)
assert.Equal(t, 5, len(s))
assert.Equal(t, []string{"he", "is", "its", "true", "yah"}, s)
}
func TestTree_StoreMap(t *testing.T) {
const k, u = "test-store-map", "usage"
d := map[string]string{"name": "yahuah", "sex": "male"}
// new fig tree with a map as k
figs := With(Options{Germinate: true})
figs.NewMap(k, d, u)
assert.Nil(t, figs.Parse())
// get map k as s
s := *figs.Map(k)
assert.Equal(t, 2, len(s))
// verify name in map
n, nok := s["name"]
assert.True(t, nok)
assert.Equal(t, "yahuah", n)
// create a new map
a := map[string]string{"name": "andrei", "sex": "male"}
// replace the map of k that is d with a new map of a in k
figs.StoreMap(k, a)
// verify the map replacement
s = *figs.Map(k)
// check the name again for a change
n, nok = s["name"]
assert.True(t, nok)
assert.Equal(t, "andrei", n)
}