1
0
mirror of https://github.com/cmur2/miflorad.git synced 2025-06-26 12:30:23 +02:00

module: add tests for common package

This commit is contained in:
cn
2018-12-14 13:34:16 +01:00
parent e75c04785a
commit c96878c22c
7 changed files with 75 additions and 3 deletions

View File

@ -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
View 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)
}
}

View File

@ -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
View 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)
}
}