goptrack
first commit of new line of development.
This commit is contained in:
40
pkg/tradebook/tradebook.go
Normal file
40
pkg/tradebook/tradebook.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package tradebook
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
type tradebook struct {
|
||||
driver string
|
||||
dsn string
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
type openOption func(*tradebook) error
|
||||
|
||||
func Open(opts ...openOption) (*tradebook, error) {
|
||||
t := tradebook{}
|
||||
// Apply Options
|
||||
for _, opt := range opts {
|
||||
opt(&t)
|
||||
}
|
||||
|
||||
db, err := sql.Open(t.driver, t.dsn)
|
||||
t.db = db
|
||||
if (err == nil) {
|
||||
err = db.Ping()
|
||||
}
|
||||
|
||||
return &t, err
|
||||
}
|
||||
|
||||
func Sqlite(fpath string) openOption {
|
||||
return func(t *tradebook) error {
|
||||
t.driver = "sqlite"
|
||||
t.dsn = fpath
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
29
pkg/tradebook/tradebook_test.go
Normal file
29
pkg/tradebook/tradebook_test.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package tradebook
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func TestOpenSQLite(t *testing.T) {
|
||||
tdir := t.TempDir()
|
||||
dpath := filepath.Join(tdir, "db")
|
||||
|
||||
tb, err := Open(Sqlite(dpath))
|
||||
if (err != nil) {
|
||||
t.Errorf("Open returned error: %v", err)
|
||||
}
|
||||
|
||||
if (tb == nil) {
|
||||
t.Error("Db is nil")
|
||||
}
|
||||
|
||||
// for sqlite dsn is just a file path
|
||||
if (tb.dsn != dpath) {
|
||||
t.Errorf( "DSN does not match! (%v != %v).", dpath, tb.dsn)
|
||||
}
|
||||
|
||||
if (tb.driver != "sqlite") {
|
||||
t.Errorf("Driver is not sqlite (%v != %v).", tb.driver, "sqlite")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user