默认单数表名

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) {
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 {