-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_test.go
158 lines (140 loc) · 3.19 KB
/
redis_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
151
152
153
154
155
156
157
158
package kvsync_test
import (
"errors"
"github.com/alicebob/miniredis/v2"
"github.com/ndthuan/kvsync"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/bson"
"testing"
)
type erroneousMarshaler struct{}
func (e erroneousMarshaler) Marshal(v any) ([]byte, error) {
return nil, errors.New("marshaling error")
}
func (e erroneousMarshaler) Unmarshal(data []byte, v any) error {
return errors.New("unmarshaling error")
}
type User struct {
ID int
Name string
}
func TestRedisStore_Set(t *testing.T) {
redisStore, miniRedis := setUpStore()
defer miniRedis.Close()
defer func() {
redisStore.Marshaler = nil
}()
testCases := []struct {
name string
marshaler kvsync.MarshalingAdapter
key string
value any
wantErr bool
}{
{
name: "set a struct",
key: "user:1",
value: &User{ID: 1, Name: "Alice"},
wantErr: false,
},
{
name: "set a struct but marshaler returns error",
marshaler: &erroneousMarshaler{},
key: "user:1",
value: &User{ID: 1, Name: "Alice"},
wantErr: true,
},
{
name: "set a non-struct",
key: "user:2",
value: "Alice",
wantErr: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
redisStore.Marshaler = tc.marshaler
err := redisStore.Put(tc.key, tc.value)
if tc.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}
func TestRedisStore_FetchInto(t *testing.T) {
redisStore, miniRedis := setUpStore()
defer miniRedis.Close()
defer func() {
redisStore.Marshaler = nil
}()
var validDest User
validMarshal, err := bson.Marshal(&User{ID: 1, Name: "Alice"})
assert.NoError(t, err)
_ = miniRedis.Set("kvsync:user:1", string(validMarshal))
_ = miniRedis.Set("kvsync:unmarshallable", "unmarshalable")
testCases := []struct {
name string
key string
dest any
wantUsername string
wantErr bool
}{
{
name: "valid dest and marshalable",
key: "user:1",
dest: &validDest,
wantUsername: "Alice",
wantErr: false,
},
{
name: "valid dest but unmarshalable",
key: "unmarshalable",
dest: &validDest,
wantUsername: "",
wantErr: true,
},
{
name: "invalid dest (non-struct)",
key: "user:1",
dest: "Alice",
wantErr: true,
},
{
name: "valid dest but key not found",
key: "user:999",
dest: &validDest,
wantUsername: "",
wantErr: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err = redisStore.Fetch(tc.key, tc.dest)
if tc.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tc.wantUsername, tc.dest.(*User).Name)
}
})
}
}
func setUpStore() (*kvsync.RedisStore, *miniredis.Miniredis) {
// Create a new miniredis server
s, err := miniredis.Run()
if err != nil {
panic(err)
}
clusterClient := redis.NewClusterClient(&redis.ClusterOptions{
Addrs: []string{s.Addr()},
})
// Create a new RedisStore
store := &kvsync.RedisStore{
Client: clusterClient,
Expiration: 0,
}
return store, s
}