From f1ce07fd7ec3ed6702dcd90d1661f194a5ee463b Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Tue, 15 Aug 2023 14:33:54 +0200 Subject: [PATCH] storage: prepare for parallel testing of sqlite and pgsql --- storage/sql/sql.go | 7 ++-- tests/etc/{etc_sqlite.go => etc_sql.go} | 18 +++++++-- tests/storage_test.go | 49 ++++++++++++++++--------- 3 files changed, 50 insertions(+), 24 deletions(-) rename tests/etc/{etc_sqlite.go => etc_sql.go} (57%) diff --git a/storage/sql/sql.go b/storage/sql/sql.go index 6532d93..a2e3175 100644 --- a/storage/sql/sql.go +++ b/storage/sql/sql.go @@ -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) { +} diff --git a/tests/etc/etc_sqlite.go b/tests/etc/etc_sql.go similarity index 57% rename from tests/etc/etc_sqlite.go rename to tests/etc/etc_sql.go index 913931b..8f1fb53 100644 --- a/tests/etc/etc_sqlite.go +++ b/tests/etc/etc_sql.go @@ -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", } } diff --git a/tests/storage_test.go b/tests/storage_test.go index 0a96622..5391b07 100644 --- a/tests/storage_test.go +++ b/tests/storage_test.go @@ -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)`