src.dualinventive.com/go/influxdb-logger/internal/device/device_test.go

54 lines
1.3 KiB
Go

package device
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"src.dualinventive.com/go/dinet/rpc"
)
func TestFieldInfo(t *testing.T) {
testcases := []struct {
class rpc.Class
UID uint16
result *FieldInfo
err error
}{
{class: rpc.ClassSensor, UID: 1, result: &FieldInfo{Label: "sensor1", Type: "number"}, err: nil},
{class: rpc.ClassSensor, UID: 10, result: nil, err: ErrNotFound},
{class: rpc.ClassNotify, UID: 1, result: &FieldInfo{Label: "notify1", Type: "bool"}, err: nil},
{class: rpc.ClassNotify, UID: 10, result: nil, err: ErrNotFound},
{class: rpc.ClassLog, UID: 1, result: nil, err: ErrInvalidClass},
}
d := &Device{
Type: "tws-3000-wum",
SensorFields: map[uint16]*FieldInfo{
1: {Label: "sensor1", Type: "number"},
},
NotifyFields: map[uint16]*FieldInfo{
1: {Label: "notify1", Type: "bool"},
},
}
for _, tc := range testcases {
tc := tc
t.Run(fmt.Sprintf("%s-%d", tc.class, tc.UID), func(t *testing.T) {
fi, err := d.FieldInfo(tc.class, tc.UID)
if tc.result == nil {
assert.Nil(t, fi)
assert.Equal(t, tc.err, err)
} else {
require.NotNil(t, fi)
assert.Equal(t, *tc.result, *fi)
assert.Nil(t, err)
}
})
}
d = nil
fi, err := d.FieldInfo("", 0)
assert.Nil(t, fi)
assert.Equal(t, ErrNoDevice, err)
}