1 parent
bedcf4dd83
commit
d2a9033695
23 files changed
+1544
-6
No files matched your search
@@ -0,0 +1,47 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||