2019-06-21 09:48:44 +00:00
|
|
|
package logfile_test
|
|
|
|
|
2019-06-22 12:25:58 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2019-06-21 09:48:44 +00:00
|
|
|
|
2019-06-22 12:25:58 +00:00
|
|
|
"github.com/go-flucky/flucky/pkg/logfile"
|
|
|
|
uuid "github.com/satori/go.uuid"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
var tmpDir = os.TempDir()
|
|
|
|
|
|
|
|
type interfaceMap struct {
|
|
|
|
testFile string
|
|
|
|
logfileA logfile.Logfile
|
|
|
|
goldenFile string
|
|
|
|
logfileB logfile.Logfile
|
|
|
|
}
|
|
|
|
|
2019-06-24 20:57:45 +00:00
|
|
|
func TestCompression(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
testDir, cleanup := createTestDir(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
testfiles := []*interfaceMap{
|
|
|
|
&interfaceMap{
|
|
|
|
testFile: "pkg/logfile/test/json/testCompression_01.json",
|
|
|
|
logfileA: logfile.New(filepath.Join(testDir, "testCompression_01.json")),
|
|
|
|
goldenFile: "pkg/logfile/test/json/goldenCompression_01.json",
|
|
|
|
logfileB: logfile.New(filepath.Join(testDir, "goldenCompression_01.json")),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
createTestFiles(t, testfiles)
|
|
|
|
|
|
|
|
temperatures, err := testfiles[0].logfileA.ReadTemperatures()
|
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
temperatures = logfile.CompressTemperature(temperatures)
|
|
|
|
|
|
|
|
diffLogfile := logfile.New(filepath.Join(testDir, "diffCompression_01.json"))
|
|
|
|
err = diffLogfile.WriteTemperatures(temperatures)
|
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
// verifyFileWithAsset(t, filepath.Join(testDir, "diffCompression_01.json"), "pkg/logfile/test/json/goldenCompression_01.json")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConvertTemperatures(t *testing.T) {
|
2019-06-22 12:25:58 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
markupLanguages := []string{"csv", "json", "xml"}
|
|
|
|
|
|
|
|
for _, markupLanguageFrom := range markupLanguages {
|
2019-06-23 12:19:47 +00:00
|
|
|
testDir, cleanup := createTestDir(t)
|
|
|
|
defer cleanup()
|
2019-06-22 12:25:58 +00:00
|
|
|
|
|
|
|
for _, markupLanguageTo := range markupLanguages {
|
|
|
|
|
|
|
|
convertMaps := []*interfaceMap{
|
|
|
|
&interfaceMap{
|
|
|
|
testFile: fmt.Sprintf("pkg/logfile/test/%v/validTemperatures_01.%v", markupLanguageFrom, markupLanguageFrom),
|
|
|
|
logfileA: logfile.New(filepath.Join(testDir, fmt.Sprintf("validTemperatures_01.%v", markupLanguageFrom))),
|
|
|
|
goldenFile: fmt.Sprintf("pkg/logfile/test/%v/validTemperatures_01.%v", markupLanguageTo, markupLanguageTo),
|
|
|
|
logfileB: logfile.New(filepath.Join(testDir, fmt.Sprintf("validTemperatures_01.%v", markupLanguageTo))),
|
|
|
|
},
|
|
|
|
&interfaceMap{
|
|
|
|
testFile: fmt.Sprintf("pkg/logfile/test/%v/validTemperatures_02.%v", markupLanguageFrom, markupLanguageFrom),
|
|
|
|
logfileA: logfile.New(filepath.Join(testDir, fmt.Sprintf("validTemperatures_02.%v", markupLanguageFrom))),
|
|
|
|
goldenFile: fmt.Sprintf("pkg/logfile/test/%v/validTemperatures_02.%v", markupLanguageTo, markupLanguageTo),
|
|
|
|
logfileB: logfile.New(filepath.Join(testDir, fmt.Sprintf("validTemperatures_02.%v", markupLanguageTo))),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
createTestFiles(t, convertMaps)
|
|
|
|
|
|
|
|
for _, convertMap := range convertMaps {
|
|
|
|
temperatures, err := convertMap.logfileA.ReadTemperatures()
|
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
err = convertMap.logfileB.WriteTemperatures(temperatures)
|
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
verifyFileWithAsset(t, convertMap.logfileB.GetLogfile(), convertMap.goldenFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createTestFiles(t *testing.T, interfaceMaps []*interfaceMap) {
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
for _, interfaceMap := range interfaceMaps {
|
|
|
|
f, err := os.Create(interfaceMap.logfileA.GetLogfile())
|
|
|
|
require.Nil(err)
|
|
|
|
f.Write(MustAsset(interfaceMap.testFile))
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
verifyFileWithAsset(t, interfaceMap.logfileA.GetLogfile(), interfaceMap.testFile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createTestDir(t *testing.T) (string, func() error) {
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
testDir := filepath.Join(tmpDir, uuid.NewV4().String())
|
|
|
|
|
|
|
|
err := os.MkdirAll(testDir, 0755)
|
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
rmDir := func() error {
|
|
|
|
return os.RemoveAll(testDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
return testDir, rmDir
|
|
|
|
}
|
|
|
|
|
|
|
|
func verifyFileWithAsset(t *testing.T, file string, asset string) {
|
|
|
|
require := require.New(t)
|
|
|
|
f, err := os.Open(file)
|
|
|
|
require.Nil(err)
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
bytes, err := ioutil.ReadAll(f)
|
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
require.Equalf(string(MustAsset(asset)), string(bytes), "Asset %v and file %v not even", asset, file)
|
2019-06-21 09:48:44 +00:00
|
|
|
}
|