-
-
Notifications
You must be signed in to change notification settings - Fork 90
/
error_translator_test.go
63 lines (58 loc) · 1.57 KB
/
error_translator_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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 1451",
args: args{err: &mysql.MySQLError{Number: uint16(1451)}},
want: gorm.ErrForeignKeyViolated,
},
{
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)
}
})
}
}