|
1 | 1 | package memory
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "github.com/stretchr/testify/assert" |
| 4 | + "github.com/shimmeringbee/persistence/impl/test" |
5 | 5 | "testing"
|
6 | 6 | )
|
7 | 7 |
|
8 |
| -func TestMemory_Keys(t *testing.T) { |
9 |
| - t.Run("added keys are returned", func(t *testing.T) { |
10 |
| - s := New() |
11 |
| - |
12 |
| - _ = s.Set("a", "one") |
13 |
| - _ = s.Set("b", "two") |
14 |
| - |
15 |
| - keys := s.Keys() |
16 |
| - assert.Len(t, keys, 2) |
17 |
| - assert.Contains(t, keys, "a") |
18 |
| - assert.Contains(t, keys, "b") |
19 |
| - }) |
20 |
| -} |
21 |
| - |
22 |
| -func TestMemory_Delete(t *testing.T) { |
23 |
| - t.Run("deleting a key removes it", func(t *testing.T) { |
24 |
| - s := New() |
25 |
| - |
26 |
| - _ = s.Set("a", "one") |
27 |
| - |
28 |
| - assert.Contains(t, s.Keys(), "a") |
29 |
| - |
30 |
| - assert.True(t, s.Delete("a")) |
31 |
| - |
32 |
| - assert.NotContains(t, s.Keys(), "a") |
33 |
| - }) |
34 |
| - |
35 |
| - t.Run("returns false if key not present", func(t *testing.T) { |
36 |
| - s := New() |
37 |
| - |
38 |
| - assert.False(t, s.Delete("a")) |
39 |
| - }) |
40 |
| -} |
41 |
| - |
42 |
| -func TestMemory_Bool(t *testing.T) { |
43 |
| - t.Run("can be set and retrieved", func(t *testing.T) { |
44 |
| - s := New() |
45 |
| - |
46 |
| - val, found := s.Bool("boolKey") |
47 |
| - assert.False(t, val) |
48 |
| - assert.False(t, found) |
49 |
| - |
50 |
| - val, found = s.Bool("boolKey", true) |
51 |
| - assert.True(t, val) |
52 |
| - assert.False(t, found) |
53 |
| - |
54 |
| - assert.NoError(t, s.Set("boolKey", true)) |
55 |
| - |
56 |
| - val, found = s.Bool("boolKey", true) |
57 |
| - assert.True(t, val) |
58 |
| - assert.True(t, found) |
59 |
| - }) |
60 |
| -} |
61 |
| - |
62 |
| -func TestMemory_Bytes(t *testing.T) { |
63 |
| - t.Run("can be set and retrieved", func(t *testing.T) { |
64 |
| - s := New() |
65 |
| - |
66 |
| - val, found := s.Bytes("bytesKey") |
67 |
| - assert.Nil(t, val) |
68 |
| - assert.False(t, found) |
69 |
| - |
70 |
| - val, found = s.Bytes("bytesKey", []byte{}) |
71 |
| - assert.Equal(t, []byte{}, val) |
72 |
| - assert.False(t, found) |
73 |
| - |
74 |
| - assert.NoError(t, s.Set("bytesKey", []byte{0x01})) |
75 |
| - |
76 |
| - val, found = s.Bytes("bytesKey", nil) |
77 |
| - assert.Equal(t, []byte{0x01}, val) |
78 |
| - assert.True(t, found) |
79 |
| - }) |
80 |
| -} |
81 |
| - |
82 |
| -func TestMemory_String(t *testing.T) { |
83 |
| - t.Run("can be set and retrieved", func(t *testing.T) { |
84 |
| - s := New() |
85 |
| - |
86 |
| - val, found := s.String("stringKey") |
87 |
| - assert.Equal(t, "", val) |
88 |
| - assert.False(t, found) |
89 |
| - |
90 |
| - val, found = s.String("stringKey", "none") |
91 |
| - assert.Equal(t, "none", val) |
92 |
| - assert.False(t, found) |
93 |
| - |
94 |
| - assert.NoError(t, s.Set("stringKey", "test")) |
95 |
| - |
96 |
| - val, found = s.String("stringKey", "other") |
97 |
| - assert.Equal(t, "test", val) |
98 |
| - assert.True(t, found) |
99 |
| - }) |
100 |
| -} |
101 |
| - |
102 |
| -func TestMemory_Float(t *testing.T) { |
103 |
| - t.Run("can be set and retrieved", func(t *testing.T) { |
104 |
| - s := New() |
105 |
| - |
106 |
| - val, found := s.Float("float64Key") |
107 |
| - assert.Equal(t, 0.0, val) |
108 |
| - assert.False(t, found) |
109 |
| - |
110 |
| - val, found = s.Float("float64Key", 0.1) |
111 |
| - assert.Equal(t, 0.1, val) |
112 |
| - assert.False(t, found) |
113 |
| - |
114 |
| - assert.NoError(t, s.Set("float64Key", 0.2)) |
115 |
| - |
116 |
| - val, found = s.Float("float64Key", 0.1) |
117 |
| - assert.Equal(t, 0.2, val) |
118 |
| - assert.True(t, found) |
119 |
| - |
120 |
| - assert.NoError(t, s.Set("float32Key", float32(0.2))) |
121 |
| - |
122 |
| - val, found = s.Float("float32Key", 0.1) |
123 |
| - assert.InDelta(t, 0.2, val, 0.0001) |
124 |
| - assert.True(t, found) |
125 |
| - }) |
126 |
| -} |
127 |
| - |
128 |
| -func TestMemory_Int(t *testing.T) { |
129 |
| - t.Run("can be set and retrieved", func(t *testing.T) { |
130 |
| - s := New() |
131 |
| - |
132 |
| - val, found := s.Int("intKey") |
133 |
| - assert.Equal(t, int64(0), val) |
134 |
| - assert.False(t, found) |
135 |
| - |
136 |
| - val, found = s.Int("intKey", 1) |
137 |
| - assert.Equal(t, int64(1), val) |
138 |
| - assert.False(t, found) |
139 |
| - |
140 |
| - assert.NoError(t, s.Set("intKey", 2)) |
141 |
| - |
142 |
| - val, found = s.Int("intKey", 1) |
143 |
| - assert.Equal(t, int64(2), val) |
144 |
| - assert.True(t, found) |
145 |
| - |
146 |
| - assert.NoError(t, s.Set("int8Key", int8(2))) |
147 |
| - |
148 |
| - val, found = s.Int("int8Key", 1) |
149 |
| - assert.Equal(t, int64(2), val) |
150 |
| - assert.True(t, found) |
151 |
| - |
152 |
| - assert.NoError(t, s.Set("int16Key", int16(2))) |
153 |
| - |
154 |
| - val, found = s.Int("int16Key", 1) |
155 |
| - assert.Equal(t, int64(2), val) |
156 |
| - assert.True(t, found) |
157 |
| - |
158 |
| - assert.NoError(t, s.Set("int32Key", int32(2))) |
159 |
| - |
160 |
| - val, found = s.Int("int32Key", 1) |
161 |
| - assert.Equal(t, int64(2), val) |
162 |
| - assert.True(t, found) |
163 |
| - |
164 |
| - assert.NoError(t, s.Set("int64Key", int64(2))) |
165 |
| - |
166 |
| - val, found = s.Int("int64Key", 1) |
167 |
| - assert.Equal(t, int64(2), val) |
168 |
| - assert.True(t, found) |
169 |
| - }) |
170 |
| -} |
171 |
| - |
172 |
| -func TestMemory_UInt(t *testing.T) { |
173 |
| - t.Run("can be set and retrieved", func(t *testing.T) { |
174 |
| - s := New() |
175 |
| - |
176 |
| - val, found := s.UInt("intKey") |
177 |
| - assert.Equal(t, uint64(0), val) |
178 |
| - assert.False(t, found) |
179 |
| - |
180 |
| - val, found = s.UInt("intKey", 1) |
181 |
| - assert.Equal(t, uint64(1), val) |
182 |
| - assert.False(t, found) |
183 |
| - |
184 |
| - assert.NoError(t, s.Set("intKey", uint(2))) |
185 |
| - |
186 |
| - val, found = s.UInt("intKey", 1) |
187 |
| - assert.Equal(t, uint64(2), val) |
188 |
| - assert.True(t, found) |
189 |
| - |
190 |
| - assert.NoError(t, s.Set("int8Key", uint8(2))) |
191 |
| - |
192 |
| - val, found = s.UInt("int8Key", 1) |
193 |
| - assert.Equal(t, uint64(2), val) |
194 |
| - assert.True(t, found) |
195 |
| - |
196 |
| - assert.NoError(t, s.Set("int16Key", uint16(2))) |
197 |
| - |
198 |
| - val, found = s.UInt("int16Key", 1) |
199 |
| - assert.Equal(t, uint64(2), val) |
200 |
| - assert.True(t, found) |
201 |
| - |
202 |
| - assert.NoError(t, s.Set("int32Key", uint32(2))) |
203 |
| - |
204 |
| - val, found = s.UInt("int32Key", 1) |
205 |
| - assert.Equal(t, uint64(2), val) |
206 |
| - assert.True(t, found) |
207 |
| - |
208 |
| - assert.NoError(t, s.Set("int64Key", uint64(2))) |
209 |
| - |
210 |
| - val, found = s.UInt("int64Key", 1) |
211 |
| - assert.Equal(t, uint64(2), val) |
212 |
| - assert.True(t, found) |
213 |
| - }) |
214 |
| -} |
215 |
| - |
216 |
| -func TestMemory_Section(t *testing.T) { |
217 |
| - t.Run("a chained section can be created and persists upon retrieval", func(t *testing.T) { |
218 |
| - s := New() |
219 |
| - |
220 |
| - cs := s.Section("tier1", "tier2") |
221 |
| - _ = cs.Set("key", "value") |
222 |
| - |
223 |
| - assert.Contains(t, s.SectionKeys(), "tier1") |
224 |
| - |
225 |
| - t1 := s.Section("tier1") |
226 |
| - |
227 |
| - assert.Contains(t, t1.SectionKeys(), "tier2") |
228 |
| - |
229 |
| - t2 := t1.Section("tier2") |
230 |
| - |
231 |
| - v, _ := t2.String("key") |
232 |
| - assert.Equal(t, "value", v) |
233 |
| - }) |
234 |
| -} |
235 |
| - |
236 |
| -func TestMemory_SectionKeys(t *testing.T) { |
237 |
| - t.Run("seconds can be listed", func(t *testing.T) { |
238 |
| - s := New() |
239 |
| - |
240 |
| - s.Section("one") |
241 |
| - s.Section("two") |
242 |
| - |
243 |
| - assert.Contains(t, s.SectionKeys(), "one") |
244 |
| - assert.Contains(t, s.SectionKeys(), "two") |
245 |
| - }) |
246 |
| -} |
247 |
| - |
248 |
| -func TestMemory_DeleteSection(t *testing.T) { |
249 |
| - t.Run("seconds can be deleted", func(t *testing.T) { |
250 |
| - s := New() |
251 |
| - |
252 |
| - s.Section("one") |
253 |
| - assert.Contains(t, s.SectionKeys(), "one") |
254 |
| - |
255 |
| - s.SectionDelete("one") |
256 |
| - assert.NotContains(t, s.SectionKeys(), "one") |
257 |
| - }) |
258 |
| -} |
259 |
| - |
260 |
| -func TestMemory_Exists(t *testing.T) { |
261 |
| - t.Run("returns if a key exists", func(t *testing.T) { |
262 |
| - s := New() |
263 |
| - |
264 |
| - _ = s.Set("key", "value") |
265 |
| - assert.True(t, s.Exists("key")) |
266 |
| - assert.False(t, s.Exists("otherKey")) |
267 |
| - }) |
268 |
| -} |
269 |
| - |
270 |
| -func TestMemory_SectionExists(t *testing.T) { |
271 |
| - t.Run("returns if a section exists", func(t *testing.T) { |
272 |
| - s := New() |
273 |
| - |
274 |
| - _ = s.Section("key") |
275 |
| - assert.True(t, s.SectionExists("key")) |
276 |
| - assert.False(t, s.SectionExists("otherKey")) |
277 |
| - }) |
278 |
| -} |
279 |
| - |
280 |
| -func TestMemory_SectionKeyNotClash(t *testing.T) { |
281 |
| - t.Run("ensure that keys and sections dont shared the same name space", func(t *testing.T) { |
282 |
| - s := New() |
283 |
| - |
284 |
| - s.Section("key") |
285 |
| - s.Section("key2") |
286 |
| - s.Set("key", 42) |
287 |
| - s.Set("key3", 42) |
288 |
| - |
289 |
| - actualKeyInt, _ := s.Int("key") |
290 |
| - assert.Equal(t, int64(42), actualKeyInt) |
291 |
| - |
292 |
| - assert.Contains(t, s.Keys(), "key") |
293 |
| - assert.NotContains(t, s.Keys(), "key2") |
294 |
| - assert.Contains(t, s.Keys(), "key3") |
295 |
| - |
296 |
| - assert.Contains(t, s.SectionKeys(), "key") |
297 |
| - assert.Contains(t, s.SectionKeys(), "key2") |
298 |
| - assert.NotContains(t, s.SectionKeys(), "key3") |
299 |
| - }) |
| 8 | +func TestMemory(t *testing.T) { |
| 9 | + test.Impl{New: New}.Test(t) |
300 | 10 | }
|
0 commit comments