默认单数表名

This commit is contained in:
Yun
2026-06-07 15:20:10 +08:00
parent 83c727372a
commit 6525b3efa6
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) { func TestNewWithCustomDialector(t *testing.T) {
sqlDB, _, err := sqlmock.New() sqlDB, mock, err := sqlmock.New()
if err != nil { if err != nil {
t.Fatalf("failed to create sqlmock: %v", err) t.Fatalf("failed to create sqlmock: %v", err)
} }
defer sqlDB.Close() defer sqlDB.Close()
mock.ExpectQuery("SELECT VERSION()").WillReturnRows(sqlmock.NewRows([]string{"VERSION()"}).AddRow("5.7.0"))
client, err := NewDB( client, err := NewDB(
WithDialector(drivermysql.New(drivermysql.Config{Conn: sqlDB})), WithDialector(drivermysql.New(drivermysql.Config{Conn: sqlDB})),
WithLogger(logger.Default.LogMode(logger.Silent)), WithLogger(logger.Default.LogMode(logger.Silent)),
@@ -34,6 +36,9 @@ func TestNewWithCustomDialector(t *testing.T) {
if client.DB() == nil { if client.DB() == nil {
t.Fatal("expected non-nil DB") 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) { 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) { func TestNewWithoutDSNOrDialector(t *testing.T) {
_, err := NewDB() _, err := NewDB()
if err == nil { if err == nil {
+8 -2
View File
@@ -43,13 +43,12 @@ type config struct {
} }
type DSN struct { type DSN struct {
} }
func defaultConfig() *config { func defaultConfig() *config {
return &config{ return &config{
Logger: logger.Default, Logger: logger.Default,
NamingStrategy: schema.NamingStrategy{}, NamingStrategy: schema.NamingStrategy{SingularTable: true},
MaxIdleConns: 10, MaxIdleConns: 10,
MaxOpenConns: 100, 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. // WithConnectionPool configures database connection pooling.
func WithConnectionPool(maxIdleConns, maxOpenConns int, maxIdleTime, maxLifetime time.Duration) Option { func WithConnectionPool(maxIdleConns, maxOpenConns int, maxIdleTime, maxLifetime time.Duration) Option {
return func(cfg *config) { return func(cfg *config) {