-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_test.go
75 lines (65 loc) · 1.55 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
package merge
import (
"testing"
)
func TestMapCache_Get(t *testing.T) {
cache := NewCache()
cache.Set("key1", "data1")
cache.Set("key2", "data2")
t.Run("Existing key", func(t *testing.T) {
data, ok := cache.Get("key1")
if !ok {
t.Errorf("Get returned false for an existing key")
}
if data != "data1" {
t.Errorf("Get returned %s, want data1", data)
}
})
t.Run("Non-existing key", func(t *testing.T) {
data, ok := cache.Get("key3")
if ok {
t.Errorf("Get returned true for a non-existing key")
}
if data != "" {
t.Errorf("Get returned %s, want empty string", data)
}
})
}
func TestMapCache_Set(t *testing.T) {
cache := NewCache()
t.Run("Set key and data", func(t *testing.T) {
cache.Set("key1", "data1")
data, ok := cache.Get("key1")
if !ok {
t.Errorf("Get returned false for an existing key")
}
if data != "data1" {
t.Errorf("Get returned %s, want data1", data)
}
})
t.Run("Update existing key", func(t *testing.T) {
cache.Set("key1", "updatedData")
data, ok := cache.Get("key1")
if !ok {
t.Errorf("Get returned false for an existing key")
}
if data != "updatedData" {
t.Errorf("Get returned %s, want updatedData", data)
}
})
}
func TestMapCache_Clear(t *testing.T) {
cache := NewCache()
cache.Set("key1", "data1")
cache.Set("key2", "data2")
cache.Clear()
t.Run("Get after clear", func(t *testing.T) {
data, ok := cache.Get("key1")
if ok {
t.Errorf("Get returned true after clearing the cache")
}
if data != "" {
t.Errorf("Get returned %s, want empty string", data)
}
})
}