抽取 Gin、配置、日志、数据库、Redis 与出站 HTTP 基础能力。 新增统一接入指南,将能力清单和相对链接检查纳入 make verify。 对齐远端 module 路径,修正 Redis 读写超时特殊值映射。 验证:make verify、make race-check 均通过。
This commit is contained in:
commit
b4c3ac622c
55 files changed
+5862
No files matched your search
@@ -0,0 +1,64 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"git.zhangdeman.cn/github/ginframework/fault"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type rejectedPlugin struct{}
|
||||
|
||||
func (rejectedPlugin) Name() string { return "reject" }
|
||||
func (rejectedPlugin) Initialize(*gorm.DB) error { return context.Canceled }
|
||||
|
||||
func TestIndependentSessionsAndPluginFailure(t *testing.T) {
|
||||
connection, err := Open(context.Background(), Config{}, Options{Dialector: sqlite.Open(":memory:")})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() {
|
||||
if err := connection.Close(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}()
|
||||
a, b := connection.Session(context.Background()), connection.Session(context.Background())
|
||||
poolA, _ := a.DB()
|
||||
poolB, _ := b.DB()
|
||||
if a == b || poolA != poolB {
|
||||
t.Fatal("sessions do not share one pool")
|
||||
}
|
||||
if err := a.Exec("CREATE TABLE example (id INTEGER PRIMARY KEY)").Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rollback := errors.New("rollback expected")
|
||||
if err := a.Transaction(func(tx *gorm.DB) error {
|
||||
if e := tx.Exec("INSERT INTO example(id) VALUES (1)").Error; e != nil {
|
||||
return e
|
||||
}
|
||||
return rollback
|
||||
}); !errors.Is(err, rollback) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var (
|
||||
count int64
|
||||
)
|
||||
if err := b.Table("example").Count(&count).Error; err != nil || count != 0 {
|
||||
t.Fatalf("transaction changed: %d %v", count, err)
|
||||
}
|
||||
failed, err := Open(context.Background(), Config{}, Options{Dialector: sqlite.Open(":memory:"), Plugins: []gorm.Plugin{rejectedPlugin{}}})
|
||||
if failed != nil || !errors.Is(err, context.Canceled) || !fault.Internal(err) {
|
||||
t.Fatalf("plugin failure lost: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigurationDoesNotOpenOnInvalidPool(t *testing.T) {
|
||||
negative := -1
|
||||
connection, err := Open(context.Background(), Config{MaxIdle: &negative}, Options{})
|
||||
if connection != nil || !fault.Internal(err) {
|
||||
t.Fatal("invalid configuration accepted")
|
||||
}
|
||||
}
|
||||