test(pkg/cli): Implementation of test to stabilize the code

This commit is contained in:
Markus Pesch 2020-01-11 13:03:58 +01:00
parent b595cf1ac8
commit 40857249c4
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
3 changed files with 123 additions and 5 deletions

View File

@ -1,23 +1,43 @@
package cli
import (
"errors"
"fmt"
"io"
"regexp"
"text/tabwriter"
"github.com/go-flucky/flucky/pkg/config"
"github.com/go-flucky/flucky/pkg/types"
)
// GetSensorsByMeasuredValues returns commulated list of sensors by measured values
func GetSensorIDsByMeasuredValues(measuredValues []*types.MeasuredValue, cnf *config.Configuration) map[string]*types.Sensor {
var (
errorMeasuredValueIDNotValid = errors.New("Measured value id is not a valid uuid")
errorSensorIDNotValid = errors.New("Sensor id is not a valid uuid")
validUUID = regexp.MustCompile("^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$")
)
// GetSensorIDsByMeasuredValues returns commulated list of sensors by measured values
func GetSensorIDsByMeasuredValues(measuredValues []*types.MeasuredValue, cnf *config.Configuration) (map[string]*types.Sensor, error) {
sensors := make(map[string]*types.Sensor)
for _, measuredValue := range measuredValues {
// check if sensor id is valid
if !validUUID.MatchString(measuredValue.ID) {
return nil, errorMeasuredValueIDNotValid
}
// check if sensor id is valid
if !validUUID.MatchString(measuredValue.SensorID) {
return nil, errorSensorIDNotValid
}
if _, ok := sensors[measuredValue.SensorID]; !ok {
sensors[measuredValue.SensorID] = cnf.GetSensorByID(measuredValue.SensorID)
}
}
return sensors
return sensors, nil
}
// PrintRGBLEDs displays a list with all configured RGBLEDs
@ -86,9 +106,12 @@ func PrintSensors(cnf *config.Configuration, w io.Writer) error {
}
// PrintMeasuredValues displays a list of measured values
func PrintMeasuredValues(measuredValues []*types.MeasuredValue, cnf *config.Configuration, w io.Writer) {
func PrintMeasuredValues(measuredValues []*types.MeasuredValue, cnf *config.Configuration, w io.Writer) error {
sensors := GetSensorIDsByMeasuredValues(measuredValues, cnf)
sensors, err := GetSensorIDsByMeasuredValues(measuredValues, cnf)
if err != nil {
return err
}
// sort measured values for every sensor
orderedMeasuredValues := make(map[string][]*types.MeasuredValue)
@ -130,4 +153,6 @@ func PrintMeasuredValues(measuredValues []*types.MeasuredValue, cnf *config.Conf
fmt.Fprint(tw, "\n")
}
tw.Flush()
return err
}

92
pkg/cli/cli_test.go Normal file
View File

@ -0,0 +1,92 @@
package cli
import (
"testing"
"github.com/go-flucky/flucky/pkg/config"
"github.com/go-flucky/flucky/pkg/types"
"github.com/stretchr/testify/require"
)
type testCase struct {
goldenConfig *config.Configuration
goldenMeasuredValues []*types.MeasuredValue
expectedSensors map[string]*types.Sensor
expectedError error
}
var (
goldenSensors = []*types.Sensor{
&types.Sensor{
ID: "5b7c08c8-ee2a-4f88-b796-83658caae09d",
},
}
goldenSensorMap = map[string]*types.Sensor{
"5b7c08c8-ee2a-4f88-b796-83658caae09d": goldenSensors[0],
}
goldenConfig = &config.Configuration{
Sensors: goldenSensors,
}
goldenMeasuredValues = []*types.MeasuredValue{
&types.MeasuredValue{
ID: "4887b7cc-6b40-4293-bcc1-db1b5035a711",
SensorID: "5b7c08c8-ee2a-4f88-b796-83658caae09d",
},
}
measuredValuesWrongID = []*types.MeasuredValue{
&types.MeasuredValue{
ID: "b2784dfb-c1a4-428d-8897-e09a18e7d94d",
SensorID: "5b7c08c8-ee2a-4f88-b796-83658caae09d",
},
&types.MeasuredValue{
ID: "81ca1a60-dfea-42f3-a7",
SensorID: "5b7c08c8-ee2a-4f88-b796-83658caae09d",
},
}
measuredValuesWrongSensorID = []*types.MeasuredValue{
&types.MeasuredValue{
ID: "b2784dfb-c1a4-428d-8897-e09a18e7d94d",
SensorID: "5b7c08c8-ee2a-4f88-b796-83658caae09d",
},
&types.MeasuredValue{
ID: "b2784dfb-c1a4-428d-8897-e09a18e7d94d",
SensorID: "5b7c08c8-ee2a-4f88-b79",
},
}
)
func TestGetSensorIDsByMeasuredValues(t *testing.T) {
require := require.New(t)
testCases := []*testCase{
&testCase{
goldenConfig: goldenConfig,
goldenMeasuredValues: goldenMeasuredValues,
expectedSensors: goldenSensorMap,
expectedError: nil,
},
&testCase{
goldenConfig: goldenConfig,
goldenMeasuredValues: measuredValuesWrongID,
expectedSensors: nil,
expectedError: errorMeasuredValueIDNotValid,
},
&testCase{
goldenConfig: goldenConfig,
goldenMeasuredValues: measuredValuesWrongSensorID,
expectedSensors: nil,
expectedError: errorSensorIDNotValid,
},
}
for _, testCase := range testCases {
actualSensors, err := GetSensorIDsByMeasuredValues(testCase.goldenMeasuredValues, testCase.goldenConfig)
require.EqualValues(testCase.expectedError, err, "Not the expected error")
require.EqualValues(testCase.expectedSensors, actualSensors, "Returned sensor list not expected")
}
}

View File

@ -84,6 +84,7 @@ func Start(cnf *config.Configuration, cleanCacheInterval time.Duration, compress
return
}
for _, measmeasuredValue := range measuredValues {
debugChannel <- fmt.Sprintf("%v: %v: %v", sensor.GetID(), measmeasuredValue.ValueType, measmeasuredValue.Value)
measuredValueChannel <- measmeasuredValue
}
}