diff options
author | Matthew Lemon <lemon@matthewlemon.com> | 2019-11-28 11:12:46 +0000 |
---|---|---|
committer | Matthew Lemon <lemon@matthewlemon.com> | 2019-11-28 11:12:46 +0000 |
commit | 1c97dc2be19988e9e58061d2d21f45a62b0e6ad6 (patch) | |
tree | 9fa38bb8d0ff9a705c1de8c509abebe12b504d38 | |
parent | 05f6a2c270b227d3d41294b4f43e16dc89c51c85 (diff) | |
parent | 96d8cdf6fbe892a3e97040d0bc282b2dab80d081 (diff) |
Merge branch 'master' of github.com:hammerheadlemon/datamaps-go
-rw-r--r-- | reader/reader.go | 69 | ||||
-rw-r--r-- | reader/reader_test.go | 108 | ||||
-rw-r--r-- | reader/testdata/datamap.csv | 2 | ||||
-rw-r--r-- | reader/testdata/test_template.xlsx | bin | 9039 -> 9250 bytes |
4 files changed, 97 insertions, 82 deletions
diff --git a/reader/reader.go b/reader/reader.go index 71870c5..449b8ba 100644 --- a/reader/reader.go +++ b/reader/reader.go @@ -37,10 +37,10 @@ type DatamapLine struct { //ExtractedCell is data pulled from a cell. type ExtractedCell struct { - Cell *xlsx.Cell - ColL string - RowLidx int - Value string + Cell *xlsx.Cell + Col string + Row int + Value string } //sheetInSlice is a helper which returns true @@ -122,68 +122,55 @@ func cols(n int) []string { //ReadXLSX returns the file's data as a map, // keyed on sheet name. All values are returned as strings. // Paths to a datamap and the spreadsheet file required. -func ReadXLSX(dm string, ssheet string) FileData { - - // TODO - to implement filtering by Datamap, - // pull the data first, then go through each - // item in the dmlData slice to check for sheet/cellref - // matches, and then return them. Duplicate this func - // to do so +func ReadXLSX(ssheet string) FileData { // open the files - excelData, err := xlsx.OpenFile(ssheet) + data, err := xlsx.OpenFile(ssheet) if err != nil { log.Fatal(err) } - dmlData, err := ReadDML(dm) - if err != nil { - log.Fatal(err) - } - sheetNames := getSheetNames(dmlData) - output := make(FileData, len(sheetNames)) + outer := make(FileData, 1) // get the data - for _, sheet := range excelData.Sheets { - data := make(SheetData) + for _, sheet := range data.Sheets { + inner := make(SheetData) for rowLidx, row := range sheet.Rows { for colLidx, cell := range row.Cells { ex := ExtractedCell{ - Cell: cell, - ColL: colstream[colLidx], - RowLidx: rowLidx + 1, - Value: cell.Value} - cellref := fmt.Sprintf("%s%d", ex.ColL, ex.RowLidx) - data[cellref] = ex + Cell: cell, + Col: colstream[colLidx], + Row: rowLidx + 1, + Value: cell.Value} + cellref := fmt.Sprintf("%s%d", ex.Col, ex.Row) + inner[cellref] = ex } - output[sheet.Name] = data + outer[sheet.Name] = inner } } - return output + return outer } //Extract returns the file's data as a map, -// keyed on sheet name. All values are returned as strings. +// using the datamap as a filter, keyed on sheet name. All values +// are returned as strings. // Paths to a datamap and the spreadsheet file required. func Extract(dm string, ssheet string) ExtractedData { - data := ReadXLSX(dm, ssheet) - dmlData, err := ReadDML(dm) + xdata := ReadXLSX(ssheet) + ddata, err := ReadDML(dm) if err != nil { log.Fatal(err) } - sheetNames := getSheetNames(dmlData) - output := make(ExtractedData, len(sheetNames)) + names := getSheetNames(ddata) + outer := make(ExtractedData, len(names)) + inner := make(map[string]xlsx.Cell) - for _, i := range dmlData { + for _, i := range ddata { sheet := i.Sheet cellref := i.Cellref - if val, ok := data[sheet][cellref]; ok { - // TODO check what is happening here... - // ddg "golang assingment to entry in nil map" - // first SO entry, but I don't think I totally understand - inner := make(map[string]xlsx.Cell) + if val, ok := xdata[sheet][cellref]; ok { inner[cellref] = *val.Cell - output[sheet] = inner + outer[sheet] = inner } } - return output + return outer } diff --git a/reader/reader_test.go b/reader/reader_test.go index 77c2133..e6ac686 100644 --- a/reader/reader_test.go +++ b/reader/reader_test.go @@ -6,19 +6,18 @@ import ( func TestReadDML(t *testing.T) { d, _ := ReadDML("testdata/datamap.csv") - // Test Key values - if d[0].Key != "Project/Programme Name" { - t.Errorf("d[0].Key = %s; want Project/Programme Name", d[0].Key) - } - if d[1].Key != "Department" { - t.Errorf("d[1].Key = %s; want Department (without a space)", d[1].Key) - } - if d[2].Key != "Delivery Body" { - t.Errorf("d[2].Key = %s; want Delivery Body (without a space)", d[2].Key) - } - // Test Sheet values - if d[0].Sheet != "Introduction" { - t.Errorf("d[0].Sheet = %s; want Introduction", d[0].Key) + cases := []struct { + idx int + val string + }{ + {0, "Project/Programme Name"}, + {1, "Department"}, + {2, "Delivery Body"}, + } + for _, c := range cases { + if got := d[c.idx].Key; got != c.val { + t.Errorf("The test expected %s, got %s.", c.val, d[c.idx].Key) + } } } @@ -40,7 +39,7 @@ func TestBadDMLLine(t *testing.T) { func TestAlphaStream(t *testing.T) { if colstream[26] != "AA" { - t.Errorf("Expected AA, got %v", colstream[26]) + t.Errorf("The test expected AA, got %v.", colstream[26]) } if len(colstream) > maxCols { t.Errorf(`Number of columns in alphastream exceeds Excel maximum. @@ -51,29 +50,38 @@ func TestAlphaStream(t *testing.T) { func TestAlphaSingle(t *testing.T) { ab := alphabet() if ab[0] != "A" { - t.Errorf("Expected A, got %v", ab[0]) + t.Errorf("The test expected A, got %v.", ab[0]) } if ab[1] != "B" { - t.Errorf("Expected B, got %v", ab[1]) + t.Errorf("The test expected B, got %v.", ab[1]) } if ab[25] != "Z" { - t.Errorf("Expected Z, got %v", ab[25]) + t.Errorf("The test expected Z, got %v.", ab[25]) } } func TestAlphas(t *testing.T) { - ecs := cols(2) - if ecs[0] != "A" { - t.Errorf("Expected A, got %v", ecs[0]) - } - if ecs[25] != "Z" { - t.Errorf("Expected Z, got %v", ecs[25]) - } - if ecs[26] != "AA" { - t.Errorf("Expected AA, got %v", ecs[26]) - } - if ecs[52] != "BA" { - t.Errorf("Expected BA, got %v", ecs[52]) + a := 2 // two alphabets long + ecs := cols(a) + cases := []struct { + col int + val string + }{ + {0, "A"}, + {25, "Z"}, + {26, "AA"}, + {52, "BA"}, + } + for _, c := range cases { + // we're making sure we can pass that index + r := 26 * a + if c.col > r { + t.Fatalf("Cannot use %d as index to array of %d", c.col, r) + } + if got := ecs[c.col]; got != c.val { + t.Errorf("The test expected ecs[%d] to be %s - got %s.", + c.col, c.val, ecs[c.col]) + } } } @@ -81,28 +89,46 @@ func TestGetSheetsFromDM(t *testing.T) { slice, _ := ReadDML("testdata/datamap.csv") sheetNames := getSheetNames(slice) if len(sheetNames) != 14 { - t.Errorf("Expected 14 sheets in slice, got %d", + t.Errorf("The test expected 14 sheets in slice, got %d.", len(sheetNames)) } } func TestReadXLSX(t *testing.T) { - d := ReadXLSX("testdata/datamap.csv", "testdata/test_template.xlsx") - if d["Summary"]["A2"].Value != "Date:" { - t.Errorf("Expected A2 in Summary sheet to be 'Date:' - instead it is %s", d["Summary"]["A2"].Value) - } - if d["Another Sheet"]["F5"].Value != "4.2" { - t.Errorf("Expected F5 in Another Sheet sheet to be 4.2 - instead it is %s", d["Another Sheet"]["F5"].Value) - } - if d["Another Sheet"]["J22"].Value != "18" { - t.Errorf("Expected J22 in Another Sheet sheet to be 18 - instead it is %s", d["Another Sheet"]["J22"].Value) + d := ReadXLSX("testdata/test_template.xlsx") + cases := []struct { + sheet, cellref, val string + }{ + {"Summary", "A2", "Date:"}, + {"Summary", "IG10", "botticelli"}, + {"Another Sheet", "F5", "4.2"}, + {"Another Sheet", "J22", "18"}, + } + for _, c := range cases { + got := d[c.sheet][c.cellref].Value + if got != c.val { + t.Errorf("The test expected %s in %s sheet to be %s "+ + " - instead it is %s.", c.cellref, c.sheet, c.val, d[c.sheet][c.cellref].Value) + } } } func TestExtract(t *testing.T) { d := Extract("testdata/datamap.csv", "testdata/test_template.xlsx") - if d["Introduction"]["C9"].Value != "Test Department" { - t.Errorf("Expected C9 in Introduction sheet to be Test Department - instead it is %s", d["Introduction"]["C9"].Value) + cases := []struct { + sheet, cellref, val string + }{ + {"Introduction", "C9", "Test Department"}, + {"Introduction", "J9", "Greedy Parrots"}, + {"Introduction", "A1", "10"}, + } + for _, c := range cases { + got := d[c.sheet][c.cellref].Value + if got != c.val { + t.Errorf("The test expected %s in %s sheet to be %s "+ + "- instead it is %s.", c.sheet, c.cellref, c.val, + d[c.sheet][c.cellref].Value) + } } if d["Another Sheet"]["E26"].Value != "Integer:" { t.Errorf("Expected E26 in Another Sheet sheet to be Integer: - instead it is %s", d["Another Sheet"]["E26"].Value) diff --git a/reader/testdata/datamap.csv b/reader/testdata/datamap.csv index fbcaaf3..9bb71e2 100644 --- a/reader/testdata/datamap.csv +++ b/reader/testdata/datamap.csv @@ -5,6 +5,8 @@ Stoogge value,Another Sheet,E26 Delivery Body ,Introduction,C10 DRRDD - IPA ID Number,Introduction,C12 Controls Project ID number,Introduction,C13 +Jokey Entry,Introduction,A1 +Parrots Name,Introduction,J9 Project Type (for IPA use),Introduction,C14 Classification,Introduction,C15 DRRDD (GMPP - formally joined GMPP),Introduction,C16 diff --git a/reader/testdata/test_template.xlsx b/reader/testdata/test_template.xlsx Binary files differindex 5e2ea97..9e81d13 100644 --- a/reader/testdata/test_template.xlsx +++ b/reader/testdata/test_template.xlsx |