-
-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ErrForeignKeyViolated implemented
- Loading branch information
1 parent
91dcda2
commit 3f88908
Showing
5 changed files
with
67 additions
and
10 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,58 @@ | ||
package mysql | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"gorm.io/gorm" | ||
|
||
"github.com/go-sql-driver/mysql" | ||
) | ||
|
||
func TestDialector_Translate(t *testing.T) { | ||
normalErr := errors.New("normal error") | ||
|
||
type fields struct { | ||
Config *Config | ||
} | ||
type args struct { | ||
err error | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
want error | ||
}{ | ||
{ | ||
name: "it should translate error to ErrDuplicatedKey when the error number is 1062", | ||
args: args{err: &mysql.MySQLError{Number: uint16(1062)}}, | ||
want: gorm.ErrDuplicatedKey, | ||
}, | ||
{ | ||
name: "it should translate error to ErrForeignKeyViolated when the error number is 1452", | ||
args: args{err: &mysql.MySQLError{Number: uint16(1452)}}, | ||
want: gorm.ErrForeignKeyViolated, | ||
}, | ||
{ | ||
name: "it should not translate the error when the error number is not registered in translated error codes", | ||
args: args{err: &mysql.MySQLError{Number: uint16(8888)}}, | ||
want: &mysql.MySQLError{Number: uint16(8888)}, | ||
}, | ||
{ | ||
name: "it should not translate the error when the error is not a mysql error", | ||
args: args{err: normalErr}, | ||
want: normalErr, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
dialector := Dialector{ | ||
Config: tt.fields.Config, | ||
} | ||
if err := dialector.Translate(tt.args.err); !errors.Is(err, tt.want) { | ||
t.Errorf("Translate() got error = %v, want error %v", err, tt.want) | ||
} | ||
}) | ||
} | ||
} |
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