From 40857249c48dd999eb31d4a4478794c504a39c50 Mon Sep 17 00:00:00 2001 From: Markus Pesch Date: Sat, 11 Jan 2020 13:03:58 +0100 Subject: [PATCH] test(pkg/cli): Implementation of test to stabilize the code --- pkg/cli/cli.go | 35 ++++++++++++++--- pkg/cli/cli_test.go | 92 ++++++++++++++++++++++++++++++++++++++++++++ pkg/daemon/daemon.go | 1 + 3 files changed, 123 insertions(+), 5 deletions(-) create mode 100644 pkg/cli/cli_test.go diff --git a/pkg/cli/cli.go b/pkg/cli/cli.go index 5b8bc78..f6bd434 100644 --- a/pkg/cli/cli.go +++ b/pkg/cli/cli.go @@ -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 } diff --git a/pkg/cli/cli_test.go b/pkg/cli/cli_test.go new file mode 100644 index 0000000..91b18d9 --- /dev/null +++ b/pkg/cli/cli_test.go @@ -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") + } +} diff --git a/pkg/daemon/daemon.go b/pkg/daemon/daemon.go index f5314c3..1f0ab54 100644 --- a/pkg/daemon/daemon.go +++ b/pkg/daemon/daemon.go @@ -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 } }