(t *testing.T)
| 29 | } |
| 30 | |
| 31 | func 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 | |
| 64 | func TestSupportedDialectorWithErrForeignKeyViolated(t *testing.T) { |
| 65 | tidbSkip(t, "not support the foreign key feature") |
nothing calls this directly
no test coverage detected