-
-
Notifications
You must be signed in to change notification settings - Fork 123
/
error_translator_test.go
55 lines (52 loc) · 1.3 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
package postgres
import (
"errors"
"testing"
"github.com/jackc/pgx/v5/pgconn"
"gorm.io/gorm"
)
func TestDialector_Translate(t *testing.T) {
type fields struct {
Config *Config
}
type args struct {
err error
}
tests := []struct {
name string
fields fields
args args
want error
}{
{
name: "it should return ErrDuplicatedKey error if the status code is 23505",
args: args{err: &pgconn.PgError{Code: "23505"}},
want: gorm.ErrDuplicatedKey,
},
{
name: "it should return ErrForeignKeyViolated error if the status code is 23503",
args: args{err: &pgconn.PgError{Code: "23503"}},
want: gorm.ErrForeignKeyViolated,
},
{
name: "it should return gorm.ErrInvalidField error if the status code is 42703",
args: args{err: &pgconn.PgError{Code: "42703"}},
want: gorm.ErrInvalidField,
},
{
name: "it should return gorm.ErrCheckConstraintViolated error if the status code is 23514",
args: args{err: &pgconn.PgError{Code: "23514"}},
want: gorm.ErrCheckConstraintViolated,
},
}
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() expected error = %v, got error %v", err, tt.want)
}
})
}
}