From 03073b44eaa801afc2faad7175669c2ee377838d Mon Sep 17 00:00:00 2001 From: Matthew Lemon Date: Fri, 25 Oct 2019 11:10:40 +0100 Subject: added test and some cleanup --- main.go | 6 +----- reader/reader.go | 9 +++------ reader/reader_test.go | 15 +++++++++++++++ 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/main.go b/main.go index 911e51d..525191f 100644 --- a/main.go +++ b/main.go @@ -1,10 +1,6 @@ package main -import ( - "datamaps-go/reader" -) - func main() { //reader.ReadDML("/home/lemon/Documents/datamaps/input/datamap.csv") - data, err := reader.ReadXLSX("/home/lemon/Documents/datamaps/input/A417%20Air%20Balloon_Q1%20Apr%20-%20June%202019_Return.xlsm") + //reader.ReadXLSX("/home/lemon/Documents/datamaps/input/A417%20Air%20Balloon_Q1%20Apr%20-%20June%202019_Return.xlsm") } diff --git a/reader/reader.go b/reader/reader.go index e4a6915..33e1773 100644 --- a/reader/reader.go +++ b/reader/reader.go @@ -2,6 +2,7 @@ package reader import ( "encoding/csv" + "errors" "fmt" "io" "io/ioutil" @@ -36,7 +37,7 @@ func ReadDML(path string) (*[]DatamapLine, error) { var s []DatamapLine data, err := ioutil.ReadFile(path) if err != nil { - return &s, &fileError{path, "Cannot open."} + return &s, errors.New("Cannot find file") } r := csv.NewReader(strings.NewReader(string(data))) for { @@ -45,7 +46,7 @@ func ReadDML(path string) (*[]DatamapLine, error) { break } if err != nil { - fmt.Printf("Cannot read line %s, ", err) + return &s, errors.New("Cannot read line %s") } if record[0] == "cell_key" { // this must be the header @@ -56,10 +57,6 @@ func ReadDML(path string) (*[]DatamapLine, error) { Sheet: strings.Trim(record[1], " "), Cellref: strings.Trim(record[2], " ")} s = append(s, dml) - // fmt.Printf("Key: %s; sheet: %s cellref: %s\n", dml.Key, dml.Sheet, dml.Cellref) - // klen, slen := Keylens(dml) - // fmt.Printf("Key length: %d\n", klen) - // fmt.Printf("Sheet length: %d\n\n", slen) } return &s, nil } diff --git a/reader/reader_test.go b/reader/reader_test.go index ba3172e..a7dd281 100644 --- a/reader/reader_test.go +++ b/reader/reader_test.go @@ -22,3 +22,18 @@ func TestReadDML(t *testing.T) { t.Errorf("dmlData[0].Sheet = %s; want Introduction", dmlData[0].Key) } } + +func TestNoFileReturnsError(t *testing.T) { + _, err := ReadDML("/home/bobbins.csv") + // if we get no error, something has gone wrong + if err == nil { + t.Errorf("Should have thrown error %s", err) + } +} + +func TestBadDMLLine(t *testing.T) { + _, err := ReadDML("/home/lemon/code/python/bcompiler-engine/tests/resources/datamap_empty_cols.csv") + if err != nil { + t.Errorf("This will trigger") + } +} -- cgit v1.2.3