-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_test.go
More file actions
326 lines (267 loc) · 10.4 KB
/
Copy pathcache_test.go
File metadata and controls
326 lines (267 loc) · 10.4 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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package cache
import (
"context"
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// mockDriver is a mock implementation of Driver for testing
type mockDriver[Value any] struct {
storage map[Key]Value
getErr error
setErr error
hasErr error
delErr error
}
func newMockDriver[Value any]() *mockDriver[Value] {
return &mockDriver[Value]{
storage: make(map[Key]Value),
}
}
func (m *mockDriver[Value]) Get(_ context.Context, key Key) (Value, bool, error) { //nolint:ireturn
if m.getErr != nil {
var zero Value
return zero, false, m.getErr
}
val, ok := m.storage[key]
return val, ok, nil
}
func (m *mockDriver[Value]) Set(_ context.Context, key Key, value Value, _ TTL) error {
if m.setErr != nil {
return m.setErr
}
m.storage[key] = value
return nil
}
func (m *mockDriver[Value]) Has(_ context.Context, key Key) (bool, error) {
if m.hasErr != nil {
return false, m.hasErr
}
_, ok := m.storage[key]
return ok, nil
}
func (m *mockDriver[Value]) Delete(_ context.Context, key Key) error {
if m.delErr != nil {
return m.delErr
}
delete(m.storage, key)
return nil
}
func TestNewCache(t *testing.T) {
driver := newMockDriver[string]()
cache := NewCache(driver)
require.NotNil(t, cache, "NewCache should return non-nil cache")
assert.Equal(t, driver, cache.Driver(), "Cache should return the same driver")
}
func TestCache_Get(t *testing.T) {
ctx := t.Context()
driver := newMockDriver[string]()
cache := NewCache(driver)
// Test Get with non-existing key
val, found, err := cache.Get(ctx, "non-existing")
require.NoError(t, err, "Get should not return error for non-existing key")
assert.False(t, found, "Get should return false for non-existing key")
assert.Empty(t, val, "Get should return zero value for non-existing key")
// Test Get with existing key
driver.storage["existing"] = "value"
val, found, err = cache.Get(ctx, "existing")
require.NoError(t, err, "Get should not return error for existing key")
assert.True(t, found, "Get should return true for existing key")
assert.Equal(t, "value", val, "Get should return correct value")
}
func TestCache_Set(t *testing.T) {
ctx := t.Context()
driver := newMockDriver[string]()
cache := NewCache(driver)
// Test Set with default TTL
err := cache.Set(ctx, "key1", "value1", DefaultTTL)
require.NoError(t, err, "Set should not fail")
assert.Equal(t, "value1", driver.storage["key1"], "Set should store value in driver")
// Test Set with custom TTL
err = cache.Set(ctx, "key2", "value2", WithTTL(5*time.Minute))
require.NoError(t, err, "Set should not fail with custom TTL")
assert.Equal(t, "value2", driver.storage["key2"], "Set should store value with custom TTL")
// Test Set with Forever
err = cache.Set(ctx, "key3", "value3", Forever)
require.NoError(t, err, "Set should not fail with Forever")
assert.Equal(t, "value3", driver.storage["key3"], "Set should store value with Forever")
// Test Set error handling
driver.setErr = errors.New("set error")
err = cache.Set(ctx, "key4", "value4", DefaultTTL)
require.Error(t, err, "Set should return error when driver.Set fails")
assert.Equal(t, "set error", err.Error(), "Set should return driver error")
}
func TestCache_Has(t *testing.T) {
ctx := t.Context()
driver := newMockDriver[string]()
cache := NewCache(driver)
// Test Has with non-existing key
has, err := cache.Has(ctx, "non-existing")
require.NoError(t, err, "Has should not return error for non-existing key")
assert.False(t, has, "Has should return false for non-existing key")
// Test Has with existing key
driver.storage["existing"] = "value"
has, err = cache.Has(ctx, "existing")
require.NoError(t, err, "Has should not return error for existing key")
assert.True(t, has, "Has should return true for existing key")
}
func TestCache_Delete(t *testing.T) {
ctx := t.Context()
driver := newMockDriver[string]()
cache := NewCache(driver)
// Test Delete with existing key
driver.storage["to-delete"] = "value"
has, err := driver.Has(ctx, "to-delete")
require.NoError(t, err, "Has should not return error")
require.True(t, has, "Key should exist before delete")
err = cache.Delete(ctx, "to-delete")
require.NoError(t, err, "Delete should not fail")
has, err = driver.Has(ctx, "to-delete")
require.NoError(t, err, "Has should not return error")
assert.False(t, has, "Key should not exist after delete")
// Test Delete with non-existing key (should not error)
err = cache.Delete(ctx, "non-existing")
require.NoError(t, err, "Delete should not fail for non-existing key")
// Test Delete error handling
driver.delErr = errors.New("delete error")
err = cache.Delete(ctx, "key")
require.Error(t, err, "Delete should return error when driver.Delete fails")
assert.Equal(t, "delete error", err.Error(), "Delete should return driver error")
}
func TestCache_Remember(t *testing.T) {
ctx := t.Context()
t.Run("Cache hit - returns cached value", func(t *testing.T) {
driver := newMockDriver[string]()
cache := NewCache(driver)
// Pre-populate cache
driver.storage["cached"] = "cached-value"
fetchCount := 0
fetch := func(_ context.Context) (string, error) {
fetchCount++
return "fetched-value", nil
}
val, err := cache.Remember(ctx, "cached", DefaultTTL, fetch)
require.NoError(t, err, "Remember should not fail")
assert.Equal(t, "cached-value", val, "Remember should return cached value")
assert.Equal(t, 0, fetchCount, "Fetch should not be called when cache hit")
})
t.Run("Cache miss - fetches and stores value", func(t *testing.T) {
driver := newMockDriver[string]()
cache := NewCache(driver)
fetchCount := 0
fetch := func(_ context.Context) (string, error) {
fetchCount++
return "fetched-value", nil
}
val, err := cache.Remember(ctx, "miss", DefaultTTL, fetch)
require.NoError(t, err, "Remember should not fail")
assert.Equal(t, "fetched-value", val, "Remember should return fetched value")
assert.Equal(t, 1, fetchCount, "Fetch should be called once")
// Verify value was stored in cache
cachedVal, found, err := driver.Get(ctx, "miss")
require.NoError(t, err, "Get should not return error")
assert.True(t, found, "Value should be stored in cache")
assert.Equal(t, "fetched-value", cachedVal, "Cached value should match")
})
t.Run("Fetch error - returns error without caching", func(t *testing.T) {
driver := newMockDriver[string]()
cache := NewCache(driver)
fetchErr := errors.New("fetch failed")
fetch := func(_ context.Context) (string, error) {
return "", fetchErr
}
val, err := cache.Remember(ctx, "error-key", DefaultTTL, fetch)
require.Error(t, err, "Remember should return error when fetch fails")
assert.Contains(t, err.Error(), "fetch value", "Error should contain 'fetch value'")
assert.Contains(t, err.Error(), "fetch failed", "Error should wrap fetch error")
assert.Empty(t, val, "Remember should return zero value on error")
// Verify nothing was stored in cache
_, found, err := driver.Get(ctx, "error-key")
require.NoError(t, err, "Get should not return error")
assert.False(t, found, "Value should not be stored when fetch fails")
})
t.Run("Set error after fetch - returns value but wraps error", func(t *testing.T) {
driver := newMockDriver[string]()
driver.setErr = errors.New("cache set failed")
cache := NewCache(driver)
fetch := func(_ context.Context) (string, error) {
return "fetched-value", nil
}
val, err := cache.Remember(ctx, "set-error", DefaultTTL, fetch)
require.Error(t, err, "Remember should return error when Set fails")
require.ErrorIs(t, err, ErrCacheSetFailed, "Error should wrap ErrCacheSetFailed")
assert.Equal(t, "fetched-value", val, "Remember should return fetched value even if Set fails")
assert.Contains(t, err.Error(), "cache set failed", "Error should contain Set error")
})
t.Run("Remember with different TTL values", func(t *testing.T) {
driver := newMockDriver[string]()
cache := NewCache(driver)
fetch := func(_ context.Context) (string, error) {
return "value", nil
}
// Test with DefaultTTL
_, err := cache.Remember(ctx, "default-ttl", DefaultTTL, fetch)
require.NoError(t, err, "Remember should work with DefaultTTL")
// Test with custom TTL
_, err = cache.Remember(ctx, "custom-ttl", WithTTL(5*time.Minute), fetch)
require.NoError(t, err, "Remember should work with custom TTL")
// Test with Forever
_, err = cache.Remember(ctx, "forever-ttl", Forever, fetch)
require.NoError(t, err, "Remember should work with Forever")
})
t.Run("Remember with complex type", func(t *testing.T) {
type User struct {
ID int
Name string
}
driver := newMockDriver[User]()
cache := NewCache(driver)
fetchCount := 0
expectedUser := User{ID: 123, Name: "John"}
fetch := func(_ context.Context) (User, error) {
fetchCount++
return expectedUser, nil
}
// First call - cache miss
user, err := cache.Remember(ctx, "user:123", DefaultTTL, fetch)
require.NoError(t, err, "Remember should not fail")
assert.Equal(t, expectedUser, user, "Remember should return correct user")
assert.Equal(t, 1, fetchCount, "Fetch should be called once")
// Second call - cache hit
user, err = cache.Remember(ctx, "user:123", DefaultTTL, fetch)
require.NoError(t, err, "Remember should not fail")
assert.Equal(t, expectedUser, user, "Remember should return cached user")
assert.Equal(t, 1, fetchCount, "Fetch should not be called again")
})
}
func TestCache_Driver(t *testing.T) {
driver := newMockDriver[string]()
cache := NewCache(driver)
returnedDriver := cache.Driver()
assert.Equal(t, driver, returnedDriver, "Driver should return the same driver instance")
}
func TestTTL(t *testing.T) {
t.Run("DefaultTTL", func(t *testing.T) {
assert.True(t, DefaultTTL.useDefault, "DefaultTTL should have useDefault=true")
})
t.Run("Forever", func(t *testing.T) {
assert.False(t, Forever.useDefault, "Forever should have useDefault=false")
assert.Equal(t, time.Duration(0), Forever.duration, "Forever should have duration=0")
})
t.Run("WithTTL", func(t *testing.T) {
ttl := WithTTL(5 * time.Minute)
assert.False(t, ttl.useDefault, "WithTTL should have useDefault=false")
assert.Equal(t, 5*time.Minute, ttl.duration, "WithTTL should set correct duration")
})
}
func TestCache_InterfaceCompliance(t *testing.T) {
// Test that cache implements Cacher interface
var _ Cacher[string] = (*Cache[string])(nil)
var _ Cacher[int] = (*Cache[int])(nil)
type User struct {
ID int
}
var _ Cacher[User] = (*Cache[User])(nil)
}