-
Notifications
You must be signed in to change notification settings - Fork 28
/
storage_test.go
123 lines (96 loc) · 2.78 KB
/
storage_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 joe
import (
"bytes"
"encoding/gob"
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
)
func TestStorage(t *testing.T) {
logger := zaptest.NewLogger(t)
store := NewStorage(logger)
ok, err := store.Get("test", nil)
assert.NoError(t, err)
assert.False(t, ok)
err = store.Set("test", "foo")
assert.NoError(t, err)
err = store.Set("test", "foo")
assert.NoError(t, err, "setting a key more than once should not error")
err = store.Set("test-2", "bar")
assert.NoError(t, err)
keys, err := store.Keys()
assert.NoError(t, err)
assert.Equal(t, []string{"test", "test-2"}, keys)
ok, err = store.Get("test", nil)
assert.NoError(t, err, "getting a key without a target to unmarshal to should not fail")
assert.True(t, ok)
var val string
ok, err = store.Get("test", &val)
assert.NoError(t, err)
assert.True(t, ok)
assert.Equal(t, "foo", val)
ok, err = store.Delete("does-not-exist")
assert.NoError(t, err)
assert.False(t, ok)
ok, err = store.Delete("test")
assert.NoError(t, err)
assert.True(t, ok)
ok, err = store.Get("test", nil)
assert.NoError(t, err)
assert.False(t, ok)
assert.NoError(t, store.Close())
}
func TestStorage_Encoder(t *testing.T) {
logger := zaptest.NewLogger(t)
enc := new(gobEncoder)
store := NewStorage(logger)
store.SetMemoryEncoder(enc)
val := []string{"foo", "bar"}
err := store.Set("test", val)
require.NoError(t, err)
var actual []string
ok, err := store.Get("test", &actual)
require.NoError(t, err)
assert.True(t, ok)
assert.Equal(t, val, actual)
}
func TestStorage_EncoderErrors(t *testing.T) {
logger := zaptest.NewLogger(t)
enc := new(gobEncoder)
store := NewStorage(logger)
store.SetMemoryEncoder(enc)
err := store.Set("test", "ok")
require.NoError(t, err, "should insert the first value without an error")
enc.encodeErr = errors.New("something went wrong")
err = store.Set("test", "foo")
assert.EqualError(t, err, "encode data: something went wrong")
var actual []string
enc.decodeErr = errors.New("this did not work")
ok, err := store.Get("test", &actual)
assert.EqualError(t, err, "decode data: this did not work")
assert.False(t, ok)
}
// gobEncoder is an example of a different encoder. This is not part of joe to
// avoid the extra import in production code.
type gobEncoder struct {
encodeErr error
decodeErr error
}
func (e gobEncoder) Encode(value interface{}) ([]byte, error) {
if err := e.encodeErr; err != nil {
return nil, err
}
data := new(bytes.Buffer)
enc := gob.NewEncoder(data)
err := enc.Encode(value)
return data.Bytes(), err
}
func (e gobEncoder) Decode(data []byte, target interface{}) error {
if err := e.decodeErr; err != nil {
return err
}
enc := gob.NewDecoder(bytes.NewBuffer(data))
return enc.Decode(target)
}