-
Notifications
You must be signed in to change notification settings - Fork 4
/
consumer_test.go
160 lines (150 loc) · 3.9 KB
/
consumer_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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package gorabbit_test
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/KardinalAI/gorabbit"
)
func TestMQTTMessageHandlers_Validate(t *testing.T) {
tests := []struct {
handlers gorabbit.MQTTMessageHandlers
expectedError error
}{
{
handlers: gorabbit.MQTTMessageHandlers{
"event.user.#": func(_ []byte) error { return nil },
"event.email.*.generated": func(_ []byte) error { return nil },
"event.*.space.boom": func(_ []byte) error { return nil },
"*.toto.order.passed": func(_ []byte) error { return nil },
"#.toto": func(_ []byte) error { return nil },
},
expectedError: nil,
},
{
handlers: gorabbit.MQTTMessageHandlers{
"": func(_ []byte) error { return nil },
},
expectedError: errors.New("a routing key cannot be empty"),
},
{
handlers: gorabbit.MQTTMessageHandlers{
" ": func(_ []byte) error { return nil },
},
expectedError: errors.New("a routing key cannot contain spaces"),
},
{
handlers: gorabbit.MQTTMessageHandlers{
"#": func(_ []byte) error { return nil },
},
expectedError: errors.New("a routing key cannot be the wildcard '#'"),
},
{
handlers: gorabbit.MQTTMessageHandlers{
"toto.#.titi": func(_ []byte) error { return nil },
},
expectedError: errors.New("the wildcard '#' in the routing key 'toto.#.titi' is not allowed"),
},
{
handlers: gorabbit.MQTTMessageHandlers{
"toto titi": func(_ []byte) error { return nil },
},
expectedError: errors.New("a routing key cannot contain spaces"),
},
{
handlers: gorabbit.MQTTMessageHandlers{
"toto..titi": func(_ []byte) error { return nil },
},
expectedError: errors.New("the routing key 'toto..titi' is not properly formatted"),
},
{
handlers: gorabbit.MQTTMessageHandlers{
".toto.titi": func(_ []byte) error { return nil },
},
expectedError: errors.New("the routing key '.toto.titi' is not properly formatted"),
},
{
handlers: gorabbit.MQTTMessageHandlers{
"toto.titi.": func(_ []byte) error { return nil },
},
expectedError: errors.New("the routing key 'toto.titi.' is not properly formatted"),
},
}
for _, test := range tests {
err := test.handlers.Validate()
assert.Equal(t, test.expectedError, err)
}
}
func TestMQTTMessageHandlers_FindFunc(t *testing.T) {
handlers := gorabbit.MQTTMessageHandlers{
"event.user.#": func(_ []byte) error { return nil },
"event.email.*.generated": func(_ []byte) error { return nil },
"event.*.space.boom": func(_ []byte) error { return nil },
"*.toto.order.passed": func(_ []byte) error { return nil },
"#.toto": func(_ []byte) error { return nil },
}
tests := []struct {
input string
shouldMatch bool
}{
{
input: "event.user.plan.generated",
shouldMatch: true,
},
{
input: "event.user.password.generated.before.awakening.the.titan",
shouldMatch: true,
},
{
input: "event.email.subject.generated",
shouldMatch: true,
},
{
input: "event.email.toto.generated",
shouldMatch: true,
},
{
input: "event.email.titi.generated",
shouldMatch: true,
},
{
input: "event.email.order.created",
shouldMatch: false,
},
{
input: "event.toto.space.boom",
shouldMatch: true,
},
{
input: "event.toto.space.not_boom",
shouldMatch: false,
},
{
input: "command.toto.order.passed",
shouldMatch: true,
},
{
input: "command.toto.order.passed.please",
shouldMatch: false,
},
{
input: "event.toto",
shouldMatch: true,
},
{
input: "event.space.space.toto",
shouldMatch: true,
},
{
input: "event.toto.space",
shouldMatch: false,
},
}
for _, test := range tests {
fn := handlers.FindFunc(test.input)
if test.shouldMatch {
assert.NotNil(t, fn)
} else {
assert.Nil(t, fn)
}
}
}