Files
ginframework/database/query/behavior_test.go
T
2026-11-28 03:10:18 -08:00

48 lines
1.3 KiB
Go

package query
import (
"context"
"errors"
"testing"
"git.zhangdeman.cn/zhangdeman/ginframework/fault"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func TestCountAndClassifiedCauses(t *testing.T) {
db, err := gorm.Open(sqlite.Open("file:"+t.Name()+"?mode=memory&cache=shared"), &gorm.Config{})
if err != nil {
t.Fatal(err)
}
pool, err := db.DB()
if err != nil {
t.Fatal(err)
}
defer pool.Close()
if err = db.AutoMigrate(&migrationRow{}); err != nil {
t.Fatal(err)
}
dao := &BaseDao[migrationRow, migrationColumns]{}
if err = dao.Create(db, &migrationRow{ID: 1, TenantID: 7}); err != nil {
t.Fatal(err)
}
if n, err := dao.Count(db, WithWhere(map[string]int64{"tenant_id": 7})); err != nil || n != 1 {
t.Fatalf("count=%d err=%v", n, err)
}
var (
row migrationRow
classified *fault.Error
)
err = dao.Detail(db, &row, WithWhere(map[string]int64{"id": 999}))
if !errors.Is(err, gorm.ErrRecordNotFound) || !errors.As(err, &classified) || classified.Code() != CodeDetail {
t.Fatalf("not found cause or scene lost: %v", err)
}
ctx, cancel := context.WithCancel(context.Background())
cancel()
err = dao.List(db.WithContext(ctx), &[]migrationRow{})
if !errors.Is(err, context.Canceled) || !errors.As(err, &classified) || classified.Code() != CodeList {
t.Fatalf("cancel cause or scene lost: %v", err)
}
}