//go:build cgo package dberror import ( "fmt" "testing" "github.com/mattn/go-sqlite3" ) // TestIsDuplicateKeySQLite 验证CGO驱动约束类型,保留主键/唯一键幂等语义,不吞掉其他约束错误。 func TestIsDuplicateKeySQLite(t *testing.T) { for _, item := range []struct { name string err error duplicate bool }{ {name: "unique", err: sqlite3.Error{Code: sqlite3.ErrConstraint, ExtendedCode: sqlite3.ErrConstraintUnique}, duplicate: true}, {name: "wrapped primary", err: fmt.Errorf("write event: %w", sqlite3.Error{Code: sqlite3.ErrConstraint, ExtendedCode: sqlite3.ErrConstraintPrimaryKey}), duplicate: true}, {name: "foreign key", err: sqlite3.Error{Code: sqlite3.ErrConstraint, ExtendedCode: sqlite3.ErrConstraintForeignKey}}, {name: "not null", err: sqlite3.Error{Code: sqlite3.ErrConstraint, ExtendedCode: sqlite3.ErrConstraintNotNull}}, } { t.Run(item.name, func(t *testing.T) { if got := IsDuplicateKey(item.err); got != item.duplicate { t.Fatalf("duplicate=%v want=%v", got, item.duplicate) } }) } }