From 1e25b557896cac655c1667846b981c62a0a5c1e0 Mon Sep 17 00:00:00 2001 From: Markus Pesch Date: Sat, 22 Jun 2019 14:25:58 +0200 Subject: [PATCH] test(pkg/logfile/convert): add test for cross converting between logfiles --- pkg/logfile/csv.go | 14 +-- pkg/logfile/json_test.go | 90 --------------- pkg/logfile/logfile_test.go | 104 +++++++++++++++++- pkg/logfile/test/csv/validTemperatures_02.csv | 2 + pkg/logfile/test/xml/validTemperatures_01.xml | 20 ++++ pkg/logfile/test/xml/validTemperatures_02.xml | 19 ++++ pkg/logfile/xml.go | 3 - 7 files changed, 147 insertions(+), 105 deletions(-) delete mode 100644 pkg/logfile/json_test.go create mode 100644 pkg/logfile/test/csv/validTemperatures_02.csv create mode 100644 pkg/logfile/test/xml/validTemperatures_01.xml create mode 100644 pkg/logfile/test/xml/validTemperatures_02.xml diff --git a/pkg/logfile/csv.go b/pkg/logfile/csv.go index 463f974..9fa27b5 100644 --- a/pkg/logfile/csv.go +++ b/pkg/logfile/csv.go @@ -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) diff --git a/pkg/logfile/json_test.go b/pkg/logfile/json_test.go deleted file mode 100644 index cd4906c..0000000 --- a/pkg/logfile/json_test.go +++ /dev/null @@ -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 -} diff --git a/pkg/logfile/logfile_test.go b/pkg/logfile/logfile_test.go index 831f5b0..4a3ed9f 100644 --- a/pkg/logfile/logfile_test.go +++ b/pkg/logfile/logfile_test.go @@ -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) } diff --git a/pkg/logfile/test/csv/validTemperatures_02.csv b/pkg/logfile/test/csv/validTemperatures_02.csv new file mode 100644 index 0000000..e92fd04 --- /dev/null +++ b/pkg/logfile/test/csv/validTemperatures_02.csv @@ -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, diff --git a/pkg/logfile/test/xml/validTemperatures_01.xml b/pkg/logfile/test/xml/validTemperatures_01.xml new file mode 100644 index 0000000..3cfa0bb --- /dev/null +++ b/pkg/logfile/test/xml/validTemperatures_01.xml @@ -0,0 +1,20 @@ + + + a469503b-fc16-4e72-8d29-7eeee08ba957 + 24.562 + 2019-06-14T21:15:28.504051541+02:00 + 2019-06-14T21:18:07.384104493+02:00 + 84eac248-6927-4db6-b6f9-7891ce2d301e + 2019-06-14T21:18:07.465885864+02:00 + 2019-06-14T21:18:07.46587076+02:00 + + + 5f119ba3-bcea-4c3b-aabb-0406ea70f7e1 + 24.375 + 2019-06-14T21:15:28.583856443+02:00 + 2019-06-14T21:18:07.463893776+02:00 + efcd755e-82d1-4789-a50b-355b8735b8d8 + 2019-06-14T21:18:07.465885864+02:00 + 2019-06-14T21:18:07.46587701+02:00 + + \ No newline at end of file diff --git a/pkg/logfile/test/xml/validTemperatures_02.xml b/pkg/logfile/test/xml/validTemperatures_02.xml new file mode 100644 index 0000000..66cb6bb --- /dev/null +++ b/pkg/logfile/test/xml/validTemperatures_02.xml @@ -0,0 +1,19 @@ + + + a469503b-fc16-4e72-8d29-7eeee08ba957 + 24.562 + 2019-06-14T21:15:28.504051541+02:00 + 2019-06-14T21:18:07.384104493+02:00 + 84eac248-6927-4db6-b6f9-7891ce2d301e + 2019-06-14T21:18:07.465885864+02:00 + 2019-06-14T21:18:07.46587076+02:00 + + + 5f119ba3-bcea-4c3b-aabb-0406ea70f7e1 + 24.375 + 2019-06-14T21:15:28.583856443+02:00 + 2019-06-14T21:15:28.583856443+02:00 + efcd755e-82d1-4789-a50b-355b8735b8d8 + 2019-06-14T21:18:07.465885864+02:00 + + \ No newline at end of file diff --git a/pkg/logfile/xml.go b/pkg/logfile/xml.go index 139e76d..de77a66 100644 --- a/pkg/logfile/xml.go +++ b/pkg/logfile/xml.go @@ -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 {