test(pkg/logfile/convert): add test for cross converting between logfiles

This commit is contained in:
Markus Pesch 2019-06-22 14:25:58 +02:00
parent 6707dabdf1
commit 1e25b55789
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
7 changed files with 147 additions and 105 deletions

View File

@ -84,16 +84,14 @@ func (cl *csvLogfile) ReadTemperatures() ([]*types.Temperature, error) {
SensorID: record[4],
}
if len(record) == 6 && record[5] != "" {
temperatureCreationDate, err := time.Parse(timeFormat, record[5])
if err != nil {
return nil, fmt.Errorf("%v %v: %v", errorParseTime, record[5], err)
}
temperature.CreationDate = &temperatureCreationDate
// Creation date
temperatureCreationDate, err := time.Parse(timeFormat, record[5])
if err != nil {
return nil, fmt.Errorf("%v %v: %v", errorParseTime, record[5], err)
}
temperature.CreationDate = &temperatureCreationDate
if len(record) == 7 && record[6] != "" {
if record[6] != "" {
temperatureUpdateDate, err := time.Parse(timeFormat, record[6])
if err != nil {
return nil, fmt.Errorf("%v %v: %v", errorParseTime, record[6], err)

View File

@ -1,90 +0,0 @@
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
}

View File

@ -1,8 +1,104 @@
package logfile_test
import "testing"
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
type testCase struct {
name string
f func(t *testing.T)
"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 TestConvert(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)
}

View File

@ -0,0 +1,2 @@
a469503b-fc16-4e72-8d29-7eeee08ba957,24.562,2019-06-14 21:15:28.504051541 +0200,2019-06-14 21:18:07.384104493 +0200,84eac248-6927-4db6-b6f9-7891ce2d301e,2019-06-14 21:18:07.465885864 +0200,2019-06-14 21:18:07.46587076 +0200
5f119ba3-bcea-4c3b-aabb-0406ea70f7e1,24.375,2019-06-14 21:15:28.583856443 +0200,2019-06-14 21:15:28.583856443 +0200,efcd755e-82d1-4789-a50b-355b8735b8d8,2019-06-14 21:18:07.465885864 +0200,
1 a469503b-fc16-4e72-8d29-7eeee08ba957 24.562 2019-06-14 21:15:28.504051541 +0200 2019-06-14 21:18:07.384104493 +0200 84eac248-6927-4db6-b6f9-7891ce2d301e 2019-06-14 21:18:07.465885864 +0200 2019-06-14 21:18:07.46587076 +0200
2 5f119ba3-bcea-4c3b-aabb-0406ea70f7e1 24.375 2019-06-14 21:15:28.583856443 +0200 2019-06-14 21:15:28.583856443 +0200 efcd755e-82d1-4789-a50b-355b8735b8d8 2019-06-14 21:18:07.465885864 +0200

View File

@ -0,0 +1,20 @@
<temperatures>
<temperature>
<temperature_id>a469503b-fc16-4e72-8d29-7eeee08ba957</temperature_id>
<temperature_value>24.562</temperature_value>
<temperature_from_date>2019-06-14T21:15:28.504051541+02:00</temperature_from_date>
<temperature_till_date>2019-06-14T21:18:07.384104493+02:00</temperature_till_date>
<sensor_id>84eac248-6927-4db6-b6f9-7891ce2d301e</sensor_id>
<creation_date>2019-06-14T21:18:07.465885864+02:00</creation_date>
<update_date>2019-06-14T21:18:07.46587076+02:00</update_date>
</temperature>
<temperature>
<temperature_id>5f119ba3-bcea-4c3b-aabb-0406ea70f7e1</temperature_id>
<temperature_value>24.375</temperature_value>
<temperature_from_date>2019-06-14T21:15:28.583856443+02:00</temperature_from_date>
<temperature_till_date>2019-06-14T21:18:07.463893776+02:00</temperature_till_date>
<sensor_id>efcd755e-82d1-4789-a50b-355b8735b8d8</sensor_id>
<creation_date>2019-06-14T21:18:07.465885864+02:00</creation_date>
<update_date>2019-06-14T21:18:07.46587701+02:00</update_date>
</temperature>
</temperatures>

View File

@ -0,0 +1,19 @@
<temperatures>
<temperature>
<temperature_id>a469503b-fc16-4e72-8d29-7eeee08ba957</temperature_id>
<temperature_value>24.562</temperature_value>
<temperature_from_date>2019-06-14T21:15:28.504051541+02:00</temperature_from_date>
<temperature_till_date>2019-06-14T21:18:07.384104493+02:00</temperature_till_date>
<sensor_id>84eac248-6927-4db6-b6f9-7891ce2d301e</sensor_id>
<creation_date>2019-06-14T21:18:07.465885864+02:00</creation_date>
<update_date>2019-06-14T21:18:07.46587076+02:00</update_date>
</temperature>
<temperature>
<temperature_id>5f119ba3-bcea-4c3b-aabb-0406ea70f7e1</temperature_id>
<temperature_value>24.375</temperature_value>
<temperature_from_date>2019-06-14T21:15:28.583856443+02:00</temperature_from_date>
<temperature_till_date>2019-06-14T21:15:28.583856443+02:00</temperature_till_date>
<sensor_id>efcd755e-82d1-4789-a50b-355b8735b8d8</sensor_id>
<creation_date>2019-06-14T21:18:07.465885864+02:00</creation_date>
</temperature>
</temperatures>

View File

@ -4,7 +4,6 @@ import (
"encoding/xml"
"fmt"
"io/ioutil"
"log"
"os"
"github.com/go-flucky/flucky/pkg/types"
@ -63,8 +62,6 @@ func (xl *xmlLogfile) ReadTemperatures() ([]*types.Temperature, error) {
return nil, fmt.Errorf("%v: %v", errorLogfileUnmarshal, err)
}
log.Println(len(tmpTemperatures.Temperatures))
temperatures := make([]*types.Temperature, 0)
for _, tmpTemperature := range tmpTemperatures.Temperatures {