gobot.io/x/gobot/v2@v2.1.0/platforms/mqtt/mqtt_driver_test.go (about)

     1  package mqtt
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"gobot.io/x/gobot/v2"
     8  	"gobot.io/x/gobot/v2/gobottest"
     9  )
    10  
    11  var _ gobot.Driver = (*Driver)(nil)
    12  
    13  func TestMqttDriver(t *testing.T) {
    14  	d := NewDriver(initTestMqttAdaptor(), "/test/topic")
    15  
    16  	gobottest.Assert(t, strings.HasPrefix(d.Name(), "MQTT"), true)
    17  	gobottest.Assert(t, strings.HasPrefix(d.Connection().Name(), "MQTT"), true)
    18  
    19  	gobottest.Assert(t, d.Start(), nil)
    20  	gobottest.Assert(t, d.Halt(), nil)
    21  }
    22  
    23  func TestMqttDriverName(t *testing.T) {
    24  	d := NewDriver(initTestMqttAdaptor(), "/test/topic")
    25  	gobottest.Assert(t, strings.HasPrefix(d.Name(), "MQTT"), true)
    26  	d.SetName("NewName")
    27  	gobottest.Assert(t, d.Name(), "NewName")
    28  }
    29  
    30  func TestMqttDriverTopic(t *testing.T) {
    31  	d := NewDriver(initTestMqttAdaptor(), "/test/topic")
    32  	gobottest.Assert(t, d.Topic(), "/test/topic")
    33  	d.SetTopic("/test/newtopic")
    34  	gobottest.Assert(t, d.Topic(), "/test/newtopic")
    35  }
    36  
    37  func TestMqttDriverPublish(t *testing.T) {
    38  	a := initTestMqttAdaptor()
    39  	d := NewDriver(a, "/test/topic")
    40  	a.Connect()
    41  	d.Start()
    42  	defer d.Halt()
    43  	gobottest.Assert(t, d.Publish([]byte{0x01, 0x02, 0x03}), true)
    44  }
    45  
    46  func TestMqttDriverPublishError(t *testing.T) {
    47  	a := initTestMqttAdaptor()
    48  	d := NewDriver(a, "/test/topic")
    49  	d.Start()
    50  	defer d.Halt()
    51  	gobottest.Assert(t, d.Publish([]byte{0x01, 0x02, 0x03}), false)
    52  }