-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock_redis_test.go
58 lines (47 loc) · 1.31 KB
/
mock_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
package mockredis
import (
"context"
"fmt"
_ "strconv"
"strings"
"testing"
"time"
"github.com/alicebob/miniredis"
redis "github.com/go-redis/redis/v8"
)
func TestDoSomething(t *testing.T) {
mr, _ := miniredis.Run()
defer mr.Close()
c := redis.NewClient(&redis.Options{Addr: mr.Addr()})
err := DoSomething(context.Background(), c, map[string]interface{}{"key1": "value1"}, time.Hour)
// Validate error
if err != nil {
t.Errorf("No error was expected")
}
// Validate hash key value
if val := mr.HGet("myhash", "key1"); val != "value1" {
t.Errorf("Actual value %v does not match expected value", val)
}
// Validate expiry
if mr.FastForward(time.Hour + 1); mr.Exists("myhash") {
t.Errorf("Key should have expired")
}
}
func TestDoSomethingHSetFail(t *testing.T) {
mr, _ := miniredis.Run()
defer mr.Close()
c := redis.NewClient(&redis.Options{Addr: mr.Addr()})
c.AddHook(&RedisTestHook{
Target: "before",
C: c,
Match: `^(?i)expire `,
Action: func(c *redis.Client) error {
return fmt.Errorf("expire error")
},
})
err := DoSomething(context.Background(), c, map[string]interface{}{"key1": "value1"}, time.Hour)
// Validate error
if err == nil || !strings.Contains(err.Error(), "expire error") {
t.Errorf("Expected error `%v` does not match actual error `%v`", "expire error", err)
}
}