-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialization_test.go
123 lines (98 loc) · 2.75 KB
/
serialization_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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package cache
import (
"bytes"
"encoding/gob"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCache_Save(t *testing.T) {
t.Run("Save cache items to io.Writer", func(t *testing.T) {
c := New(DefaultExpiration, 0)
c.Set("key1", "value1", NoExpiration)
c.Set("key2", "value2", NoExpiration)
var buf bytes.Buffer
err := c.Save(&buf)
assert.NoError(t, err)
// Decode the saved data to verify
dec := gob.NewDecoder(&buf)
items := map[string]Item{}
err = dec.Decode(&items)
assert.NoError(t, err)
assert.Equal(t, 2, len(items))
assert.Equal(t, "value1", items["key1"].Object)
assert.Equal(t, "value2", items["key2"].Object)
})
}
func TestCache_SaveFile(t *testing.T) {
t.Run("Save cache items to file", func(t *testing.T) {
c := New(DefaultExpiration, 0)
c.Set("key1", "value1", NoExpiration)
c.Set("key2", "value2", NoExpiration)
fileName := "test_cache_save.gob"
defer os.Remove(fileName) // Clean up
err := c.SaveFile(fileName)
assert.NoError(t, err)
// Load the file and verify its contents
file, err := os.Open(fileName)
assert.NoError(t, err)
defer file.Close()
dec := gob.NewDecoder(file)
items := map[string]Item{}
err = dec.Decode(&items)
assert.NoError(t, err)
assert.Equal(t, 2, len(items))
assert.Equal(t, "value1", items["key1"].Object)
assert.Equal(t, "value2", items["key2"].Object)
})
}
func TestCache_Load(t *testing.T) {
t.Run("Load cache items from io.Reader", func(t *testing.T) {
c := New(DefaultExpiration, 0)
// Prepare data to load
items := map[string]Item{
"key1": {Object: "value1", Expiration: 0},
"key2": {Object: "value2", Expiration: 0},
}
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(&items)
assert.NoError(t, err)
err = c.Load(&buf)
assert.NoError(t, err)
// Verify loaded data
val, found := c.Get("key1")
assert.True(t, found)
assert.Equal(t, "value1", val)
val, found = c.Get("key2")
assert.True(t, found)
assert.Equal(t, "value2", val)
})
}
func TestCache_LoadFile(t *testing.T) {
t.Run("Load cache items from file", func(t *testing.T) {
c := New(DefaultExpiration, 0)
// Prepare data to load
items := map[string]Item{
"key1": {Object: "value1", Expiration: 0},
"key2": {Object: "value2", Expiration: 0},
}
fileName := "test_cache_load.gob"
defer os.Remove(fileName) // Clean up
file, err := os.Create(fileName)
assert.NoError(t, err)
enc := gob.NewEncoder(file)
err = enc.Encode(&items)
assert.NoError(t, err)
file.Close()
err = c.LoadFile(fileName)
assert.NoError(t, err)
// Verify loaded data
val, found := c.Get("key1")
assert.True(t, found)
assert.Equal(t, "value1", val)
val, found = c.Get("key2")
assert.True(t, found)
assert.Equal(t, "value2", val)
})
}