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 {
driver := cfg.Driver
connStr := cfg.Connstr
db, err := sql.Open(driver, connStr)
db, err := sql.Open(cfg.Driver, cfg.Connstr)
if err != nil {
log.Error(err).Msg("sql.Open")
return nil
}
return &Storage{db}
}
func Exec(db *Storage, s string) {
}

View file

@ -6,24 +6,34 @@ import (
)
func ConfigSqlite() *sql.Cfg {
ovr := OverridesSqlite().Use
ovr := OverridesSql().Use
return &sql.Cfg{
Driver: "sqlite",
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
// explicit Override() or SCOPES_* environment settings.
const (
SQLITE_CONNSTR = "sqlite_connstr"
PGSQL_CONNSTR = "pgsql_connstr"
)
// in a production scenario this should be put in a separate
// file `settings.go` that should not be stored in code repository.
func OverridesSqlite() config.Settings {
func OverridesSql() 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/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/tests/etc"
)
var (
cfg_sqlite *sql.Cfg
db_sqlite *sql.Storage
)
func TestSqlite(tb *tbase.T) {
cfg_sqlite = etc.ConfigSqlite()
db_sqlite = sql.Open(cfg_sqlite)
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) {
fmt.Println(cfg_sqlite.Connstr)
func TestPgsql(tb *tbase.T) {
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
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
var sqlite_create_table = `create table test (
id integer not null primary key,
label text)`
var sqlite_insert = `insert into test (label) values
('Hello World'),
('Good Afternoon')`
var sqlite_query = `select label from test
where id = $1`
var pgsql_create_table = `create table test (
id serial not null primary key,
label text)`