-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_code_test.go
More file actions
188 lines (180 loc) · 4.43 KB
/
Copy patherror_code_test.go
File metadata and controls
188 lines (180 loc) · 4.43 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package dberror
import (
"errors"
"testing"
"github.com/go-sql-driver/mysql"
"github.com/jackc/pgx/v5/pgconn"
"github.com/mattn/go-sqlite3"
"github.com/stretchr/testify/assert"
)
func TestIsDuplicateEntry(t *testing.T) {
tests := []struct {
name string
err error
want bool
comment string
}{
{
name: "MySQL_DuplicateEntry_1062",
err: &mysql.MySQLError{Number: errorCodeMySQLDuplicateEntry},
want: true,
comment: "MySQL duplicate entry error (1062)",
},
{
name: "MySQL_DuplicateEntryWithKeyName_1586",
err: &mysql.MySQLError{Number: errorCodeMySQLDuplicateEntryWithKeyName},
want: true,
comment: "MySQL duplicate entry with key name error (1586)",
},
{
name: "MySQL_WrongCode_1045",
err: &mysql.MySQLError{Number: 1045},
want: false,
comment: "MySQL wrong error code (1045)",
},
{
name: "PostgreSQL_UniqueViolation_23505",
err: &pgconn.PgError{Code: errorCodePostgresUniqueViolation},
want: true,
comment: "PostgreSQL unique violation error (23505)",
},
{
name: "PostgreSQL_WrongCode_23502",
err: &pgconn.PgError{Code: "23502"},
want: false,
comment: "PostgreSQL wrong error code (23502)",
},
{
name: "GenericError",
err: errors.New("some error"),
want: false,
comment: "Non-database error",
},
{
name: "NilError",
err: nil,
want: false,
comment: "Nil error",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := IsDuplicateEntry(tt.err)
assert.Equal(t, tt.want, got, tt.comment)
})
}
}
func TestIsForeignKeyViolation(t *testing.T) {
tests := []struct {
name string
err error
want bool
comment string
}{
{
name: "MySQL_ForeignKeyViolation_1452",
err: &mysql.MySQLError{Number: errorCodeMySQLForeignKeyViolation},
want: true,
comment: "MySQL foreign key violation error (1452)",
},
{
name: "MySQL_WrongCode_1062",
err: &mysql.MySQLError{Number: 1062},
want: false,
comment: "MySQL wrong error code (1062)",
},
{
name: "PostgreSQL_ForeignKeyViolation_23503",
err: &pgconn.PgError{Code: errorCodePostgresForeignKeyViolation},
want: true,
comment: "PostgreSQL foreign key violation error (23503)",
},
{
name: "PostgreSQL_WrongCode_23505",
err: &pgconn.PgError{Code: "23505"},
want: false,
comment: "PostgreSQL wrong error code (23505)",
},
{
name: "SQLite_NotSupported",
err: &sqlite3.Error{Code: sqlite3.ErrNo(19), ExtendedCode: 2067},
want: false,
comment: "SQLite does not support foreign key violation detection",
},
{
name: "GenericError",
err: errors.New("some error"),
want: false,
comment: "Non-database error",
},
{
name: "NilError",
err: nil,
want: false,
comment: "Nil error",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := IsForeignKeyViolation(tt.err)
assert.Equal(t, tt.want, got, tt.comment)
})
}
}
func TestIsNotNullViolation(t *testing.T) {
tests := []struct {
name string
err error
want bool
comment string
}{
{
name: "MySQL_NotNullViolation_1048",
err: &mysql.MySQLError{Number: errorCodeMySQLNotNullViolation},
want: true,
comment: "MySQL not null violation error (1048)",
},
{
name: "MySQL_WrongCode_1062",
err: &mysql.MySQLError{Number: 1062},
want: false,
comment: "MySQL wrong error code (1062)",
},
{
name: "PostgreSQL_NotNullViolation_23502",
err: &pgconn.PgError{Code: errorCodePostgresNotNullViolation},
want: true,
comment: "PostgreSQL not null violation error (23502)",
},
{
name: "PostgreSQL_WrongCode_23505",
err: &pgconn.PgError{Code: "23505"},
want: false,
comment: "PostgreSQL wrong error code (23505)",
},
{
name: "SQLite_NotSupported",
err: &sqlite3.Error{Code: sqlite3.ErrNo(19), ExtendedCode: 2067},
want: false,
comment: "SQLite does not support not null violation detection",
},
{
name: "GenericError",
err: errors.New("some error"),
want: false,
comment: "Non-database error",
},
{
name: "NilError",
err: nil,
want: false,
comment: "Nil error",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := IsNotNullViolation(tt.err)
assert.Equal(t, tt.want, got, tt.comment)
})
}
}