Skip to content

Commit

Permalink
test: coverage for duplicated key err (go-gorm#6389)
Browse files Browse the repository at this point in the history
* test: ErrDuplicatedKey coverage added

* test: updated sqlserver version

* test: removed sqlserver

* test: support added for sqlserver

---------

Co-authored-by: Saeid Saeidee <[email protected]>
  • Loading branch information
saeidee and Saeid Saeidee authored Jun 10, 2023
1 parent 7dd702d commit c2d571c
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 14 deletions.
2 changes: 1 addition & 1 deletion tests/associations_many2many_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ func TestDuplicateMany2ManyAssociation(t *testing.T) {
}

func TestConcurrentMany2ManyAssociation(t *testing.T) {
db, err := OpenTestConnection()
db, err := OpenTestConnection(&gorm.Config{})
if err != nil {
t.Fatalf("open test connection failed, err: %+v", err)
}
Expand Down
31 changes: 31 additions & 0 deletions tests/error_translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,34 @@ func TestDialectorWithErrorTranslatorSupport(t *testing.T) {
t.Fatalf("expected err: %v got err: %v", translatedErr, err)
}
}

func TestSupportedDialectorWithErrDuplicatedKey(t *testing.T) {
type City struct {
gorm.Model
Name string `gorm:"unique"`
}

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
}

if err = db.AutoMigrate(&City{}); err != nil {
t.Fatalf("failed to migrate cities table, got error: %v", err)
}

err = db.Create(&City{Name: "Kabul"}).Error
if err != nil {
t.Fatalf("failed to create record: %v", err)
}

err = db.Create(&City{Name: "Kabul"}).Error
if !errors.Is(err, gorm.ErrDuplicatedKey) {
t.Fatalf("expected err: %v got err: %v", gorm.ErrDuplicatedKey, err)
}
}
5 changes: 2 additions & 3 deletions tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ require (
github.com/jinzhu/now v1.1.5
github.com/lib/pq v1.10.8
github.com/mattn/go-sqlite3 v1.14.16 // indirect
golang.org/x/crypto v0.8.0 // 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.4.3
gorm.io/gorm v1.25.0
gorm.io/driver/sqlserver v1.5.1
gorm.io/gorm v1.25.1
)

replace gorm.io/gorm => ../
4 changes: 2 additions & 2 deletions tests/prepared_stmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func TestPreparedStmtFromTransaction(t *testing.T) {
}

func TestPreparedStmtDeadlock(t *testing.T) {
tx, err := OpenTestConnection()
tx, err := OpenTestConnection(&gorm.Config{})
AssertEqual(t, err, nil)

sqlDB, _ := tx.DB()
Expand Down Expand Up @@ -127,7 +127,7 @@ func TestPreparedStmtDeadlock(t *testing.T) {
}

func TestPreparedStmtError(t *testing.T) {
tx, err := OpenTestConnection()
tx, err := OpenTestConnection(&gorm.Config{})
AssertEqual(t, err, nil)

sqlDB, _ := tx.DB()
Expand Down
14 changes: 7 additions & 7 deletions tests/tests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var (

func init() {
var err error
if DB, err = OpenTestConnection(); err != nil {
if DB, err = OpenTestConnection(&gorm.Config{}); err != nil {
log.Printf("failed to connect database, got error %v", err)
os.Exit(1)
} else {
Expand All @@ -49,15 +49,15 @@ func init() {
}
}

func OpenTestConnection() (db *gorm.DB, err error) {
func OpenTestConnection(cfg *gorm.Config) (db *gorm.DB, err error) {
dbDSN := os.Getenv("GORM_DSN")
switch os.Getenv("GORM_DIALECT") {
case "mysql":
log.Println("testing mysql...")
if dbDSN == "" {
dbDSN = mysqlDSN
}
db, err = gorm.Open(mysql.Open(dbDSN), &gorm.Config{})
db, err = gorm.Open(mysql.Open(dbDSN), cfg)
case "postgres":
log.Println("testing postgres...")
if dbDSN == "" {
Expand All @@ -66,7 +66,7 @@ func OpenTestConnection() (db *gorm.DB, err error) {
db, err = gorm.Open(postgres.New(postgres.Config{
DSN: dbDSN,
PreferSimpleProtocol: true,
}), &gorm.Config{})
}), cfg)
case "sqlserver":
// go install github.com/microsoft/go-sqlcmd/cmd/sqlcmd@latest
// SQLCMDPASSWORD=LoremIpsum86 sqlcmd -U sa -S localhost:9930
Expand All @@ -80,16 +80,16 @@ func OpenTestConnection() (db *gorm.DB, err error) {
if dbDSN == "" {
dbDSN = sqlserverDSN
}
db, err = gorm.Open(sqlserver.Open(dbDSN), &gorm.Config{})
db, err = gorm.Open(sqlserver.Open(dbDSN), cfg)
case "tidb":
log.Println("testing tidb...")
if dbDSN == "" {
dbDSN = tidbDSN
}
db, err = gorm.Open(mysql.Open(dbDSN), &gorm.Config{})
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")), &gorm.Config{})
db, err = gorm.Open(sqlite.Open(filepath.Join(os.TempDir(), "gorm.db")), cfg)
}

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion tests/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ func TestDisabledNestedTransaction(t *testing.T) {
}

func TestTransactionOnClosedConn(t *testing.T) {
DB, err := OpenTestConnection()
DB, err := OpenTestConnection(&gorm.Config{})
if err != nil {
t.Fatalf("failed to connect database, got error %v", err)
}
Expand Down

0 comments on commit c2d571c

Please sign in to comment.