mirror of
https://github.com/cmur2/miflorad.git
synced 2024-12-22 02:54:24 +01:00
module: add tests for common package
This commit is contained in:
parent
e75c04785a
commit
c96878c22c
1
Makefile
1
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:
|
||||
|
@ -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 {
|
||||
|
24
common/datatypes_test.go
Normal file
24
common/datatypes_test.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
@ -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:]),
|
||||
}
|
||||
}
|
||||
|
||||
|
45
common/proto_test.go
Normal file
45
common/proto_test.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
1
go.mod
1
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
|
||||
)
|
||||
|
1
go.sum
1
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=
|
||||
|
Loading…
Reference in New Issue
Block a user