PKGBUILD/test/goldenfiles/goldenfiles.go

70 lines
1.8 KiB
Go
Raw Normal View History

2019-08-20 19:37:45 +00:00
package goldenfiles
import (
"bytes"
"encoding/json"
"strings"
"testing"
"github.com/go-flucky/flucky/pkg/types"
"github.com/stretchr/testify/require"
)
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) {
f := string(MustAsset(goldenFilePath))
r := strings.NewReader(f)
measuredValues := make([]*types.MeasuredValue, 0)
jsonDecoder := json.NewDecoder(r)
err := jsonDecoder.Decode(&measuredValues)
if err != nil {
return nil, err
}
return measuredValues, nil
}
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
}