/
marshaler_test.go
150 lines (111 loc) · 2.89 KB
/
marshaler_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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package marshaler
import (
"errors"
"testing"
"time"
"github.com/eko/gocache/store"
mocksCache "github.com/eko/gocache/test/mocks/cache"
"github.com/stretchr/testify/assert"
"github.com/vmihailenco/msgpack"
)
type testCacheValue struct {
Hello string
}
func TestNew(t *testing.T) {
// Given
cache := &mocksCache.CacheInterface{}
// When
marshaler := New(cache)
// Then
assert.IsType(t, new(Marshaler), marshaler)
assert.Equal(t, cache, marshaler.cache)
}
func TestGetWhenStoreReturnsSliceOfBytes(t *testing.T) {
// Given
cacheValue := &testCacheValue{
Hello: "world",
}
cacheValueBytes, err := msgpack.Marshal(cacheValue)
if err != nil {
assert.Error(t, err)
}
cache := &mocksCache.CacheInterface{}
cache.On("Get", "my-key").Return(cacheValueBytes, nil)
marshaler := New(cache)
// When
value, err := marshaler.Get("my-key", new(testCacheValue))
// Then
assert.Nil(t, err)
assert.Equal(t, cacheValue, value)
}
func TestGetWhenStoreReturnsString(t *testing.T) {
// Given
cacheValue := &testCacheValue{
Hello: "world",
}
cacheValueBytes, err := msgpack.Marshal(cacheValue)
if err != nil {
assert.Error(t, err)
}
cache := &mocksCache.CacheInterface{}
cache.On("Get", "my-key").Return(string(cacheValueBytes), nil)
marshaler := New(cache)
// When
value, err := marshaler.Get("my-key", new(testCacheValue))
// Then
assert.Nil(t, err)
assert.Equal(t, cacheValue, value)
}
func TestGetWhenUnmarshalingError(t *testing.T) {
// Given
cache := &mocksCache.CacheInterface{}
cache.On("Get", "my-key").Return("unknown-string", nil)
marshaler := New(cache)
// When
value, err := marshaler.Get("my-key", new(testCacheValue))
// Then
assert.NotNil(t, err)
assert.Nil(t, value)
}
func TestGetWhenNotFoundInStore(t *testing.T) {
// Given
expectedErr := errors.New("Unable to find item in store")
cache := &mocksCache.CacheInterface{}
cache.On("Get", "my-key").Return(nil, expectedErr)
marshaler := New(cache)
// When
value, err := marshaler.Get("my-key", new(testCacheValue))
// Then
assert.Equal(t, expectedErr, err)
assert.Nil(t, value)
}
func TestSetWhenStruct(t *testing.T) {
// Given
cacheValue := &testCacheValue{
Hello: "world",
}
options := &store.Options{
Expiration: 5 * time.Second,
}
cache := &mocksCache.CacheInterface{}
cache.On("Set", "my-key", []byte{0x81, 0xa5, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0xa5, 0x77, 0x6f, 0x72, 0x6c, 0x64}, options).Return(nil)
marshaler := New(cache)
// When
err := marshaler.Set("my-key", cacheValue, options)
// Then
assert.Nil(t, err)
}
func TestSetWhenString(t *testing.T) {
// Given
cacheValue := "test"
options := &store.Options{
Expiration: 5 * time.Second,
}
cache := &mocksCache.CacheInterface{}
cache.On("Set", "my-key", []byte{0xa4, 0x74, 0x65, 0x73, 0x74}, options).Return(nil)
marshaler := New(cache)
// When
err := marshaler.Set("my-key", cacheValue, options)
// Then
assert.Nil(t, err)
}