MCPcopy
hub / github.com/go-gorm/gorm / TestSupportedDialectorWithErrDuplicatedKey

Function TestSupportedDialectorWithErrDuplicatedKey

tests/error_translator_test.go:31–62  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

29}
30
31func TestSupportedDialectorWithErrDuplicatedKey(t *testing.T) {
32 type City struct {
33 gorm.Model
34 Name string `gorm:"unique"`
35 }
36
37 db, err := OpenTestConnection(&gorm.Config{TranslateError: true})
38 if err != nil {
39 t.Fatalf("failed to connect database, got error %v", err)
40 }
41
42 dialectors := map[string]bool{"sqlite": true, "postgres": true, "gaussdb": true, "mysql": true, "sqlserver": true}
43 if supported, found := dialectors[db.Dialector.Name()]; !(found && supported) {
44 return
45 }
46
47 DB.Migrator().DropTable(&City{})
48
49 if err = db.AutoMigrate(&City{}); err != nil {
50 t.Fatalf("failed to migrate cities table, got error: %v", err)
51 }
52
53 err = db.Create(&City{Name: "Kabul"}).Error
54 if err != nil {
55 t.Fatalf("failed to create record: %v", err)
56 }
57
58 err = db.Create(&City{Name: "Kabul"}).Error
59 if !errors.Is(err, gorm.ErrDuplicatedKey) {
60 t.Fatalf("expected err: %v got err: %v", gorm.ErrDuplicatedKey, err)
61 }
62}
63
64func TestSupportedDialectorWithErrForeignKeyViolated(t *testing.T) {
65 tidbSkip(t, "not support the foreign key feature")

Callers

nothing calls this directly

Calls 6

OpenTestConnectionFunction · 0.85
NameMethod · 0.65
DropTableMethod · 0.65
MigratorMethod · 0.65
AutoMigrateMethod · 0.65
CreateMethod · 0.65

Tested by

no test coverage detected