hybridgroup.gobot/platforms/mqtt/mqtt_adaptor_test.go

59 lines
1.3 KiB
Go

package mqtt
import (
"errors"
"fmt"
"testing"
multierror "github.com/hashicorp/go-multierror"
"gobot.io/x/gobot"
"gobot.io/x/gobot/gobottest"
)
var _ gobot.Adaptor = (*Adaptor)(nil)
func initTestMqttAdaptor() *Adaptor {
return NewAdaptor("localhost:1883", "client")
}
func TestMqttAdaptorConnect(t *testing.T) {
a := initTestMqttAdaptor()
var expected error
expected = multierror.Append(expected, errors.New("Network Error : Unknown protocol"))
gobottest.Assert(t, a.Connect(), expected)
}
func TestMqttAdaptorFinalize(t *testing.T) {
a := initTestMqttAdaptor()
gobottest.Assert(t, a.Finalize(), nil)
}
func TestMqttAdaptorCannotPublishUnlessConnected(t *testing.T) {
a := initTestMqttAdaptor()
data := []byte("o")
gobottest.Assert(t, a.Publish("test", data), false)
}
func TestMqttAdaptorPublishWhenConnected(t *testing.T) {
a := initTestMqttAdaptor()
a.Connect()
data := []byte("o")
gobottest.Assert(t, a.Publish("test", data), true)
}
func TestMqttAdaptorCannotOnUnlessConnected(t *testing.T) {
a := initTestMqttAdaptor()
gobottest.Assert(t, a.On("hola", func(data []byte) {
fmt.Println("hola")
}), false)
}
func TestMqttAdaptorOnWhenConnected(t *testing.T) {
a := initTestMqttAdaptor()
a.Connect()
gobottest.Assert(t, a.On("hola", func(data []byte) {
fmt.Println("hola")
}), true)
}