storage: prepare for parallel testing of sqlite and pgsql

This commit is contained in:
Helmut Merz 2023-08-15 14:33:54 +02:00
parent ecceb9803b
commit f1ce07fd7e
3 changed files with 50 additions and 24 deletions

View file

@ -15,12 +15,13 @@ type Storage struct {
} }
func Open(cfg *Cfg) *Storage { func Open(cfg *Cfg) *Storage {
driver := cfg.Driver db, err := sql.Open(cfg.Driver, cfg.Connstr)
connStr := cfg.Connstr
db, err := sql.Open(driver, connStr)
if err != nil { if err != nil {
log.Error(err).Msg("sql.Open") log.Error(err).Msg("sql.Open")
return nil return nil
} }
return &Storage{db} return &Storage{db}
} }
func Exec(db *Storage, s string) {
}

View file

@ -6,24 +6,34 @@ import (
) )
func ConfigSqlite() *sql.Cfg { func ConfigSqlite() *sql.Cfg {
ovr := OverridesSqlite().Use ovr := OverridesSql().Use
return &sql.Cfg{ return &sql.Cfg{
Driver: "sqlite", Driver: "sqlite",
Connstr: ovr("somefile.sqlite", SQLITE_CONNSTR), Connstr: ovr("somefile.sqlite", SQLITE_CONNSTR),
} }
} }
func ConfigPgsql() *sql.Cfg {
ovr := OverridesSql().Use
return &sql.Cfg{
Driver: "postgres",
Connstr: ovr("user=ccotest password=cco dbname=ccotest", PGSQL_CONNSTR),
}
}
// collect here the names of fields that may be overridden via // collect here the names of fields that may be overridden via
// explicit Override() or SCOPES_* environment settings. // explicit Override() or SCOPES_* environment settings.
const ( const (
SQLITE_CONNSTR = "sqlite_connstr" SQLITE_CONNSTR = "sqlite_connstr"
PGSQL_CONNSTR = "pgsql_connstr"
) )
// in a production scenario this should be put in a separate // in a production scenario this should be put in a separate
// file `settings.go` that should not be stored in code repository. // file `settings.go` that should not be stored in code repository.
func OverridesSqlite() config.Settings { func OverridesSql() config.Settings {
return config.Settings{ return config.Settings{
SQLITE_CONNSTR: ":memory", //SQLITE_CONNSTR: ":memory",
SQLITE_CONNSTR: "data/scopes.sqlite",
PGSQL_CONNSTR: "user=ccotest password=cco dbname=ccotest",
} }
} }

View file

@ -6,38 +6,53 @@ import (
"git.sr.ht/~cco/go-scopes/common/testing" "git.sr.ht/~cco/go-scopes/common/testing"
"git.sr.ht/~cco/go-scopes/storage/sql" "git.sr.ht/~cco/go-scopes/storage/sql"
_ "git.sr.ht/~cco/go-scopes/storage/sql/pgsql"
_ "git.sr.ht/~cco/go-scopes/storage/sql/sqlite" _ "git.sr.ht/~cco/go-scopes/storage/sql/sqlite"
"git.sr.ht/~cco/go-scopes/tests/etc" "git.sr.ht/~cco/go-scopes/tests/etc"
) )
var (
cfg_sqlite *sql.Cfg
db_sqlite *sql.Storage
)
func TestSqlite(tb *tbase.T) { func TestSqlite(tb *tbase.T) {
cfg_sqlite = etc.ConfigSqlite()
db_sqlite = sql.Open(cfg_sqlite)
t := testing.SetUp(tb) t := testing.SetUp(tb)
t.Run("sqlite-base", SqliteBaseTest) cfg := etc.ConfigSqlite()
db := sql.Open(cfg)
// resetSqlite(db)
DoTests(t, cfg, db)
} }
func SqliteBaseTest(t *testing.T) { func TestPgsql(tb *tbase.T) {
fmt.Println(cfg_sqlite.Connstr) t := testing.SetUp(tb)
cfg := etc.ConfigPgsql()
db := sql.Open(cfg)
// resetPgsql(db)
DoTests(t, cfg, db)
}
func DoTests(t *testing.T, cfg *sql.Cfg, db *sql.Storage) {
t.Run("base", func(t *testing.T) { BaseTest(t, cfg, db) })
}
func BaseTest(t *testing.T, cfg *sql.Cfg, db *sql.Storage) {
fmt.Println(cfg.Connstr)
} }
// SQL statements // SQL statements
var sqlite_drop_table = `drop table test` var drop_table = `drop table test`
var insert = `insert into test (label) values
('Hello World'),
('Good Afternoon')`
var query = `select label from test
where id = $1`
// database-specific SQL statements
// note: ... integer ... primary key ... automatically fills row sequentially // note: ... integer ... primary key ... automatically fills row sequentially
var sqlite_create_table = `create table test ( var sqlite_create_table = `create table test (
id integer not null primary key, id integer not null primary key,
label text)` label text)`
var sqlite_insert = `insert into test (label) values var pgsql_create_table = `create table test (
('Hello World'), id serial not null primary key,
('Good Afternoon')` label text)`
var sqlite_query = `select label from test
where id = $1`