34 lines
1.2 KiB
Go
34 lines
1.2 KiB
Go
package dberror
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/go-sql-driver/mysql"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// TestIsDuplicateKeyUsesTypedCauses 验证包装不影响驱动识别,伪造文本和其他约束不进入幂等成功。
|
|
func TestIsDuplicateKeyUsesTypedCauses(t *testing.T) {
|
|
for _, item := range []struct {
|
|
name string
|
|
err error
|
|
duplicate bool
|
|
}{
|
|
{name: "nil"},
|
|
{name: "mysql", err: &mysql.MySQLError{Number: 1062, Message: "localized and renamed index"}, duplicate: true},
|
|
{name: "wrapped mysql", err: fmt.Errorf("write event: %w", &mysql.MySQLError{Number: 1062}), duplicate: true},
|
|
{name: "gorm translated", err: fmt.Errorf("write event: %w", gorm.ErrDuplicatedKey), duplicate: true},
|
|
{name: "plain text", err: errors.New("1062 Duplicate entry uk_active_user")},
|
|
{name: "wrong mysql number", err: &mysql.MySQLError{Number: 1054, Message: "1062 Duplicate entry"}},
|
|
{name: "sqlite plain text", err: errors.New("UNIQUE constraint failed: table.id")},
|
|
} {
|
|
t.Run(item.name, func(t *testing.T) {
|
|
if got := IsDuplicateKey(item.err); got != item.duplicate {
|
|
t.Fatalf("IsDuplicateKey(%T)=%v want=%v", item.err, got, item.duplicate)
|
|
}
|
|
})
|
|
}
|
|
}
|