mirror of https://github.com/go-gorm/gorm.git
test: coverage for foreign key violation err (#6403)
* test: coverage for foreign key violation err * test: enabled foreign keys constraint for sqlite * test: enabled mysql& mssql for ErrForeignKeyViolate * test: disabled mysql & updated sqlserver driver version * test: skipped tidb --------- Co-authored-by: Saeid Saeidee <s.saeidee@sensysgatso.com>
This commit is contained in:
parent
2066138684
commit
c10f807d3c
|
@ -44,6 +44,8 @@ func TestSupportedDialectorWithErrDuplicatedKey(t *testing.T) {
|
|||
return
|
||||
}
|
||||
|
||||
DB.Migrator().DropTable(&City{})
|
||||
|
||||
if err = db.AutoMigrate(&City{}); err != nil {
|
||||
t.Fatalf("failed to migrate cities table, got error: %v", err)
|
||||
}
|
||||
|
@ -58,3 +60,52 @@ func TestSupportedDialectorWithErrDuplicatedKey(t *testing.T) {
|
|||
t.Fatalf("expected err: %v got err: %v", gorm.ErrDuplicatedKey, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSupportedDialectorWithErrForeignKeyViolated(t *testing.T) {
|
||||
tidbSkip(t, "not support the foreign key feature")
|
||||
|
||||
type City struct {
|
||||
gorm.Model
|
||||
Name string `gorm:"unique"`
|
||||
}
|
||||
|
||||
type Museum struct {
|
||||
gorm.Model
|
||||
Name string `gorm:"unique"`
|
||||
CityID uint
|
||||
City City `gorm:"Constraint:OnUpdate:CASCADE,OnDelete:CASCADE;FOREIGNKEY:CityID;References:ID"`
|
||||
}
|
||||
|
||||
db, err := OpenTestConnection(&gorm.Config{TranslateError: true})
|
||||
if err != nil {
|
||||
t.Fatalf("failed to connect database, got error %v", err)
|
||||
}
|
||||
|
||||
dialectors := map[string]bool{"sqlite": true, "postgres": true, "mysql": true, "sqlserver": true}
|
||||
if supported, found := dialectors[db.Dialector.Name()]; !(found && supported) {
|
||||
return
|
||||
}
|
||||
|
||||
DB.Migrator().DropTable(&City{}, &Museum{})
|
||||
|
||||
if err = db.AutoMigrate(&City{}, &Museum{}); err != nil {
|
||||
t.Fatalf("failed to migrate countries & cities tables, got error: %v", err)
|
||||
}
|
||||
|
||||
city := City{Name: "Amsterdam"}
|
||||
|
||||
err = db.Create(&city).Error
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create city: %v", err)
|
||||
}
|
||||
|
||||
err = db.Create(&Museum{Name: "Eye Filmmuseum", CityID: city.ID}).Error
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create museum: %v", err)
|
||||
}
|
||||
|
||||
err = db.Create(&Museum{Name: "Dungeon", CityID: 123}).Error
|
||||
if !errors.Is(err, gorm.ErrForeignKeyViolated) {
|
||||
t.Fatalf("expected err: %v got err: %v", gorm.ErrForeignKeyViolated, err)
|
||||
}
|
||||
}
|
||||
|
|
14
tests/go.mod
14
tests/go.mod
|
@ -4,15 +4,13 @@ go 1.16
|
|||
|
||||
require (
|
||||
github.com/google/uuid v1.3.0
|
||||
github.com/jackc/pgx/v5 v5.3.1 // indirect
|
||||
github.com/jinzhu/now v1.1.5
|
||||
github.com/lib/pq v1.10.8
|
||||
github.com/mattn/go-sqlite3 v1.14.16 // indirect
|
||||
gorm.io/driver/mysql v1.5.0
|
||||
gorm.io/driver/postgres v1.5.0
|
||||
gorm.io/driver/sqlite v1.5.0
|
||||
gorm.io/driver/sqlserver v1.5.1
|
||||
gorm.io/gorm v1.25.1
|
||||
github.com/lib/pq v1.10.9
|
||||
gorm.io/driver/mysql v1.5.2-0.20230612053416-48b6526a21f0
|
||||
gorm.io/driver/postgres v1.5.3-0.20230607070428-18bc84b75196
|
||||
gorm.io/driver/sqlite v1.5.2
|
||||
gorm.io/driver/sqlserver v1.5.2-0.20230613072041-6e2cde390b0a
|
||||
gorm.io/gorm v1.25.2-0.20230610234218-206613868439
|
||||
)
|
||||
|
||||
replace gorm.io/gorm => ../
|
||||
|
|
|
@ -89,7 +89,7 @@ func OpenTestConnection(cfg *gorm.Config) (db *gorm.DB, err error) {
|
|||
db, err = gorm.Open(mysql.Open(dbDSN), cfg)
|
||||
default:
|
||||
log.Println("testing sqlite3...")
|
||||
db, err = gorm.Open(sqlite.Open(filepath.Join(os.TempDir(), "gorm.db")), cfg)
|
||||
db, err = gorm.Open(sqlite.Open(filepath.Join(os.TempDir(), "gorm.db?_foreign_keys=on")), cfg)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in New Issue