go-scopes/tests/storage_test.go

130 lines
3.4 KiB
Go

package scopes
import (
tbase "testing"
"time"
lib "git.sr.ht/~cco/go-scopes"
"git.sr.ht/~cco/go-scopes/common/testing"
sql "git.sr.ht/~cco/go-scopes/storage"
"git.sr.ht/~cco/go-scopes/storage/db/pgsql"
_ "git.sr.ht/~cco/go-scopes/storage/db/sqlite"
"git.sr.ht/~cco/go-scopes/storage/message"
"git.sr.ht/~cco/go-scopes/storage/tracking"
"git.sr.ht/~cco/go-scopes/tests/etc"
)
func TestSqlite(tb *tbase.T) {
t := testing.SetUp(tb)
cfg := etc.ConfigSqlite()
db := sql.Open(cfg)
resetSqlite(db)
DoTests(t, cfg, db)
}
func TestPgsql(tb *tbase.T) {
t := testing.SetUp(tb)
cfg := etc.ConfigPgsql()
db := sql.Open(cfg)
db.Params["idType"] = pgsql.IdType
db.Params["jsonType"] = pgsql.JsonType
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) })
t.Run("message", func(t *testing.T) { MessageTest(t, cfg, db) })
t.Run("tracking", func(t *testing.T) { TrackingTest(t, cfg, db) })
}
type greet struct {
id int64
label string
}
func (g greet) Scan(rows *sql.Rows) (greet, error) {
err := rows.Scan(&g.id, &g.label)
return g, err
}
func (g *greet) ScanP(rows *sql.Rows) error {
return rows.Scan(&g.id, &g.label)
}
func BaseTest(t *testing.T, cfg *sql.Cfg, db *sql.Storage) {
db.Exec(sql_insert)
//data := sql.QueryCol[string](db, query, 2)
data := sql.QueryData[greet](db, sql_query)
t.AssertEqual(len(data), 2)
t.AssertEqual(data[0].label, "Hello World")
t.AssertEqual(data[1].label, "Good Afternoon")
data = sql.QueryDataP[*greet](db, sql_query)
t.AssertEqual(len(data), 2)
t.AssertEqual(data[0].label, "Hello World")
t.AssertEqual(data[1].label, "Good Afternoon")
}
func TrackingTest(t *testing.T, cfg *sql.Cfg, db *sql.Storage) {
cont := tracking.Tracks(db)
cont.CreateTable()
track := tracking.New(cont, "t01", "john")
cont.Insert(track)
t.AssertEqual(track.TrackId(), lib.Ident(1))
t.AssertEqual(track.TimeStamp().Year(), time.Now().Year())
tr2 := cont.Get(1)
//fmt.Printf("%+v\n", tr2.TimeStamp())
t.AssertEqual(tr2.Head()["userName"], "john")
tr3 := cont.QueryLast("", "john")
t.AssertEqual(tr3.Head()["taskId"], "t01")
tr3.SetData(lib.Map{"desc": "go-scopes documentation"})
cont.Update(tr3)
tr4 := tracking.Get(db, "rec-1")
t.AssertEqual(tr4.Data()["desc"], "go-scopes documentation")
}
func MessageTest(t *testing.T, cfg *sql.Cfg, db *sql.Storage) {
//msg := message.SimpleMessage("data")
//msgstore.StoreDB(db, msg)
cont := message.Messages(db)
cont.CreateTable()
msg := message.New(cont, "", "data")
cont.Insert(msg)
t.AssertEqual(msg.TrackId(), lib.Ident(1))
t.AssertEqual(msg.Domain(), "scopes")
t.AssertEqual(msg.Uid(), "msg-1")
}
func resetSqlite(db *sql.Storage) {
db.DropTable("test")
db.DropTable("tracks")
db.DropTable("messages")
db.Exec(sqlite_create_table)
}
func resetPgsql(db *sql.Storage) {
db.DropTable("test")
db.DropTable("testing.tracks")
db.DropTable("testing.messages")
db.Exec(pgsql_create_table)
}
// SQL statements
const (
sql_insert = `insert into test (label) values
('Hello World'),
('Good Afternoon')`
sql_query = `select id, label from test ` //where id = $1`
// database-specific SQL statements
// note: ... integer ... primary key ... automatically fills row sequentially
sqlite_create_table = `create table test (
id integer not null primary key,
label text)`
pgsql_create_table = `create table test (
id serial not null primary key,
label text)`
)