-
Notifications
You must be signed in to change notification settings - Fork 0
/
kredis_test.go
79 lines (58 loc) · 1.71 KB
/
kredis_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 kredis
import (
"context"
"fmt"
"testing"
redistesthooks "github.com/mjc-gh/redis-test-hook"
"github.com/stretchr/testify/suite"
)
type KredisTestSuite struct {
suite.Suite
captureHook *redistesthooks.Hook
}
type testLogger struct{ stdLogger }
var testWarnings []string
func (tl testLogger) Warn(fnName string, err error) {
testWarnings = append(testWarnings, fmt.Sprintf("%s %s", fnName, err.Error()))
}
func (suite *KredisTestSuite) SetupTest() {
// TODO use a unique namespace for each test (thus potentially enabling
// parallel tests)
SetConfiguration("shared", "ns", "redis://localhost:6379/2")
SetConfiguration("capture", "ns", "redis://localhost:6379/2")
SetConfiguration("badconn", "", "redis://localhost:1234/0")
EnableDebugLogging()
suite.captureHook = redistesthooks.New()
client, _, _ := getConnection("capture")
client.AddHook(suite.captureHook)
testWarnings = []string{}
SetDebugLogger(&testLogger{})
}
func (suite *KredisTestSuite) TearDownTest() {
ctx := context.Background()
c, _, _ := getConnection("shared")
// Delete all keys in namespace to reset test state
keys, _ := c.Keys(ctx, "ns:*").Result()
for _, key := range keys {
c.Del(ctx, key)
}
suite.captureHook.Reset()
// Reset connections
delete(connections, "shared")
delete(connections, "badconn")
delete(connections, "capture")
}
// listen for 'go test' command --> run test methods
func TestKredisTestSuit(t *testing.T) {
suite.Run(t, new(KredisTestSuite))
}
func (s *KredisTestSuite) TestKredisJSON() {
kj := NewKredisJSON(`{"a":1}`)
s.Equal(`{"a":1}`, kj.String())
var data interface{}
err := kj.Unmarshal(&data)
s.NoError(err)
obj, ok := data.(map[string]interface{})
s.True(ok)
s.Equal(1.0, obj["a"])
}