mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 21:05:30 +00:00
add basic tests for mqtt.message.encode
This commit is contained in:
@@ -1,8 +1,10 @@
|
|||||||
package processor_test
|
package processor_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"slices"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||||
"github.com/jwetzell/showbridge-go/internal/config"
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||||
)
|
)
|
||||||
@@ -25,3 +27,63 @@ func TestMQTTMessageEncodeFromRegistry(t *testing.T) {
|
|||||||
t.Fatalf("mqtt.message.encode processor has wrong type: %s", processorInstance.Type())
|
t.Fatalf("mqtt.message.encode processor has wrong type: %s", processorInstance.Type())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGoodMQTTMessageEncode(t *testing.T) {
|
||||||
|
stringEncoder := processor.MQTTMessageEncode{}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
payload mqtt.Message
|
||||||
|
expected []byte
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "basic string",
|
||||||
|
payload: processor.NewMQTTMessage("test/topic", 1, true, []byte("hello")),
|
||||||
|
expected: []byte{0x68, 0x65, 0x6c, 0x6c, 0x6f},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
got, err := stringEncoder.Process(t.Context(), test.payload)
|
||||||
|
|
||||||
|
gotBytes, ok := got.([]byte)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("mqtt.message.encode returned a %T payload: %s", got, got)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("mqtt.message.encode failed: %s", err)
|
||||||
|
}
|
||||||
|
if !slices.Equal(gotBytes, test.expected) {
|
||||||
|
t.Fatalf("mqtt.message.encode got %s, expected %s", got, test.expected)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBadMQTTMessageEncode(t *testing.T) {
|
||||||
|
stringEncoder := processor.MQTTMessageEncode{}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
payload any
|
||||||
|
errorString string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "non-mqtt message input",
|
||||||
|
payload: []byte{0x68, 0x65, 0x6c, 0x6c, 0x6f},
|
||||||
|
errorString: "mqtt.message.encode processor only accepts an mqtt.Message",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
got, err := stringEncoder.Process(t.Context(), test.payload)
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("mqtt.message.encode expected to fail but got payload: %s", got)
|
||||||
|
}
|
||||||
|
if err.Error() != test.errorString {
|
||||||
|
t.Fatalf("mqtt.message.encode got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user