-
Notifications
You must be signed in to change notification settings - Fork 34
/
packet_test.go
127 lines (112 loc) · 3.69 KB
/
packet_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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package roger
import (
"errors"
"testing"
"github.com/senseyeio/roger/constants"
)
func TestErrorPacketIsError(t *testing.T) {
pkt := newErrorPacket(errors.New("test error"))
if pkt.IsError() == false {
t.Error("Test packet should return true when IsError is called")
}
}
func TestErrorPacketGetErrorNonNil(t *testing.T) {
pkt := newErrorPacket(errors.New("test error"))
err := pkt.GetError()
if err == nil {
t.Error("GetError should return a non nil error when the packet has an error")
}
}
func TestErrorPacketGetErrorNil(t *testing.T) {
pkt := newErrorPacket(nil)
err := pkt.GetError()
if err != nil {
t.Error("GetError should return nil when the packet has no error")
}
}
func TestErrorPacketResultObject(t *testing.T) {
testError := errors.New("test error")
pkt := newErrorPacket(testError)
obj, err := pkt.GetResultObject()
if err != testError {
t.Error("An error packet should return the error when GetResultObject is called")
}
if obj != nil {
t.Error("An error packet should return a nil object")
}
}
func TestCommandFailurePacketIsError(t *testing.T) {
failedCmdPkt := newPacket(0x01000002, []byte{})
if failedCmdPkt.IsError() == false {
t.Error("A command with an error flag should return true when IsError is called")
}
}
func TestCommandFailurePacketIsOk(t *testing.T) {
failedCmdPkt := newPacket(0x01000002, []byte{})
if failedCmdPkt.IsOk() == true {
t.Error("A command with an error flag should return false when IsOk is called")
}
}
func TestCommandFailurePacketResultObject(t *testing.T) {
failedCmdPkt := newPacket(0x01000002, []byte{})
obj, err := failedCmdPkt.GetResultObject()
if err.Error() != "Command error with status code: 1" {
t.Error("A failed command packet's error message should contain the status code element of the command response")
}
if obj != nil {
t.Error("A failed command packet should return a nil object")
}
}
func TestCommandFailurePacketResultStatus(t *testing.T) {
failedCmdPkt := newPacket(0x02000002, []byte{})
obj, err := failedCmdPkt.GetResultObject()
if err.Error() != "Command error with status: Invalid expression" {
t.Error("A failed command packet's error message should contain the status message of the command response")
}
if obj != nil {
t.Error("A failed command packet should return a nil object")
}
}
func TestCommandSuccessPacketIsError(t *testing.T) {
successfulCmdPkt := newPacket(0x01000003, []byte{})
if successfulCmdPkt.IsError() == true {
t.Error("A command without an error flag should return false when IsError is called")
}
}
func TestCommandSuccessPacketIsOk(t *testing.T) {
successfulCmdPkt := newPacket(0x01000003, []byte{})
if successfulCmdPkt.IsOk() == false {
t.Error("A command without an error flag should return true when IsOk is called")
}
}
func TestEmptyResponsePacketResultObject(t *testing.T) {
emptyPkt := newPacket(0x01000003, []byte{})
obj, err := emptyPkt.GetResultObject()
if err == nil {
t.Error("An empty packet should return an error")
}
if obj != nil {
t.Error("An empty packet should return a nil object")
}
}
func TestSuccessfulResponseResultObject(t *testing.T) {
client, _ := NewRClient("localhost", 6311)
pkt := client.EvaluateSync("2")
obj, err := pkt.GetResultObject()
if err != nil {
t.Error("A successful query should not result in an error")
}
if obj == nil {
t.Error("A successful query should return a response object")
}
}
func TestNonSEXPResponse(t *testing.T) {
stringResp := newPacket(0x01000003, []byte{byte(constants.DtString)})
obj, err := stringResp.GetResultObject()
if err == nil {
t.Error("Packets containing non SEXP content should return an error")
}
if obj != nil {
t.Error("Packets containing non SEXP content should not return an object")
}
}