diff options
-rw-r--r-- | pkg/datamaps/config.go | 9 | ||||
-rw-r--r-- | pkg/datamaps/db.go | 4 | ||||
-rw-r--r-- | pkg/datamaps/db_test.go | 4 | ||||
-rw-r--r-- | pkg/datamaps/reader.go | 22 | ||||
-rw-r--r-- | pkg/datamaps/reader_test.go | 10 |
5 files changed, 26 insertions, 23 deletions
diff --git a/pkg/datamaps/config.go b/pkg/datamaps/config.go index 7c7f8e7..694fec4 100644 --- a/pkg/datamaps/config.go +++ b/pkg/datamaps/config.go @@ -30,9 +30,10 @@ type dbPathChecker struct { getUserConfigDir GetUserConfigDir } -// NewDBPathChecker creaes a DBPathChecker using whatever -// func you want as the argument, as long as it produces -// the user config directory. +// NewDBPathChecker creates a DBPathChecker using whatever +// func you want as the argument, as long as it matches the +// type os.UserConfigDir. This makes it convenient for testing +// and was done as an experiment here to practice mocking in Go. func NewDBPathChecker(h GetUserConfigDir) *dbPathChecker { return &dbPathChecker{getUserConfigDir: h} } @@ -76,7 +77,7 @@ func SetUp() (string, error) { return "", err } log.Printf("Creating database file at %s\n", dbPath) - _, err := SetupDB(dbPath) + _, err := setupDB(dbPath) if err != nil { return "", err } diff --git a/pkg/datamaps/db.go b/pkg/datamaps/db.go index 9ae2204..ad612c4 100644 --- a/pkg/datamaps/db.go +++ b/pkg/datamaps/db.go @@ -12,8 +12,8 @@ import ( _ "github.com/mattn/go-sqlite3" ) -// SetupDB creates the intitial database -func SetupDB(path string) (*sql.DB, error) { +// setupDB creates the intitial database +func setupDB(path string) (*sql.DB, error) { stmtBase := `DROP TABLE IF EXISTS datamap; CREATE TABLE datamap(id INTEGER PRIMARY KEY, name TEXT, date_created TEXT); DROP TABLE IF EXISTS datamap_line; diff --git a/pkg/datamaps/db_test.go b/pkg/datamaps/db_test.go index 4d2179f..2055d8a 100644 --- a/pkg/datamaps/db_test.go +++ b/pkg/datamaps/db_test.go @@ -5,7 +5,7 @@ import ( ) func TestOpenSQLiteFile(t *testing.T) { - db, err := SetupDB("./testdata/test.db") + db, err := setupDB("./testdata/test.db") defer db.Close() if err != nil { @@ -37,7 +37,7 @@ func TestOpenSQLiteFile(t *testing.T) { } func TestDatamapGoesIntoDB(t *testing.T) { - db, err := SetupDB("./testdata/test.db") + db, err := setupDB("./testdata/test.db") defer db.Close() if err != nil { diff --git a/pkg/datamaps/reader.go b/pkg/datamaps/reader.go index 326224a..311dceb 100644 --- a/pkg/datamaps/reader.go +++ b/pkg/datamaps/reader.go @@ -22,11 +22,13 @@ import ( ) type ( - // SheetData is the data from the sheet + // SheetData is the data from the sheet. SheetData map[string]ExtractedCell + // FileData is the data from the file. FileData map[string]SheetData - // ExtractedData is the Extraced data from the file, filtered by a Datamap. + + // ExtractedData is the Extracted data from the file, filtered by a Datamap. ExtractedData map[string]map[string]xlsx.Cell ) @@ -111,12 +113,12 @@ func ReadDML(path string) (ExtractedDatamapFile, error) { return s, nil } -// ReadXLSX returns the file's data as a map, +// ReadXLSX returns a file at path'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(ssheet string) FileData { +func ReadXLSX(path string) FileData { // open the files - data, err := xlsx.OpenFile(ssheet) + data, err := xlsx.OpenFile(path) if err != nil { log.Fatal(err) } @@ -186,9 +188,9 @@ func DatamapFromDB(name string, db *sql.DB) (ExtractedDatamapFile, error) { return out, nil } -// ExtractDBDM uses a datamap named from the database db to extract values +// ExtractDBDatamap uses a datamap named from the database db to extract values // from the populated spreadsheet file file. -func ExtractDBDM(name string, file string, db *sql.DB) (ExtractedData, error) { +func ExtractDBDatamap(name string, file string, db *sql.DB) (ExtractedData, error) { xdata := ReadXLSX(file) ddata, err := DatamapFromDB(name, db) // this will need to return an ExtractedDatamapFile @@ -213,12 +215,12 @@ func ExtractDBDM(name string, file string, db *sql.DB) (ExtractedData, error) { return outer, nil } -//Extract returns the file's data as a map, +//Extract returns the file at path's data as a map, // 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 { - xdata := ReadXLSX(ssheet) +func Extract(dm string, path string) ExtractedData { + xdata := ReadXLSX(path) ddata, err := ReadDML(dm) if err != nil { diff --git a/pkg/datamaps/reader_test.go b/pkg/datamaps/reader_test.go index 79d636e..e5f2c98 100644 --- a/pkg/datamaps/reader_test.go +++ b/pkg/datamaps/reader_test.go @@ -72,7 +72,7 @@ func TestReadXLSX(t *testing.T) { // func TestExtractWithDBDatamap(t *testing.T) { // // setup - we need the datamap in the test database -// db, err := SetupDB("./testdata/test.db") +// db, err := setupDB("./testdata/test.db") // defer db.Close() // if err != nil { @@ -89,12 +89,12 @@ func TestReadXLSX(t *testing.T) { // t.Errorf("Unable to write datamap to database file because %v.", err) // } -// d := ExtractDBDM("First Datamap", "testdata/test_template.xlsx") +// d := ExtractDBDatamap("First Datamap", "testdata/test_template.xlsx") // } func TestDMLSliceFromDatabase(t *testing.T) { // setup - we need the datamap in the test database - db, err := SetupDB("./testdata/test.db") + db, err := setupDB("./testdata/test.db") defer db.Close() if err != nil { @@ -141,7 +141,7 @@ 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") + db, err := setupDB("./testdata/test.db") defer db.Close() if err != nil { @@ -158,7 +158,7 @@ func TestExtractUsingDBDM(t *testing.T) { t.Errorf("Unable to write datamap to database file because %v.", err) } - d, _ := ExtractDBDM("First Datamap", "testdata/test_template.xlsx", db) + d, _ := ExtractDBDatamap("First Datamap", "testdata/test_template.xlsx", db) cases := []struct { sheet, cellref, val string }{ |