2017-01-26 03:19:15 +08:00
|
|
|
package mqtt
|
|
|
|
|
|
|
|
import (
|
2017-02-02 22:29:56 +08:00
|
|
|
"strings"
|
2017-01-26 03:19:15 +08:00
|
|
|
"testing"
|
|
|
|
|
2023-10-20 16:27:09 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-11-12 21:17:02 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-11-16 03:51:52 +08:00
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
2017-01-26 03:19:15 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ gobot.Driver = (*Driver)(nil)
|
|
|
|
|
|
|
|
func TestMqttDriver(t *testing.T) {
|
|
|
|
d := NewDriver(initTestMqttAdaptor(), "/test/topic")
|
|
|
|
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.True(t, strings.HasPrefix(d.Name(), "MQTT"))
|
|
|
|
assert.True(t, strings.HasPrefix(d.Connection().Name(), "MQTT"))
|
2017-01-26 03:19:15 +08:00
|
|
|
|
2023-11-12 21:17:02 +08:00
|
|
|
require.NoError(t, d.Start())
|
|
|
|
require.NoError(t, d.Halt())
|
2017-01-26 03:19:15 +08:00
|
|
|
}
|
2017-04-06 16:55:06 +08:00
|
|
|
|
|
|
|
func TestMqttDriverName(t *testing.T) {
|
|
|
|
d := NewDriver(initTestMqttAdaptor(), "/test/topic")
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.True(t, strings.HasPrefix(d.Name(), "MQTT"))
|
2017-04-06 16:55:06 +08:00
|
|
|
d.SetName("NewName")
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, "NewName", d.Name())
|
2017-04-06 16:55:06 +08:00
|
|
|
}
|
2017-04-19 16:59:33 +08:00
|
|
|
|
|
|
|
func TestMqttDriverTopic(t *testing.T) {
|
|
|
|
d := NewDriver(initTestMqttAdaptor(), "/test/topic")
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, "/test/topic", d.Topic())
|
2017-04-19 16:59:33 +08:00
|
|
|
d.SetTopic("/test/newtopic")
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, "/test/newtopic", d.Topic())
|
2017-04-19 16:59:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttDriverPublish(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
|
|
|
d := NewDriver(a, "/test/topic")
|
2023-06-13 01:51:25 +08:00
|
|
|
_ = a.Connect()
|
|
|
|
_ = d.Start()
|
|
|
|
defer func() { _ = d.Halt() }()
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.True(t, d.Publish([]byte{0x01, 0x02, 0x03}))
|
2017-04-19 16:59:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttDriverPublishError(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
|
|
|
d := NewDriver(a, "/test/topic")
|
2023-06-13 01:51:25 +08:00
|
|
|
_ = d.Start()
|
|
|
|
defer func() { _ = d.Halt() }()
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.False(t, d.Publish([]byte{0x01, 0x02, 0x03}))
|
2017-04-19 16:59:33 +08:00
|
|
|
}
|