v0.0.351 sq value converter
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 2m30s
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 2m30s
This commit is contained in:
89
sq/sq_test.go
Normal file
89
sq/sq_test.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package sq
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/mattn/go-sqlite3"
|
||||
"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"`
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
dbdir := t.TempDir()
|
||||
dbfile1 := filepath.Join(dbdir, langext.MustHexUUID()+".sqlite3")
|
||||
|
||||
sqlite3.Version() // ensure loaded
|
||||
|
||||
tst.AssertNoErr(t, os.MkdirAll(dbdir, os.ModePerm))
|
||||
|
||||
url := fmt.Sprintf("file:%s?_journal=%s&_timeout=%d&_fk=%s&_busy_timeout=%d", dbfile1, "DELETE", 1000, "true", 1000)
|
||||
|
||||
xdb := tst.Must(sqlx.Open("sqlite3", url))(t)
|
||||
|
||||
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) {
|
||||
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")
|
||||
|
||||
sqlite3.Version() // ensure loaded
|
||||
|
||||
tst.AssertNoErr(t, os.MkdirAll(dbdir, os.ModePerm))
|
||||
|
||||
url := fmt.Sprintf("file:%s?_journal=%s&_timeout=%d&_fk=%s&_busy_timeout=%d", dbfile1, "DELETE", 1000, "true", 1000)
|
||||
|
||||
xdb := tst.Must(sqlx.Open("sqlite3", url))(t)
|
||||
|
||||
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())
|
||||
}
|
Reference in New Issue
Block a user