2023-03-06 09:03:31 +03:00
|
|
|
package tests_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"gorm.io/gorm/utils/tests"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDialectorWithErrorTranslatorSupport(t *testing.T) {
|
2023-03-24 05:07:05 +03:00
|
|
|
// it shouldn't translate error when the TranslateError flag is false
|
2023-03-06 09:03:31 +03:00
|
|
|
translatedErr := errors.New("translated error")
|
2023-03-24 05:07:05 +03:00
|
|
|
untranslatedErr := errors.New("some random error")
|
2023-03-06 09:03:31 +03:00
|
|
|
db, _ := gorm.Open(tests.DummyDialector{TranslatedErr: translatedErr})
|
|
|
|
|
2023-03-24 05:07:05 +03:00
|
|
|
err := db.AddError(untranslatedErr)
|
|
|
|
if errors.Is(err, translatedErr) {
|
|
|
|
t.Fatalf("expected err: %v got err: %v", translatedErr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// it should translate error when the TranslateError flag is true
|
|
|
|
db, _ = gorm.Open(tests.DummyDialector{TranslatedErr: translatedErr}, &gorm.Config{TranslateError: true})
|
|
|
|
|
|
|
|
err = db.AddError(untranslatedErr)
|
2023-03-06 09:03:31 +03:00
|
|
|
if !errors.Is(err, translatedErr) {
|
|
|
|
t.Fatalf("expected err: %v got err: %v", translatedErr, err)
|
|
|
|
}
|
|
|
|
}
|