176 lines
4.3 KiB
Go
176 lines
4.3 KiB
Go
package query
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"testing"
|
|
|
|
"gorm.io/driver/mysql"
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type migrationRow struct {
|
|
ID int64 `gorm:"primaryKey"`
|
|
TenantID int64
|
|
Name string
|
|
Other string
|
|
Enabled bool
|
|
Count int64
|
|
Nullable *string
|
|
}
|
|
type migrationColumns struct{ ID, TenantID, Name, Other, Enabled, Count, Nullable string }
|
|
|
|
func (migrationRow) TableName() string { return "migration_probe" }
|
|
func (migrationRow) Columns() migrationColumns {
|
|
return migrationColumns{"id", "tenant_id", "name", "other", "enabled", "count", "nullable"}
|
|
}
|
|
|
|
type migrationUpdate struct {
|
|
Enabled *bool `gorm:"column:enabled"`
|
|
Count any `gorm:"column:count;type:bigint"`
|
|
Nullable **string `gorm:"column:nullable"`
|
|
}
|
|
|
|
// TestDAOAgainstSQLite 验证会话隔离、更新语义、事务以及公共条件与 OR 的组合。
|
|
func TestDAOAgainstSQLite(t *testing.T) {
|
|
var (
|
|
database, err = gorm.Open(sqlite.Open("file:"+t.Name()+"?mode=memory&cache=shared"), &gorm.Config{})
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
runMigrationContract(t, database)
|
|
}
|
|
|
|
// TestDAOAgainstMySQL 使用显式注入的临时测试 DSN 验证真实 MySQL 方言,不读取部署凭据。
|
|
func TestDAOAgainstMySQL(t *testing.T) {
|
|
var (
|
|
dsn = os.Getenv("GINFRAMEWORK_TEST_MYSQL_DSN")
|
|
)
|
|
if dsn == "" {
|
|
t.Skip("isolated MySQL DSN is not provided")
|
|
}
|
|
var (
|
|
database, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
runMigrationContract(t, database)
|
|
}
|
|
func runMigrationContract(t *testing.T, database *gorm.DB) {
|
|
var (
|
|
dao = &BaseDao[migrationRow, migrationColumns]{}
|
|
|
|
col = (migrationRow{}).Columns()
|
|
|
|
source = "initial"
|
|
|
|
records = []migrationRow{{
|
|
ID: 1,
|
|
TenantID: 7,
|
|
Name: "alpha",
|
|
Enabled: true,
|
|
Count: 2,
|
|
Nullable: &source,
|
|
}, {
|
|
ID: 2,
|
|
TenantID: 7,
|
|
Other: "alpha",
|
|
}, {
|
|
ID: 3,
|
|
TenantID: 8,
|
|
Name: "alpha",
|
|
}, {
|
|
ID: 4,
|
|
TenantID: 7,
|
|
Name: "unmatched",
|
|
}}
|
|
|
|
result []migrationRow
|
|
|
|
total int64
|
|
|
|
row migrationRow
|
|
|
|
clear *string
|
|
|
|
rollback = errors.New("force rollback")
|
|
)
|
|
t.Helper()
|
|
if pool, err := database.DB(); err == nil {
|
|
t.Cleanup(func() { _ = pool.Close() })
|
|
}
|
|
if err := database.AutoMigrate(&migrationRow{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := dao.Create(database, &records); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := dao.ListAndTotal(database, NewListTarget(&result, &total, false), WithWhere(map[string]any{
|
|
col.TenantID: 7,
|
|
}), WithOR(WithLike(col.Name, "alpha"), WithLike(col.Other, "alpha")), WithOrderAsc(col.ID), WithLimit(1, 0)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if total != 2 || len(result) != 1 || result[0].ID != 1 {
|
|
t.Fatalf("OR escaped ownership or count lost filters: total=%d records=%+v", total, result)
|
|
}
|
|
if affected, err := dao.UpdateOne(database, migrationUpdate{
|
|
Enabled: new(false),
|
|
Count: gorm.Expr(col.Count+" + ?", 3),
|
|
Nullable: &clear,
|
|
}, WithWhere(map[string]any{
|
|
col.ID: 1,
|
|
col.TenantID: 7,
|
|
})); err != nil || affected != 1 {
|
|
t.Fatalf("update: %d %v", affected, err)
|
|
}
|
|
if err := dao.Detail(database, &row, WithWhere(map[string]any{
|
|
col.ID: 1,
|
|
})); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if row.Enabled || row.Nullable != nil || row.Count != 5 {
|
|
t.Fatalf("update lost zero, NULL or expression: %+v", row)
|
|
}
|
|
if err := database.Transaction(func(tx *gorm.DB) error {
|
|
if err := dao.Create(tx, &migrationRow{
|
|
ID: 5,
|
|
TenantID: 7,
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
return rollback
|
|
}); !errors.Is(err, rollback) {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := dao.Count(database, WithWhere(map[string]any{
|
|
col.ID: 5,
|
|
})); err != nil {
|
|
t.Fatal(err)
|
|
} else if count, _ := dao.Count(database, WithWhere(map[string]any{
|
|
col.ID: 5,
|
|
})); count != 0 {
|
|
t.Fatal("transaction escaped supplied handle")
|
|
}
|
|
row = migrationRow{}
|
|
if err := dao.Detail(database.Where(map[string]any{
|
|
col.ID: 999,
|
|
}), &row, WithWhere(map[string]any{
|
|
col.ID: 1,
|
|
})); err != nil || row.ID != 1 {
|
|
t.Fatalf("upstream query state leaked into DAO: %+v %v", row, err)
|
|
}
|
|
var (
|
|
ctx, cancel = context.WithCancel(context.Background())
|
|
)
|
|
cancel()
|
|
if err := dao.Detail(database.WithContext(ctx), &row, WithWhere(map[string]any{
|
|
col.ID: 1,
|
|
})); !errors.Is(err, context.Canceled) {
|
|
t.Fatalf("context cancellation lost: %v", err)
|
|
}
|
|
}
|