-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathmqtt_driver_test.go
54 lines (43 loc) · 1.3 KB
/
mqtt_driver_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
package mqtt
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gobot.io/x/gobot/v2"
)
var _ gobot.Driver = (*Driver)(nil)
func TestMqttDriver(t *testing.T) {
d := NewDriver(initTestMqttAdaptor(), "/test/topic")
assert.True(t, strings.HasPrefix(d.Name(), "MQTT"))
assert.True(t, strings.HasPrefix(d.Connection().Name(), "MQTT"))
require.NoError(t, d.Start())
require.NoError(t, d.Halt())
}
func TestMqttDriverName(t *testing.T) {
d := NewDriver(initTestMqttAdaptor(), "/test/topic")
assert.True(t, strings.HasPrefix(d.Name(), "MQTT"))
d.SetName("NewName")
assert.Equal(t, "NewName", d.Name())
}
func TestMqttDriverTopic(t *testing.T) {
d := NewDriver(initTestMqttAdaptor(), "/test/topic")
assert.Equal(t, "/test/topic", d.Topic())
d.SetTopic("/test/newtopic")
assert.Equal(t, "/test/newtopic", d.Topic())
}
func TestMqttDriverPublish(t *testing.T) {
a := initTestMqttAdaptor()
d := NewDriver(a, "/test/topic")
_ = a.Connect()
_ = d.Start()
defer func() { _ = d.Halt() }()
assert.True(t, d.Publish([]byte{0x01, 0x02, 0x03}))
}
func TestMqttDriverPublishError(t *testing.T) {
a := initTestMqttAdaptor()
d := NewDriver(a, "/test/topic")
_ = d.Start()
defer func() { _ = d.Halt() }()
assert.False(t, d.Publish([]byte{0x01, 0x02, 0x03}))
}