-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_test.go
102 lines (74 loc) · 1.67 KB
/
cache_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
package urna
import (
"slices"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCacheGetSet(t *testing.T) {
testCases := []struct {
description string
key string
keyToGet string
value string
ok bool
want string
}{
{"just suceed to s and retrieve key", "key1", "key1", "value", true, "value"},
{"should fail because key dont exist", "key2", "wrong-key", "value", false, ""},
}
c := NewCache()
for _, tt := range testCases {
t.Run(tt.description, func(t *testing.T) {
c.Set(tt.key, tt.value)
val, ok := c.Get(tt.keyToGet)
assert.Equal(t, tt.ok, ok)
assert.Equal(t, tt.want, val)
})
}
}
func TestCacheSetMultiple(t *testing.T) {
c := NewCache()
s := map[string]string{
"key": "value",
"key2": "value2",
}
assert.Equal(t, c.Len(), 0)
c.SetMultiple(s)
assert.Equal(t, c.Len(), 2)
}
func TestCacheGetMultiple(t *testing.T) {
c := NewCache()
s := map[string]string{
"key": "value",
"key2": "value2",
}
assert.Equal(t, c.Len(), 0)
c.SetMultiple(s)
assert.Equal(t, c.Len(), 2)
results := c.GetMultiple([]string{"key", "key2"})
for _, v := range s {
assert.Equal(t, true, slices.Contains(results, v))
}
}
func TestCacheClear(t *testing.T) {
c := NewCache()
c.Set("key", "value")
c.Set("key2", "value2")
assert.Equal(t, c.Len(), 2)
c.Clear()
assert.Equal(t, c.Len(), 0)
}
func TestCacheDelete(t *testing.T) {
c := NewCache()
c.Set("key", "value")
c.Set("key2", "value2")
assert.Equal(t, c.Len(), 2)
c.Delete("key2")
assert.Equal(t, c.Len(), 1)
}
func TestCacheLen(t *testing.T) {
c := NewCache()
assert.Equal(t, c.Len(), 0)
c.Set("key", "value")
assert.Equal(t, c.Len(), 1)
}