-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathredis_test.go
90 lines (76 loc) · 1.85 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
package redis
import (
"fmt"
"os"
"testing"
vlog "github.com/svaloumas/valet/pkg/log"
)
var redisTest *RedisClient
func TestMain(m *testing.M) {
redisURL := os.Getenv("REDIS_URL")
redisTest = New(redisURL, 1, 5, "some-prefixed-key", nil)
defer redisTest.Close()
m.Run()
}
func TestCheckHealth(t *testing.T) {
result := redisTest.CheckHealth()
if result != true {
t.Fatalf("expected true got %#v instead", result)
}
}
func TestCheckHealthNotHealthy(t *testing.T) {
redisURL := os.Getenv("REDIS_URL")
notHealthyRedis := New(redisURL, 1, 5, "", nil)
notHealthyRedis.Close()
result := redisTest.CheckHealth()
if result != true {
t.Fatalf("expected false got %#v instead", result)
}
}
func TestPanicWithInvalidURL(t *testing.T) {
logger := vlog.NewLogger("redis", "text")
redisURL := "invalid_url"
defer func() {
if p := recover(); p == nil {
t.Errorf("New did not panic with invalid URL")
} else {
panicMsg := "redis: invalid URL scheme: "
if err := fmt.Errorf("%s", p); err.Error() != panicMsg {
t.Errorf("New paniced with unexpected panic message: got %v want %v", err.Error(), panicMsg)
}
}
}()
New(redisURL, 1, 5, "", logger)
}
func TestGetRedisPrefixedKey(t *testing.T) {
redisURL := os.Getenv("REDIS_URL")
rcWithoutPrefixedKey := New(redisURL, 1, 5, "", nil)
defer rcWithoutPrefixedKey.Close()
tests := []struct {
name string
key string
expected string
rc *RedisClient
}{
{
"with prefixed key",
"test",
"some-prefixed-key:test",
redisTest,
},
{
"no prefixed key",
"test",
"test",
rcWithoutPrefixedKey,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
redisKey := tt.rc.GetRedisPrefixedKey(tt.key)
if redisKey != tt.expected {
t.Errorf("get redis prefixed key returned wrong redis key: got %v want %v", redisKey, tt.expected)
}
})
}
}