test: coverage for duplicated key err (#6389)

* test: ErrDuplicatedKey coverage added

* test: updated sqlserver version

* test: removed sqlserver

* test: support added for sqlserver

---------

Co-authored-by: Saeid Saeidee <s.saeidee@sensysgatso.com>
This commit is contained in:
Saeid 2023-06-10 15:05:19 +02:00 committed by GitHub
parent 7dd702d379
commit c2d571cbc8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 44 additions and 14 deletions

View File

@ -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)
}

View File

@ -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)
}
}

View File

@ -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 => ../

View File

@ -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()
@ -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()

View File

@ -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 {
@ -49,7 +49,7 @@ 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":
@ -57,7 +57,7 @@ func OpenTestConnection() (db *gorm.DB, err error) {
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 == "" {
@ -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
@ -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 {

View File

@ -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)
}