-
Notifications
You must be signed in to change notification settings - Fork 0
/
rediscache_test.go
79 lines (71 loc) · 2.18 KB
/
rediscache_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
package rediscache
import (
"context"
"errors"
"github.com/go-redis/redismock/v8"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)
func Test_NewRedisCache(t *testing.T) {
NewRedisCache(1, Options{})
assert.Equal(t, true, true)
}
func Test_Get(t *testing.T) {
type testObject struct {
Name string `json:"name"`
Surname string `json:"surname"`
}
var cacheMock cache
db, redisMockClient := redismock.NewClientMock()
t.Run("Get returns the value", func(t *testing.T) {
redisMockClient.ExpectGet("123456").SetVal(`{"name":"John","surname":"Doe"}`)
cacheMock.client = db
actualObject, err := cacheMock.Get(context.Background(), "123456", testObject{})
require.Nil(t, err)
assert.Equal(t, testObject{
Name: "John",
Surname: "Doe",
}, actualObject)
})
t.Run("Get returns the error on client.Get request", func(t *testing.T) {
redisMockClient.ExpectGet("123456").SetErr(errors.New("error"))
cacheMock.client = db
actualObject, err := cacheMock.Get(context.Background(), "123456", testObject{})
require.Error(t, err)
assert.Equal(t, testObject{}, actualObject)
})
t.Run("Get returns the error on unmarshall", func(t *testing.T) {
redisMockClient.ExpectGet("123456").SetVal("")
cacheMock.client = db
actualObject, err := cacheMock.Get(context.Background(), "123456", testObject{})
require.Error(t, err)
assert.Equal(t, testObject{}, actualObject)
})
}
func Test_Set(t *testing.T) {
type testObject struct {
Name string `json:"name"`
Surname string `json:"surname"`
}
var cacheMock cache
db, redisMockClient := redismock.NewClientMock()
t.Run("Set returns the nil", func(t *testing.T) {
var err error
value := testObject{
Name: "John",
Surname: "Doe"}
redisMockClient.ExpectSet("123456", value, 1).RedisNil()
cacheMock.client = db
err = cacheMock.Set(context.Background(), "123456", value)
require.Nil(t, err)
})
t.Run("Set returns the error on marshal", func(t *testing.T) {
var err error
value := make(chan string)
redisMockClient.ExpectSet("123456", value, 1).RedisNil()
cacheMock.client = db
err = cacheMock.Set(context.Background(), "123456", value)
require.Error(t, err)
})
}