1 Commits

Author SHA1 Message Date
yun 6525b3efa6 默认单数表名 2026-06-07 15:20:10 +08:00
2 changed files with 29 additions and 3 deletions
+21 -1
View File
@@ -15,12 +15,14 @@ type testModel struct {
}
func TestNewWithCustomDialector(t *testing.T) {
sqlDB, _, err := sqlmock.New()
sqlDB, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("failed to create sqlmock: %v", err)
}
defer sqlDB.Close()
mock.ExpectQuery("SELECT VERSION()").WillReturnRows(sqlmock.NewRows([]string{"VERSION()"}).AddRow("5.7.0"))
client, err := NewDB(
WithDialector(drivermysql.New(drivermysql.Config{Conn: sqlDB})),
WithLogger(logger.Default.LogMode(logger.Silent)),
@@ -34,6 +36,9 @@ func TestNewWithCustomDialector(t *testing.T) {
if client.DB() == nil {
t.Fatal("expected non-nil DB")
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatalf("sqlmock expectations were not met: %v", err)
}
}
func TestBuildDSNWithExpandedConfig(t *testing.T) {
@@ -54,6 +59,21 @@ func TestBuildDSNWithExpandedConfig(t *testing.T) {
}
}
func TestDefaultConfigUsesSingularTable(t *testing.T) {
cfg := defaultConfig()
if !cfg.NamingStrategy.SingularTable {
t.Fatal("expected default naming strategy to use singular table names")
}
}
func TestWithSingularTableOption(t *testing.T) {
cfg := defaultConfig()
WithSingularTable(false)(cfg)
if cfg.NamingStrategy.SingularTable {
t.Fatal("expected naming strategy singular table to be disabled")
}
}
func TestNewWithoutDSNOrDialector(t *testing.T) {
_, err := NewDB()
if err == nil {
+8 -2
View File
@@ -43,13 +43,12 @@ type config struct {
}
type DSN struct {
}
func defaultConfig() *config {
return &config{
Logger: logger.Default,
NamingStrategy: schema.NamingStrategy{},
NamingStrategy: schema.NamingStrategy{SingularTable: true},
MaxIdleConns: 10,
MaxOpenConns: 100,
}
@@ -308,6 +307,13 @@ func WithNamingStrategy(strategy schema.NamingStrategy) Option {
}
}
// WithSingularTable enables or disables singular table names for gorm models.
func WithSingularTable(singular bool) Option {
return func(cfg *config) {
cfg.NamingStrategy.SingularTable = singular
}
}
// WithConnectionPool configures database connection pooling.
func WithConnectionPool(maxIdleConns, maxOpenConns int, maxIdleTime, maxLifetime time.Duration) Option {
return func(cfg *config) {