2019-08-20 19:37:45 +00:00
|
|
|
package goldenfiles
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2019-10-11 10:36:00 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2019-08-20 19:37:45 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-01-19 12:30:47 +00:00
|
|
|
"github.com/volker-raschek/flucky/pkg/storage/logfile"
|
|
|
|
"github.com/volker-raschek/flucky/pkg/types"
|
2019-08-20 19:37:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func CompareMeasuredValues(t *testing.T, goldenInterface interface{}, testInterface interface{}) {
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
goldenBuffer := new(bytes.Buffer)
|
|
|
|
testBuffer := new(bytes.Buffer)
|
|
|
|
|
|
|
|
jsonEncoder := json.NewEncoder(goldenBuffer)
|
|
|
|
jsonEncoder.SetIndent("", " ")
|
|
|
|
err := jsonEncoder.Encode(&goldenInterface)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
jsonEncoder = json.NewEncoder(testBuffer)
|
|
|
|
jsonEncoder.SetIndent("", " ")
|
|
|
|
err = jsonEncoder.Encode(&testInterface)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
// t.Log(goldenBuffer.String(), testBuffer.String())
|
|
|
|
|
|
|
|
require.Equal(goldenBuffer.String(), testBuffer.String(), "Expected are not equal with actual value")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetGoldenDevices(goldenFilePath string) ([]*types.Device, error) {
|
|
|
|
file := string(MustAsset(goldenFilePath))
|
|
|
|
reader := strings.NewReader(file)
|
|
|
|
devices := make([]*types.Device, 0)
|
|
|
|
jsonDecoder := json.NewDecoder(reader)
|
|
|
|
err := jsonDecoder.Decode(&devices)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return devices, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetGoldenMeasuredValues(goldenFilePath string) ([]*types.MeasuredValue, error) {
|
2019-10-11 10:36:00 +00:00
|
|
|
tempDir := os.TempDir()
|
|
|
|
if err := os.MkdirAll(tempDir, 0755); err != nil {
|
|
|
|
return nil, fmt.Errorf("Can not create temp directory %v: %v", tempDir, err)
|
|
|
|
}
|
|
|
|
tempLogfile := filepath.Join(tempDir, fmt.Sprintf("logfile %v", filepath.Ext(goldenFilePath)))
|
|
|
|
f, err := os.Create(tempLogfile)
|
2019-08-20 19:37:45 +00:00
|
|
|
if err != nil {
|
2019-10-11 10:36:00 +00:00
|
|
|
return nil, fmt.Errorf("Can not create temporary logfile %v: %v", tempLogfile, err)
|
|
|
|
}
|
|
|
|
defer os.Remove(tempLogfile)
|
|
|
|
_, err = f.Write(MustAsset(goldenFilePath))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Can not write into file %v: %v", tempLogfile, err)
|
2019-08-20 19:37:45 +00:00
|
|
|
}
|
2019-10-11 10:36:00 +00:00
|
|
|
logfile := logfile.New(tempLogfile)
|
|
|
|
return logfile.Read()
|
2019-08-20 19:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetGoldenSensors(goldenFilePath string) ([]*types.Sensor, error) {
|
|
|
|
f := string(MustAsset(goldenFilePath))
|
|
|
|
r := strings.NewReader(f)
|
|
|
|
sensors := make([]*types.Sensor, 0)
|
|
|
|
jsonDecoder := json.NewDecoder(r)
|
|
|
|
err := jsonDecoder.Decode(&sensors)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return sensors, nil
|
|
|
|
}
|