-
Notifications
You must be signed in to change notification settings - Fork 4
/
transform_test.go
99 lines (80 loc) · 3.49 KB
/
transform_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
package frizzle_test
import (
"bytes"
"testing"
"time"
"github.com/qntfy/frizzle"
"github.com/qntfy/frizzle/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestSimpleSep(t *testing.T) {
var sep byte = '\n'
updatedMsg := frizzle.NewSimpleMsg("updatedMsg", append(testMsgData, sep), time.Now())
st := frizzle.NewSimpleSepTransformer([]byte{sep})
// Send should append the sep character, same as updatedMsg
sendResult := st.SendTransform()(testMsg).Data()
assert.Equal(t, string(updatedMsg.Data()), string(sendResult))
// Receive should remove the sep character so it matches original testMsg
rcvResult := st.ReceiveTransform()(updatedMsg).Data()
assert.Equal(t, string(testMsg.Data()), string(rcvResult))
// Send should not update if sep character is already present
sendResultNoUpdate := st.SendTransform()(updatedMsg).Data()
assert.Equal(t, string(updatedMsg.Data()), string(sendResultNoUpdate))
// Receive should not update if sep character is not present
rcvResultNoUpdate := st.ReceiveTransform()(testMsg).Data()
assert.Equal(t, string(testMsg.Data()), string(rcvResultNoUpdate))
}
func TestSimpleSepMultiChar(t *testing.T) {
sep := []byte("end of file{}#")
updatedMsg := frizzle.NewSimpleMsg("updatedMsg", append(testMsgData, sep...), time.Now())
st := frizzle.NewSimpleSepTransformer(sep)
// Send should append the sep slice, same as updatedMsg
sendResult := st.SendTransform()(testMsg).Data()
assert.Equal(t, string(updatedMsg.Data()), string(sendResult))
// Receive should remove the sep slice so it matches original testMsg
rcvResult := st.ReceiveTransform()(updatedMsg).Data()
assert.Equal(t, string(testMsg.Data()), string(rcvResult))
// Send should not update if sep character is already present
sendResultNoUpdate := st.SendTransform()(updatedMsg).Data()
assert.Equal(t, string(updatedMsg.Data()), string(sendResultNoUpdate))
// Receive should not update if sep character is not present
rcvResultNoUpdate := st.ReceiveTransform()(testMsg).Data()
assert.Equal(t, string(testMsg.Data()), string(rcvResultNoUpdate))
}
func msgDataMatcher(data []byte) func(frizzle.Msg) bool {
return func(m frizzle.Msg) bool {
return bytes.Equal(data, m.Data())
}
}
func TestSimpleSepSendWithFrizzle(t *testing.T) {
f, _, mSink := testFrizzle()
var sep byte = '\n'
updatedMsg := frizzle.NewSimpleMsg("updatedMsg", append(testMsgData, sep), time.Now())
st := frizzle.NewSimpleSepTransformer([]byte{sep})
// With the transformer added, the final Send should include the updated data
f.AddOptions(frizzle.WithTransformer(st))
mSink.On("Send", mock.MatchedBy(msgDataMatcher(updatedMsg.Data())), testSinkName).Return(nil)
err := f.Send(testMsg, testSinkName)
assert.Nil(t, err)
mSink.AssertExpectations(t)
}
func TestSimpleSepRcvWithFrizzle(t *testing.T) {
mSource := &mocks.Source{}
mSink := &mocks.Sink{}
testChan := make(chan frizzle.Msg, 1)
var testChanReceive <-chan frizzle.Msg
testChanReceive = testChan
mSource.On("Receive").Return(testChanReceive)
f := frizzle.Init(mSource, mSink)
// set up and attach the sep transformer
var sep byte = '\n'
updatedMsg := frizzle.NewSimpleMsg("updatedMsg", append(testMsgData, sep), time.Now())
st := frizzle.NewSimpleSepTransformer([]byte{sep})
f.AddOptions(frizzle.WithTransformer(st))
// pass the message with the sep; the received message should be transformed and not have the sep
testChan <- updatedMsg
receivedMsg := <-f.Receive()
assert.Equal(t, testMsgData, receivedMsg.Data())
mSource.AssertExpectations(t)
}