goext/sq/sq_test.go

155 lines
4.2 KiB
Go
Raw Normal View History

2023-12-29 19:25:36 +01:00
package sq
import (
"context"
2024-01-05 16:55:53 +01:00
"database/sql"
2023-12-29 19:25:36 +01:00
"fmt"
2024-01-05 16:53:14 +01:00
"github.com/glebarez/go-sqlite"
2023-12-29 19:25:36 +01:00
"github.com/jmoiron/sqlx"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/rfctime"
"gogs.mikescher.com/BlackForestBytes/goext/tst"
"os"
"path/filepath"
"testing"
"time"
)
func TestTypeConverter1(t *testing.T) {
type RequestData struct {
ID string `db:"id"`
Timestamp time.Time `db:"timestamp"`
}
2024-01-05 16:55:53 +01:00
if !langext.InArray("sqlite3", sql.Drivers()) {
sqlite.RegisterAsSQLITE3()
}
2024-01-05 16:53:14 +01:00
2023-12-29 19:25:36 +01:00
ctx := context.Background()
dbdir := t.TempDir()
dbfile1 := filepath.Join(dbdir, langext.MustHexUUID()+".sqlite3")
tst.AssertNoErr(t, os.MkdirAll(dbdir, os.ModePerm))
2024-01-05 16:53:14 +01:00
url := fmt.Sprintf("file:%s?_pragma=journal_mode(%s)&_pragma=timeout(%d)&_pragma=foreign_keys(%s)&_pragma=busy_timeout(%d)", dbfile1, "DELETE", 1000, "true", 1000)
2023-12-29 19:25:36 +01:00
2024-01-05 16:53:14 +01:00
xdb := tst.Must(sqlx.Open("sqlite", url))(t)
2023-12-29 19:25:36 +01:00
db := NewDB(xdb)
db.RegisterDefaultConverter()
_, err := db.Exec(ctx, "CREATE TABLE `requests` ( id TEXT NOT NULL, timestamp INTEGER NOT NULL, PRIMARY KEY (id) ) STRICT", PP{})
tst.AssertNoErr(t, err)
_, err = InsertSingle(ctx, db, "requests", RequestData{
ID: "001",
Timestamp: time.Date(2000, 06, 15, 12, 0, 0, 0, time.UTC),
})
tst.AssertNoErr(t, err)
}
func TestTypeConverter2(t *testing.T) {
2024-01-05 16:53:14 +01:00
2024-01-05 16:55:53 +01:00
if !langext.InArray("sqlite3", sql.Drivers()) {
sqlite.RegisterAsSQLITE3()
}
2024-01-05 16:53:14 +01:00
2023-12-29 19:25:36 +01:00
type RequestData struct {
ID string `db:"id"`
Timestamp rfctime.UnixMilliTime `db:"timestamp"`
}
ctx := context.Background()
dbdir := t.TempDir()
dbfile1 := filepath.Join(dbdir, langext.MustHexUUID()+".sqlite3")
tst.AssertNoErr(t, os.MkdirAll(dbdir, os.ModePerm))
2024-01-05 16:53:14 +01:00
url := fmt.Sprintf("file:%s?_pragma=journal_mode(%s)&_pragma=timeout(%d)&_pragma=foreign_keys(%s)&_pragma=busy_timeout(%d)", dbfile1, "DELETE", 1000, "true", 1000)
2023-12-29 19:25:36 +01:00
2024-01-05 16:53:14 +01:00
xdb := tst.Must(sqlx.Open("sqlite", url))(t)
2023-12-29 19:25:36 +01:00
db := NewDB(xdb)
db.RegisterDefaultConverter()
_, err := db.Exec(ctx, "CREATE TABLE `requests` ( id TEXT NOT NULL, timestamp INTEGER NOT NULL, PRIMARY KEY (id) ) STRICT", PP{})
tst.AssertNoErr(t, err)
t0 := rfctime.NewUnixMilli(time.Date(2012, 03, 01, 16, 0, 0, 0, time.UTC))
_, err = InsertSingle(ctx, db, "requests", RequestData{
ID: "002",
Timestamp: t0,
})
tst.AssertNoErr(t, err)
r, err := QuerySingle[RequestData](ctx, db, "SELECT * FROM requests WHERE id = '002'", PP{}, SModeExtended, Safe)
tst.AssertNoErr(t, err)
fmt.Printf("%+v\n", r)
tst.AssertEqual(t, "002", r.ID)
tst.AssertEqual(t, t0.UnixNano(), r.Timestamp.UnixNano())
}
func TestTypeConverter3(t *testing.T) {
if !langext.InArray("sqlite3", sql.Drivers()) {
sqlite.RegisterAsSQLITE3()
}
type RequestData struct {
ID string `db:"id"`
Timestamp *rfctime.UnixMilliTime `db:"timestamp"`
}
ctx := context.Background()
dbdir := t.TempDir()
dbfile1 := filepath.Join(dbdir, langext.MustHexUUID()+".sqlite3")
tst.AssertNoErr(t, os.MkdirAll(dbdir, os.ModePerm))
url := fmt.Sprintf("file:%s?_pragma=journal_mode(%s)&_pragma=timeout(%d)&_pragma=foreign_keys(%s)&_pragma=busy_timeout(%d)", dbfile1, "DELETE", 1000, "true", 1000)
xdb := tst.Must(sqlx.Open("sqlite", url))(t)
db := NewDB(xdb)
db.RegisterDefaultConverter()
_, err := db.Exec(ctx, "CREATE TABLE `requests` ( id TEXT NOT NULL, timestamp INTEGER NULL, PRIMARY KEY (id) ) STRICT", PP{})
tst.AssertNoErr(t, err)
t0 := rfctime.NewUnixMilli(time.Date(2012, 03, 01, 16, 0, 0, 0, time.UTC))
_, err = InsertSingle(ctx, db, "requests", RequestData{
ID: "001",
Timestamp: &t0,
})
tst.AssertNoErr(t, err)
_, err = InsertSingle(ctx, db, "requests", RequestData{
ID: "002",
Timestamp: nil,
})
tst.AssertNoErr(t, err)
{
r1, err := QuerySingle[RequestData](ctx, db, "SELECT * FROM requests WHERE id = '001'", PP{}, SModeExtended, Safe)
tst.AssertNoErr(t, err)
fmt.Printf("%+v\n", r1)
tst.AssertEqual(t, "001", r1.ID)
tst.AssertEqual(t, t0.UnixNano(), r1.Timestamp.UnixNano())
}
{
r2, err := QuerySingle[RequestData](ctx, db, "SELECT * FROM requests WHERE id = '002'", PP{}, SModeExtended, Safe)
tst.AssertNoErr(t, err)
fmt.Printf("%+v\n", r2)
tst.AssertEqual(t, "002", r2.ID)
tst.AssertEqual(t, nil, r2.Timestamp)
}
}