diff options
Diffstat (limited to 'pkg/datamaps')
-rw-r--r-- | pkg/datamaps/reader.go | 39 | ||||
-rw-r--r-- | pkg/datamaps/reader_test.go | 45 |
2 files changed, 74 insertions, 10 deletions
diff --git a/pkg/datamaps/reader.go b/pkg/datamaps/reader.go index 050853a..61771c0 100644 --- a/pkg/datamaps/reader.go +++ b/pkg/datamaps/reader.go @@ -147,7 +147,9 @@ func ReadXLSX(ssheet string) FileData { return outer } -func DMLFromDB(name string, db *sql.DB) []DatamapLine { +// DMLFromDB fetches a slice of DatamapLine from the database given +// the name of a datamap. +func DMLFromDB(name string, db *sql.DB) ([]DatamapLine, error) { var out []DatamapLine @@ -159,7 +161,7 @@ func DMLFromDB(name string, db *sql.DB) []DatamapLine { ` rows, err := db.Query(query, name) if err != nil { - log.Fatal(err) + return nil, err } defer rows.Close() @@ -170,20 +172,39 @@ func DMLFromDB(name string, db *sql.DB) []DatamapLine { cellref string ) if err := rows.Scan(&key, &sheet, &cellref); err != nil { - log.Fatal(err) + return nil, err } - // log.Printf("key %s\nsheet %s\ncellref %v", key, sheet, cellref) out = append(out, DatamapLine{Key: key, Sheet: sheet, Cellref: cellref}) } - return out + return out, nil } -// func ExtractDBDM(name string, file string) ExtractedData { -// xdata := ReadXLSX(file) -// // ddata, err := DMLFromDB(name) // this will need to return a []DatamapLine -// } +func ExtractDBDM(name string, file string, db *sql.DB) ExtractedData { + xdata := ReadXLSX(file) + ddata, err := DMLFromDB(name, db) // this will need to return a []DatamapLine + + if err != nil { + log.Fatal(err) + } + + names := getSheetNames(ddata) + outer := make(ExtractedData, len(names)) + inner := make(map[string]xlsx.Cell) + + for _, i := range ddata { + sheet := i.Sheet + cellref := i.Cellref + + if val, ok := xdata[sheet][cellref]; ok { + inner[cellref] = *val.Cell + outer[sheet] = inner + } + } + + return outer +} //Extract returns the file's data as a map, // using the datamap as a filter, keyed on sheet name. All values diff --git a/pkg/datamaps/reader_test.go b/pkg/datamaps/reader_test.go index ec158a1..de169dd 100644 --- a/pkg/datamaps/reader_test.go +++ b/pkg/datamaps/reader_test.go @@ -125,7 +125,7 @@ func TestDMLSliceFromDatabase(t *testing.T) { {7, "Parrots Name"}, } - data := DMLFromDB("First Datamap", db) + data, _ := DMLFromDB("First Datamap", db) for _, c := range cases { got := data[c.index].Key @@ -139,6 +139,49 @@ func TestDMLSliceFromDatabase(t *testing.T) { } } +func TestExtractUsingDBDM(t *testing.T) { + // setup - we need the datamap in the test database + db, err := SetupDB("./testdata/test.db") + defer db.Close() + + if err != nil { + t.Fatal("Expected to be able to set up the database.") + } + + opts := Options{ + DBPath: "./testdata/test.db", + DMName: "First Datamap", + DMPath: "./testdata/datamap.csv", + } + + if err := DatamapToDB(&opts); err != nil { + t.Errorf("Unable to write datamap to database file because %v.", err) + } + + d := ExtractDBDM("First Datamap", "testdata/test_template.xlsx", db) + cases := []struct { + sheet, cellref, val string + }{ + {"Introduction", "C9", "Test Department"}, + {"Introduction", "J9", "Greedy Parrots"}, + {"Introduction", "A1", "10"}, + {"Introduction", "C22", "VUNT"}, + } + + 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) + } +} + func TestExtract(t *testing.T) { d := Extract("testdata/datamap.csv", "testdata/test_template.xlsx") cases := []struct { |