forked from go-gorm/sqlite
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Error translator for Dialector
- Loading branch information
Showing
3 changed files
with
64 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package sqlite | ||
|
||
import ( | ||
"testing" | ||
|
||
"gorm.io/gorm" | ||
"gorm.io/gorm/logger" | ||
) | ||
|
||
func TestErrorTranslator(t *testing.T) { | ||
// This is the DSN of the in-memory SQLite database for these tests. | ||
const InMemoryDSN = "file:testdatabase?mode=memory&cache=shared" | ||
|
||
// This is the example object for testing the unique constraint error | ||
type Article struct { | ||
ArticleNumber string `gorm:"unique"` | ||
} | ||
|
||
db, err := gorm.Open(&Dialector{DSN: InMemoryDSN}, &gorm.Config{ | ||
Logger: logger.Default.LogMode(logger.Silent), | ||
TranslateError: true}) | ||
|
||
if err != nil { | ||
t.Errorf("Expected Open to succeed; got error: %v", err) | ||
} | ||
if db == nil { | ||
t.Errorf("Expected db to be non-nil.") | ||
} | ||
|
||
err = db.AutoMigrate(&Article{}) | ||
if err != nil { | ||
t.Errorf("Expected to migrate database models to succeed: %v", err) | ||
} | ||
|
||
err = db.Create(&Article{ArticleNumber: "A00000XX"}).Error | ||
if err != nil { | ||
t.Errorf("Expected first create to succeed: %v", err) | ||
} | ||
|
||
err = db.Create(&Article{ArticleNumber: "A00000XX"}).Error | ||
if err == nil { | ||
t.Errorf("Expected second create to fail.") | ||
} | ||
|
||
if err != gorm.ErrDuplicatedKey { | ||
t.Errorf("Expected error from second create to be gorm.ErrDuplicatedKey: %v", err) | ||
} | ||
} |