PKGBUILD/pkg/logfile/logfile_test.go

134 lines
3.8 KiB
Go

package logfile_test
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
"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
}
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) {
require := require.New(t)
markupLanguages := []string{"csv", "json", "xml"}
for _, markupLanguageFrom := range markupLanguages {
testDir, cleanup := createTestDir(t)
defer cleanup()
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)
}