aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Lemon <matt@matthewlemon.com>2020-07-26 17:14:25 +0100
committerMatthew Lemon <matt@matthewlemon.com>2020-07-26 17:14:25 +0100
commit73e42a5e67c5d166a36bfe1473ef1ec05a761168 (patch)
tree773bac8128ed876b2381286be9e64a404b014205
parent4c29ab5529faccf927fb48e1883f8ec1b0464a05 (diff)
adding test and code for pulling []DatamapLine from sqlite3 file
Diffstat (limited to '')
-rw-r--r--pkg/datamaps/reader.go34
-rw-r--r--pkg/datamaps/reader_test.go44
2 files changed, 78 insertions, 0 deletions
diff --git a/pkg/datamaps/reader.go b/pkg/datamaps/reader.go
index bc339e8..ef4ef1a 100644
--- a/pkg/datamaps/reader.go
+++ b/pkg/datamaps/reader.go
@@ -4,6 +4,7 @@
package datamaps
import (
+ "database/sql"
"encoding/csv"
"errors"
"fmt"
@@ -146,6 +147,39 @@ func ReadXLSX(ssheet string) FileData {
return outer
}
+func DMLFromDB(name string, db *sql.DB) []DatamapLine {
+
+ query := `
+ select
+ key, sheet, cellref
+ from datamap_line
+ join datamap on datamap_line.dm_id = datamap.id where datamap.name = ?;
+ `
+ rows, err := db.Query(query, name)
+ if err != nil {
+ log.Fatal(err)
+ }
+ defer rows.Close()
+
+ for rows.Next() {
+ var (
+ key string
+ sheet string
+ cellref string
+ )
+ if err := rows.Scan(&key, &sheet, &cellref); err != nil {
+ log.Fatal(err)
+ }
+ log.Printf("key %s\nsheet %s\ncellref %v", key, sheet, cellref)
+ }
+ return make([]DatamapLine, 0)
+}
+
+// func ExtractDBDM(name string, file string) ExtractedData {
+// xdata := ReadXLSX(file)
+// // ddata, err := DMLFromDB(name) // this will need to return a []DatamapLine
+// }
+
//Extract returns the file's data as a map,
// using the datamap as a filter, keyed on sheet name. All values
// are returned as strings.
diff --git a/pkg/datamaps/reader_test.go b/pkg/datamaps/reader_test.go
index a0b3ffe..be89e5f 100644
--- a/pkg/datamaps/reader_test.go
+++ b/pkg/datamaps/reader_test.go
@@ -70,6 +70,50 @@ 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")
+// 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")
+// }
+
+func TestDMLSliceFromDatabase(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)
+ }
+ data := DMLFromDB("First Datamap", db)
+ t.Log(data)
+}
+
func TestExtract(t *testing.T) {
d := Extract("testdata/datamap.csv", "testdata/test_template.xlsx")
cases := []struct {