forked from compose/transporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadaptor_test.go
113 lines (103 loc) · 3.49 KB
/
adaptor_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
package adaptor_test
import (
"reflect"
"testing"
"github.com/compose/transporter/adaptor"
_ "github.com/compose/transporter/log"
"github.com/compose/transporter/message"
"github.com/compose/transporter/message/ops"
)
func init() {
adaptor.Add("mock", func() adaptor.Adaptor { return &adaptor.Mock{} })
adaptor.Add("unsupported", func() adaptor.Adaptor { return &adaptor.UnsupportedMock{} })
adaptor.Add("clienterr", func() adaptor.Adaptor { return &adaptor.MockClientErr{} })
adaptor.Add("writererr", func() adaptor.Adaptor { return &adaptor.MockWriterErr{} })
}
var (
mockTests = []struct {
name string
conf adaptor.Config
adaptorErr error
clientErr error
readerErr error
writerErr error
}{
{"mock", map[string]interface{}{"uri": "uri"}, nil, nil, nil, nil},
{"clienterr", map[string]interface{}{"uri": "uri"}, nil, nil, nil, nil},
{"writererr", map[string]interface{}{"uri": "uri"}, nil, nil, nil, nil},
{"notfound", map[string]interface{}{}, adaptor.ErrNotFound{Name: "notfound"}, nil, nil, nil},
{
"unsupported",
map[string]interface{}{},
nil,
adaptor.ErrFuncNotSupported{Name: "unsupported", Func: "Client()"},
adaptor.ErrFuncNotSupported{Name: "unsupported", Func: "Reader()"},
adaptor.ErrFuncNotSupported{Name: "unsupported", Func: "Writer()"},
},
}
)
func TestMocks(t *testing.T) {
for _, mt := range mockTests {
m, err := adaptor.GetAdaptor(mt.name, mt.conf)
if !reflect.DeepEqual(err, mt.adaptorErr) {
t.Errorf("[%s] wrong GetAdaptor() error, expected %s, got %s", mt.name, mt.adaptorErr, err)
}
if err == nil {
if _, err := m.Client(); !reflect.DeepEqual(err, mt.clientErr) {
t.Errorf("[%s] wrong Client() error, expected %s, got %s", mt.name, mt.clientErr, err)
}
if _, err := m.Reader(); !reflect.DeepEqual(err, mt.readerErr) {
t.Errorf("[%s] wrong Reader() error, expected %s, got %s", mt.name, mt.readerErr, err)
}
if _, err := m.Writer(nil, nil); !reflect.DeepEqual(err, mt.writerErr) {
t.Errorf("[%s] wrong Writer() error, expected %s, got %s", mt.name, mt.writerErr, err)
}
}
}
}
func TestRegisteredAdaptors(t *testing.T) {
all := adaptor.RegisteredAdaptors()
if len(all) != 4 {
t.Errorf("wrong number of registered adaptors, expected 4, got %d", len(all))
}
}
func TestAdaptors(t *testing.T) {
all := adaptor.Adaptors()
if len(all) != 4 {
t.Errorf("wrong number of registered adaptors, expected 4, got %d", len(all))
}
}
var configTests = []struct {
cfg adaptor.Config
key string
expected string
}{
{adaptor.Config{"hello": "world"}, "hello", "world"},
{adaptor.Config{"hello": "world"}, "goodbye", ""},
{adaptor.Config{"key": 1}, "key", ""},
}
func TestConfig(t *testing.T) {
for _, ct := range configTests {
val := ct.cfg.GetString(ct.key)
if !reflect.DeepEqual(val, ct.expected) {
t.Errorf("wrong string returned for %s, expected %s, got %s", ct.key, ct.expected, val)
}
}
}
func TestMockConfirms(t *testing.T) {
confirms, cleanup := adaptor.MockConfirmWrites()
defer adaptor.VerifyWriteConfirmed(cleanup, t)
m, err := adaptor.GetAdaptor("mock", map[string]interface{}{"uri": "uri"})
if err != nil {
t.Fatalf("unexpected GetAdaptor error, %s", err)
}
w, err := m.Writer(nil, nil)
if err != nil {
t.Fatalf("unexpected Writer error, %s", err)
}
msg := message.From(ops.Insert, "test", map[string]interface{}{"id": 0, "test": "hello world"})
msg = message.WithConfirms(confirms, msg)
if _, err = w.Write(msg)(nil); err != nil {
t.Errorf("unexpected Writer error, %s", err)
}
}