From 5e9668c2e4676be1a47ecaa5871f4c857d57c9fe Mon Sep 17 00:00:00 2001 From: sqshq Date: Sat, 3 Aug 2019 16:50:41 -0400 Subject: [PATCH 1/3] fixed the issue with decimal comma parsing, tests added --- component/util/parse.go | 1 + component/util/parse_test.go | 2 ++ metadata/storage_test.go | 65 ++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 metadata/storage_test.go diff --git a/component/util/parse.go b/component/util/parse.go index a3212d3..160dfb0 100644 --- a/component/util/parse.go +++ b/component/util/parse.go @@ -8,6 +8,7 @@ import ( func ParseFloat(input string) (float64, error) { clean := strings.TrimSpace(input) + clean = strings.Replace(clean, ",", ".", -1) // replace decimal comma with decimal point if strings.Contains(clean, "\n") { lastIndex := strings.LastIndex(clean, "\n") diff --git a/component/util/parse_test.go b/component/util/parse_test.go index c5f87da..0200d0e 100644 --- a/component/util/parse_test.go +++ b/component/util/parse_test.go @@ -13,6 +13,8 @@ func TestParseFloat(t *testing.T) { wantErr bool }{ {"should parse a regular number", args{"123"}, 123, false}, + {"should parse a float with decimal point", args{"123.456"}, 123.456, false}, + {"should parse a float with decimal comma", args{"123,456"}, 123.456, false}, {"should parse a regular number with spaces, tabs and line breaks", args{" \t 123 \t \n "}, 123, false}, {"should parse a last line in the given string", args{"123\n456"}, 456, false}, } diff --git a/metadata/storage_test.go b/metadata/storage_test.go new file mode 100644 index 0000000..b1cdeab --- /dev/null +++ b/metadata/storage_test.go @@ -0,0 +1,65 @@ +package metadata + +import ( + "bytes" + "gopkg.in/yaml.v3" + "io/ioutil" + "os" + "testing" +) + +type f struct { + a int +} + +func Test_fileExists(t *testing.T) { + + _, _ = os.Create(getPlatformStoragePath("exists")) + defer os.Remove(getPlatformStoragePath("exists")) + + type args struct { + filename string + } + tests := []struct { + name string + args args + want bool + }{ + {"should verify that file does not exist", args{"does-not-exist"}, false}, + {"should verify that file exists", args{"exists"}, true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := fileExists(tt.args.filename); got != tt.want { + t.Errorf("fileExists() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_saveStorageFile(t *testing.T) { + + file, _ := yaml.Marshal(f{a: 1}) + name := "test" + + saveStorageFile(file, name) + + read, _ := ioutil.ReadFile(getPlatformStoragePath(name)) + + if !bytes.Equal(file, read) { + t.Errorf("read file != saved file") + } +} + +func Test_readStorageFile(t *testing.T) { + + file, _ := yaml.Marshal(f{a: 1}) + name := "test" + + _ = ioutil.WriteFile(getPlatformStoragePath(name), file, os.ModePerm) + read := readStorageFile(getPlatformStoragePath(name)) + + if !bytes.Equal(file, read) { + t.Errorf("read file != saved file") + } +} From 5b1c6157407388297fc23d25fe513d293f6b84d2 Mon Sep 17 00:00:00 2001 From: sqshq Date: Sat, 3 Aug 2019 17:03:46 -0400 Subject: [PATCH 2/3] debug tests on CI --- metadata/storage_test.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/metadata/storage_test.go b/metadata/storage_test.go index b1cdeab..82e7985 100644 --- a/metadata/storage_test.go +++ b/metadata/storage_test.go @@ -14,7 +14,10 @@ type f struct { func Test_fileExists(t *testing.T) { - _, _ = os.Create(getPlatformStoragePath("exists")) + _, err := os.Create(getPlatformStoragePath("exists")) + if err != nil { + panic(err) + } defer os.Remove(getPlatformStoragePath("exists")) type args struct { @@ -56,7 +59,10 @@ func Test_readStorageFile(t *testing.T) { file, _ := yaml.Marshal(f{a: 1}) name := "test" - _ = ioutil.WriteFile(getPlatformStoragePath(name), file, os.ModePerm) + err := ioutil.WriteFile(getPlatformStoragePath(name), file, os.ModePerm) + if err != nil { + panic(err) + } read := readStorageFile(getPlatformStoragePath(name)) if !bytes.Equal(file, read) { From 04f50ea149ab5c442d247c6980a309804df3b420 Mon Sep 17 00:00:00 2001 From: sqshq Date: Sat, 3 Aug 2019 17:14:49 -0400 Subject: [PATCH 3/3] fix tests on CI --- metadata/storage_test.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/metadata/storage_test.go b/metadata/storage_test.go index 82e7985..cd3e696 100644 --- a/metadata/storage_test.go +++ b/metadata/storage_test.go @@ -14,10 +14,13 @@ type f struct { func Test_fileExists(t *testing.T) { + initStorage() + _, err := os.Create(getPlatformStoragePath("exists")) if err != nil { panic(err) } + defer os.Remove(getPlatformStoragePath("exists")) type args struct { @@ -42,6 +45,8 @@ func Test_fileExists(t *testing.T) { func Test_saveStorageFile(t *testing.T) { + initStorage() + file, _ := yaml.Marshal(f{a: 1}) name := "test" @@ -56,6 +61,8 @@ func Test_saveStorageFile(t *testing.T) { func Test_readStorageFile(t *testing.T) { + initStorage() + file, _ := yaml.Marshal(f{a: 1}) name := "test"