aboutsummaryrefslogtreecommitdiff
path: root/dbstore
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2022-02-05 17:59:24 -0500
committerPaul Duncan <pabs@pablotron.org>2022-02-05 17:59:24 -0500
commitd33541ff79e5d81959900b757e4857af14e4f4b2 (patch)
tree8674398bec363062569a53427fbf71ceffe2592f /dbstore
parentc89f404a0176415130089201a176a3ff76a02829 (diff)
downloadcvez-d33541ff79e5d81959900b757e4857af14e4f4b2.tar.bz2
cvez-d33541ff79e5d81959900b757e4857af14e4f4b2.zip
dbscore: add openFull and test
Diffstat (limited to 'dbstore')
-rw-r--r--dbstore/dbstore.go17
-rw-r--r--dbstore/dbstore_test.go9
2 files changed, 19 insertions, 7 deletions
diff --git a/dbstore/dbstore.go b/dbstore/dbstore.go
index 0ab6fcf..11ad02c 100644
--- a/dbstore/dbstore.go
+++ b/dbstore/dbstore.go
@@ -13,22 +13,33 @@ import (
//go:embed sql
var sqlFs embed.FS
+// sqlite3 backing store
type DbStore struct {
db *db_sql.DB
}
-// open database
-func Open(path string) (DbStore, error) {
+// Open database.
+//
+// This function is called by Open(). It is a separate package-private
+// function to make Open() easier to test.
+func openFull(dbType, path string) (DbStore, error) {
var r DbStore
+
// init db
- if db, err := db_sql.Open("sqlite3", path); err != nil {
+ if db, err := db_sql.Open(dbType, path); err != nil {
return r, err
} else {
+ // save handle
r.db = db
return r, nil
}
}
+// Open database
+func Open(path string) (DbStore, error) {
+ return openFull("sqlite3", path)
+}
+
// initialized database version
const initDbVersion = 314159
diff --git a/dbstore/dbstore_test.go b/dbstore/dbstore_test.go
index fe653dc..1e03d72 100644
--- a/dbstore/dbstore_test.go
+++ b/dbstore/dbstore_test.go
@@ -198,19 +198,20 @@ func seedTestDb(ctx context.Context, db DbStore) error {
// TODO: seed with other data
}
-func TestOpen(t *testing.T) {
+func TestOpenFull(t *testing.T) {
tests := []struct {
name string
+ dbType string
path string
exp bool
} {
- { "pass", "./testdata/test-open.db", true },
- // { "fail", "file://invalid/foobar", false },
+ { "pass", "sqlite3", "./testdata/test-open.db", true },
+ { "fail", "invalid driver", "file://invalid/foobar", false },
}
for _, test := range(tests) {
t.Run(test.name, func(t *testing.T) {
- got, err := Open(test.path)
+ got, err := openFull(test.dbType, test.path)
if test.exp && err != nil {
t.Error(err)
} else if !test.exp && err == nil {