91 lines
2.2 KiB
Go
91 lines
2.2 KiB
Go
|
package logfile_test
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"os"
|
||
|
"path/filepath"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/go-flucky/flucky/pkg/logfile"
|
||
|
"github.com/go-flucky/flucky/pkg/types"
|
||
|
uuid "github.com/satori/go.uuid"
|
||
|
"github.com/stretchr/testify/require"
|
||
|
)
|
||
|
|
||
|
var tmpDir string
|
||
|
|
||
|
func TestJSON(t *testing.T) {
|
||
|
require := require.New(t)
|
||
|
|
||
|
tmpDir = filepath.Join(os.TempDir(), uuid.NewV4().String())
|
||
|
err := os.MkdirAll(tmpDir, 0775)
|
||
|
require.Nil(err)
|
||
|
|
||
|
testCases := []*testCase{
|
||
|
&testCase{
|
||
|
name: "validateTemperatures",
|
||
|
f: testValidateTemperatures,
|
||
|
},
|
||
|
}
|
||
|
|
||
|
for _, testCase := range testCases {
|
||
|
err := t.Run(testCase.name, testCase.f)
|
||
|
require.True(err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// func testCompressTemperatures(t *testing.T) {
|
||
|
// require := require.New(t)
|
||
|
|
||
|
// assets := []string{
|
||
|
// "pkg/logfile/test/json/faultyTemperatures_01.json",
|
||
|
// "pkg/logfile/test/json/faultyTemperatures_02.json",
|
||
|
// "pkg/logfile/test/json/faultyTemperatures_03.json",
|
||
|
// "pkg/logfile/test/json/faultyTemperatures_04.json",
|
||
|
// "pkg/logfile/test/json/faultyTemperatures_05.json",
|
||
|
// }
|
||
|
|
||
|
// for i, asset := range assets {
|
||
|
// l := filepath.Join(tmpDir, fmt.Sprintf("%v.json", i))
|
||
|
// temperatures, err := saveAndReadTemperatureAsset(t, logfile.New(l), asset)
|
||
|
// require.Nil(err, "%v", err)
|
||
|
|
||
|
// }
|
||
|
|
||
|
// }
|
||
|
|
||
|
func testValidateTemperatures(t *testing.T) {
|
||
|
require := require.New(t)
|
||
|
|
||
|
assets := []string{
|
||
|
"pkg/logfile/test/json/faultyTemperatures_01.json",
|
||
|
"pkg/logfile/test/json/faultyTemperatures_02.json",
|
||
|
"pkg/logfile/test/json/faultyTemperatures_03.json",
|
||
|
"pkg/logfile/test/json/faultyTemperatures_04.json",
|
||
|
"pkg/logfile/test/json/faultyTemperatures_05.json",
|
||
|
}
|
||
|
|
||
|
for i, asset := range assets {
|
||
|
l := filepath.Join(tmpDir, fmt.Sprintf("%v.json", i))
|
||
|
temperatures, err := saveAndReadTemperatureAsset(t, logfile.New(l), asset)
|
||
|
require.Nil(err, "%v", err)
|
||
|
|
||
|
err = logfile.ValidateTemperatures(temperatures)
|
||
|
require.Errorf(err, "Asset %v", asset)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func saveAndReadTemperatureAsset(t *testing.T, logfile logfile.Logfile, a string) ([]*types.Temperature, error) {
|
||
|
require := require.New(t)
|
||
|
|
||
|
f, err := os.Create(logfile.GetLogfile())
|
||
|
require.Nil(err)
|
||
|
f.Write(MustAsset(a))
|
||
|
f.Close()
|
||
|
|
||
|
temperatures, err := logfile.ReadTemperatures()
|
||
|
require.Nil(err, "%v", err)
|
||
|
|
||
|
return temperatures, nil
|
||
|
}
|