From c96878c22c36678291cbb4407f122e99edc21fc9 Mon Sep 17 00:00:00 2001 From: cn Date: Fri, 14 Dec 2018 13:34:16 +0100 Subject: [PATCH] module: add tests for common package --- Makefile | 1 + common/datatypes.go | 2 +- common/datatypes_test.go | 24 +++++++++++++++++++++ common/proto.go | 4 ++-- common/proto_test.go | 45 ++++++++++++++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 1 + 7 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 common/datatypes_test.go create mode 100644 common/proto_test.go diff --git a/Makefile b/Makefile index a4fc3ff..b695eec 100644 --- a/Makefile +++ b/Makefile @@ -19,6 +19,7 @@ build: cmd/munin-miflora/munin-miflora cmd/munin-miflora/munin-miflora-gatt .PHONY: test test: build + cd common && go test -v -race && cd .. .PHONY: cmd/munin-miflora/munin-miflora cmd/munin-miflora/munin-miflora: diff --git a/common/datatypes.go b/common/datatypes.go index 69f36b2..737b1d8 100644 --- a/common/datatypes.go +++ b/common/datatypes.go @@ -7,8 +7,8 @@ import ( ) type VersionBatteryResponse struct { - FirmwareVersion string // as "x.y.z" BatteryLevel uint8 // in percent 0-100 + FirmwareVersion string // as "x.y.z" } type SensorDataResponse struct { diff --git a/common/datatypes_test.go b/common/datatypes_test.go new file mode 100644 index 0000000..895a772 --- /dev/null +++ b/common/datatypes_test.go @@ -0,0 +1,24 @@ +package common + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestNumericFirmwareVersion(t *testing.T) { + tables := []struct { + metaData VersionBatteryResponse + firmwire int + }{ + {VersionBatteryResponse{BatteryLevel: 99, FirmwareVersion: "1.0.0"}, 10000}, + {VersionBatteryResponse{BatteryLevel: 88, FirmwareVersion: "2.6.6"}, 20606}, + {VersionBatteryResponse{BatteryLevel: 77, FirmwareVersion: "0.1.0"}, 100}, + {VersionBatteryResponse{BatteryLevel: 66, FirmwareVersion: "1.x.5"}, 10005}, + {VersionBatteryResponse{BatteryLevel: 55, FirmwareVersion: "fubar"}, 0}, + } + + for _, table := range tables { + assert.Equal(t, table.metaData.NumericFirmwareVersion(), table.firmwire) + } +} diff --git a/common/proto.go b/common/proto.go index 937570c..a7cf054 100644 --- a/common/proto.go +++ b/common/proto.go @@ -17,8 +17,8 @@ func MifloraGetModeChangeData() []byte { func ParseVersionBattery(bytes []byte) VersionBatteryResponse { return VersionBatteryResponse{ - string(bytes[2:]), - uint8(bytes[0]), + BatteryLevel: uint8(bytes[0]), + FirmwareVersion: string(bytes[2:]), } } diff --git a/common/proto_test.go b/common/proto_test.go new file mode 100644 index 0000000..c460500 --- /dev/null +++ b/common/proto_test.go @@ -0,0 +1,45 @@ +package common + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestParseVersionBattery(t *testing.T) { + tables := []struct { + bytes []byte + metaData VersionBatteryResponse + }{ + {[]byte{0x64, 0x15, 0x32, 0x2e, 0x37, 0x2e, 0x30}, VersionBatteryResponse{BatteryLevel: 100, FirmwareVersion: "2.7.0"}}, + {[]byte{0x64, 0xee, 0x32, 0x2e, 0x37, 0x2e, 0x30}, VersionBatteryResponse{BatteryLevel: 100, FirmwareVersion: "2.7.0"}}, + {[]byte{0x64, 0x42, 0x32, 0x2e, 0x37, 0x2e, 0x30}, VersionBatteryResponse{BatteryLevel: 100, FirmwareVersion: "2.7.0"}}, + {[]byte{0x50, 0x15, 0x32, 0x2e, 0x37, 0x2e, 0x30}, VersionBatteryResponse{BatteryLevel: 80, FirmwareVersion: "2.7.0"}}, + {[]byte{0x64, 0x42, 0x32, 0x2e, 0x36, 0x2e, 0x36}, VersionBatteryResponse{BatteryLevel: 100, FirmwareVersion: "2.6.6"}}, + } + + for _, table := range tables { + assert.Equal(t, ParseVersionBattery(table.bytes), table.metaData) + } +} + +func TestParseSensorData(t *testing.T) { + tables := []struct { + bytes []byte + sensorData SensorDataResponse + }{ + // Source: https://www.open-homeautomation.com/de/2016/08/23/reverse-engineering-the-mi-plant-sensor/ + { + []byte{0xf2, 0x00, 0x00, 0x79, 0x00, 0x00, 0x00, 0x10, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, + SensorDataResponse{Temperature: 24.2, Brightness: 121, Moisture: 16, Conductivity: 101}, + }, + { + []byte{0x25, 0x01, 0x00, 0xf7, 0x26, 0x00, 0x00, 0x28, 0x0e, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, + SensorDataResponse{Temperature: 29.3, Brightness: 9975, Moisture: 40, Conductivity: 270}, + }, + } + + for _, table := range tables { + assert.Equal(t, ParseSensorData(table.bytes), table.sensorData) + } +} diff --git a/go.mod b/go.mod index 3ab5ae5..d7eb902 100644 --- a/go.mod +++ b/go.mod @@ -14,4 +14,5 @@ require ( github.com/muka/go-bluetooth v0.0.0-20181012115104-31d8f53bf9a1 github.com/pkg/errors v0.8.0 // indirect github.com/sirupsen/logrus v1.2.0 // indirect + github.com/stretchr/testify v1.2.2 ) diff --git a/go.sum b/go.sum index dabb90b..5d8a681 100644 --- a/go.sum +++ b/go.sum @@ -30,6 +30,7 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/sirupsen/logrus v1.2.0 h1:juTguoYk5qI21pwyTXY3B3Y5cOTH3ZUyZCg1v/mihuo= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= +github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=