aboutsummaryrefslogtreecommitdiffstats
path: root/internal/bolttest
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2024-08-18 14:51:00 -0400
committerGitHub <noreply@github.com>2024-08-18 14:51:00 -0400
commitd2186ed58d907a81648aa62ffa17ec20fb2d213e (patch)
treea5b2e26f1f96f333d48a8b20126ecc9ead697029 /internal/bolttest
parentMultiple issue fixes in readme (#3) (diff)
downloadcli-d2186ed58d907a81648aa62ffa17ec20fb2d213e.zip
GH-4: Add configurable migrations table name (#6)
* Add configurable migrations table name * Update codecov to be informational * Add mockdb and couple of extra tests * remove extra files * Add list query error test * Add readme update for custom migration table
Diffstat (limited to 'internal/bolttest')
-rw-r--r--internal/bolttest/db.go51
1 files changed, 43 insertions, 8 deletions
diff --git a/internal/bolttest/db.go b/internal/bolttest/db.go
index d03012f..2d21762 100644
--- a/internal/bolttest/db.go
+++ b/internal/bolttest/db.go
@@ -1,6 +1,7 @@
package bolttest
import (
+ "database/sql"
"fmt"
"os"
"testing"
@@ -15,10 +16,10 @@ func NewTestDB(t *testing.T) storage.DB {
db, err := storage.NewDB(connectionConfig)
assert.Nil(t, err)
- DropTable(t, db, "bolt_migrations")
+ DropTable(t, db, connectionConfig.MigrationsTable)
DropTable(t, db, "tmp")
t.Cleanup(func() {
- DropTable(t, db, "bolt_migrations")
+ DropTable(t, db, connectionConfig.MigrationsTable)
DropTable(t, db, "tmp")
assert.Nil(t, db.Close())
})
@@ -28,12 +29,13 @@ func NewTestDB(t *testing.T) storage.DB {
func NewTestConnectionConfig() configloader.ConnectionConfig {
return configloader.ConnectionConfig{
- Driver: os.Getenv("BOLT_DB_DRIVER"),
- DBName: os.Getenv("BOLT_DB_NAME"),
- Host: os.Getenv("BOLT_DB_HOST"),
- Port: os.Getenv("BOLT_DB_PORT"),
- User: os.Getenv("BOLT_DB_USER"),
- Password: os.Getenv("BOLT_DB_PASSWORD"),
+ Driver: os.Getenv("BOLT_DB_DRIVER"),
+ DBName: os.Getenv("BOLT_DB_NAME"),
+ Host: os.Getenv("BOLT_DB_HOST"),
+ Port: os.Getenv("BOLT_DB_PORT"),
+ User: os.Getenv("BOLT_DB_USER"),
+ Password: os.Getenv("BOLT_DB_PASSWORD"),
+ MigrationsTable: os.Getenv("BOLT_DB_MIGRATIONS_TABLE"),
}
}
@@ -41,3 +43,36 @@ func DropTable(t *testing.T, db storage.DB, tableName string) {
_, err := db.Exec(fmt.Sprintf("DROP TABLE IF EXISTS %s;", tableName))
assert.Nil(t, err)
}
+
+type MockDB struct {
+ ExecFunc func(query string, args ...interface{}) (sql.Result, error)
+ QueryFunc func(query string, args ...interface{}) (*sql.Rows, error)
+ QueryRowFunc func(query string, args ...interface{}) *sql.Row
+ TxFunc func(fn storage.TxFunc) error
+ CloseFunc func() error
+ TableExistsFunc func(tableName string) (bool, error)
+}
+
+func (m *MockDB) Exec(query string, args ...interface{}) (sql.Result, error) {
+ return m.ExecFunc(query, args...)
+}
+
+func (m *MockDB) Query(query string, args ...interface{}) (*sql.Rows, error) {
+ return m.QueryFunc(query, args...)
+}
+
+func (m *MockDB) QueryRow(query string, args ...interface{}) *sql.Row {
+ return m.QueryRowFunc(query, args...)
+}
+
+func (m *MockDB) Tx(fn storage.TxFunc) error {
+ return m.TxFunc(fn)
+}
+
+func (m *MockDB) Close() error {
+ return m.CloseFunc()
+}
+
+func (m *MockDB) TableExists(tableName string) (bool, error) {
+ return m.TableExistsFunc(tableName)
+}