-
Notifications
You must be signed in to change notification settings - Fork 1
/
memory_test.go
99 lines (83 loc) · 2.07 KB
/
memory_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
package file
import (
"fmt"
"os"
"path"
"testing"
"time"
"github.com/go-joe/joe"
"github.com/stretchr/testify/require"
)
// noinspection GoUnhandledErrorResult
func withTempFile(t *testing.T, fun func(mem joe.Memory)) {
tempFile := path.Join(os.TempDir(), fmt.Sprintf("test_%d_%d", os.Getpid(), time.Now().UnixNano()))
defer os.Remove(tempFile)
mem, err := NewMemory(tempFile)
require.NoError(t, err)
fun(mem)
err = mem.Close()
require.NoError(t, err)
}
func TestMemory_Set(t *testing.T) {
withTempFile(t, func(mem joe.Memory) {
// set a value
err := mem.Set("foo", []byte("bar"))
require.NoError(t, err)
})
}
func TestMemory_Get(t *testing.T) {
withTempFile(t, func(mem joe.Memory) {
// empty value
val, found, err := mem.Get("foo")
require.Nil(t, val)
require.False(t, found)
require.NoError(t, err)
// set a value
err = mem.Set("foo", []byte("bar"))
require.NoError(t, err)
// value found
val, found, err = mem.Get("foo")
require.EqualValues(t, []byte("bar"), val)
require.True(t, found)
require.NoError(t, err)
})
}
func TestMemory_Delete(t *testing.T) {
withTempFile(t, func(mem joe.Memory) {
// set a value
err := mem.Set("foo", []byte("bar"))
require.NoError(t, err)
found, err := mem.Delete("foo")
require.NoError(t, err)
require.True(t, found)
// value is gone
val, found, err := mem.Get("foo")
require.Nil(t, val)
require.False(t, found)
require.NoError(t, err)
})
}
func TestMemory_Delete_NoneAffected(t *testing.T) {
withTempFile(t, func(mem joe.Memory) {
ok, err := mem.Delete("foo")
require.NoError(t, err)
require.False(t, ok)
})
}
func TestMemory_Keys(t *testing.T) {
withTempFile(t, func(mem joe.Memory) {
keys := []string{"foo1", "foo2", "foo3"}
for _, k := range keys {
require.NoError(t, mem.Set(k, []byte(k+" value")))
}
foundKeys, err := mem.Keys()
require.NoError(t, err)
require.EqualValues(t, keys, foundKeys)
for _, k := range foundKeys {
v, ok, err := mem.Get(k)
require.True(t, ok)
require.NoError(t, err)
require.EqualValues(t, []byte(k+" value"), v)
}
})
}