Skip to content

Commit

Permalink
test: coverage for foreign key violation err (#6403)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
saeidee and Saeid Saeidee authored Jul 12, 2023
1 parent 2066138 commit c10f807
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 9 deletions.
51 changes: 51 additions & 0 deletions tests/error_translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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 changes: 6 additions & 8 deletions tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 => ../
2 changes: 1 addition & 1 deletion tests/tests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit c10f807

Please sign in to comment.