forked from Shyp/go-dberror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror_test.go
258 lines (234 loc) · 7.25 KB
/
error_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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package dberror
import (
"database/sql"
"os"
"sync"
"testing"
"github.com/letsencrypt/boulder/test"
"github.com/lib/pq"
)
const uuid = "3c7d2b4a-3fc8-4782-a518-4ce9efef51e7"
const uuid2 = "91f47e99-d616-4d8c-9c02-cbd13bceac60"
const email = "[email protected]"
const email2 = "[email protected]"
var db *sql.DB
var mu sync.Mutex
func setUp(t *testing.T) {
mu.Lock()
if db != nil {
mu.Unlock()
return
}
ci := os.Getenv("CI")
var err error
if ci == "" {
db, err = sql.Open("postgres", "postgres://localhost/dberror?sslmode=disable")
} else {
db, err = sql.Open("postgres", "postgres://postgres@localhost/dberror?sslmode=disable")
}
mu.Unlock()
if err != nil {
t.Fatal(err)
}
}
func tearDown(t *testing.T) {
if db != nil {
_, err := db.Exec("DELETE FROM accounts CASCADE; DELETE FROM payments CASCADE;")
test.AssertNotError(t, err, "")
}
}
func TestNilError(t *testing.T) {
test.AssertEquals(t, GetError(nil), nil)
}
func TestNotNull(t *testing.T) {
t.Parallel()
setUp(t)
_, err := db.Exec("INSERT INTO accounts (id) VALUES (null)")
dberr := GetError(err)
switch e := dberr.(type) {
case *Error:
test.AssertEquals(t, e.Error(), "No id was provided. Please provide a id")
test.AssertEquals(t, e.Column, "id")
test.AssertEquals(t, e.Table, "accounts")
default:
t.Fail()
}
}
func TestDefaultConstraint(t *testing.T) {
// this test needs to go before the Register() below... not great, add an
// unregister or clear out the map or something
setUp(t)
_, err := db.Exec("INSERT INTO accounts (id, email, balance) VALUES ($1, $2, -1)", uuid, email)
dberr := GetError(err)
switch e := dberr.(type) {
case *Error:
test.AssertEquals(t, e.Error(), "new row for relation \"accounts\" violates check constraint \"accounts_balance_check\"")
test.AssertEquals(t, e.Table, "accounts")
default:
t.Fail()
}
}
func TestCustomConstraint(t *testing.T) {
setUp(t)
defer func() {
constraintMap = map[string]*Constraint{}
}()
constraint := &Constraint{
Name: "accounts_balance_check",
GetError: func(e *pq.Error) *Error {
return &Error{
Message: "Cannot write a negative balance",
Severity: e.Severity,
Table: e.Table,
Detail: e.Detail,
Code: string(e.Code),
}
},
}
RegisterConstraint(constraint)
_, err := db.Exec("INSERT INTO accounts (id, email, balance) VALUES ($1, $2, -1)", uuid, email)
dberr := GetError(err)
switch e := dberr.(type) {
case *Error:
test.AssertEquals(t, e.Error(), "Cannot write a negative balance")
test.AssertEquals(t, e.Table, "accounts")
default:
t.Fail()
}
}
func TestInvalidUUID(t *testing.T) {
t.Parallel()
setUp(t)
_, err := db.Exec("INSERT INTO accounts (id) VALUES ('foo')")
dberr := GetError(err)
switch e := dberr.(type) {
case *Error:
test.AssertEquals(t, e.Error(), "Invalid input syntax for type uuid: \"foo\"")
default:
t.Fail()
}
}
func TestInvalidJSON(t *testing.T) {
t.Parallel()
setUp(t)
_, err := db.Exec("INSERT INTO accounts (data) VALUES ('')")
dberr := GetError(err)
switch e := dberr.(type) {
case *Error:
test.AssertEquals(t, e.Error(), "Invalid input syntax for type json")
default:
t.Fail()
}
}
func TestInvalidEnum(t *testing.T) {
t.Parallel()
setUp(t)
_, err := db.Exec("INSERT INTO accounts (id, email, balance, status) VALUES ($1, $2, 1, 'blah')", uuid, email)
dberr := GetError(err)
switch e := dberr.(type) {
case *Error:
test.AssertEquals(t, e.Error(), "Invalid account_status: \"blah\"")
default:
t.Fail()
}
}
func TestTooLargeInt(t *testing.T) {
t.Parallel()
setUp(t)
_, err := db.Exec("INSERT INTO accounts (id, email, balance) VALUES ($1, $2, 40000)", uuid, email)
dberr := GetError(err)
switch e := dberr.(type) {
case *Error:
test.AssertEquals(t, e.Error(), "Smallint too large or too small")
default:
t.Fail()
}
}
func TestUniqueConstraint(t *testing.T) {
setUp(t)
defer tearDown(t)
query := "INSERT INTO accounts (id, email, balance) VALUES ($1, $2, 1)"
_, err := db.Exec(query, uuid, email)
test.AssertNotError(t, err, "")
_, err = db.Exec(query, uuid, email)
dberr := GetError(err)
switch e := dberr.(type) {
case *Error:
test.AssertEquals(t, e.Error(), "A id already exists with this value (3c7d2b4a-3fc8-4782-a518-4ce9efef51e7)")
test.AssertEquals(t, e.Column, "id")
test.AssertEquals(t, e.Table, "accounts")
test.AssertEquals(t, e.Code, CodeUniqueViolation)
default:
t.Fail()
}
}
func TestUniqueFailureOnUpdate(t *testing.T) {
setUp(t)
defer tearDown(t)
query := "INSERT INTO accounts (id, email, balance) VALUES ($1, $2, 1)"
_, err := db.Exec(query, uuid, email)
test.AssertNotError(t, err, "")
_, err = db.Exec(query, uuid2, email2)
test.AssertNotError(t, err, "")
_, err = db.Exec("UPDATE accounts SET email = $1 WHERE id = $2", email, uuid2)
dberr := GetError(err)
switch e := dberr.(type) {
case *Error:
test.AssertEquals(t, e.Error(), "A email already exists with this value ([email protected])")
test.AssertEquals(t, e.Column, "email")
test.AssertEquals(t, e.Table, "accounts")
test.AssertEquals(t, e.Code, CodeUniqueViolation)
default:
t.Fail()
}
}
func TestForeignKeyFailure(t *testing.T) {
setUp(t)
defer tearDown(t)
query := "INSERT INTO payments (id, account_id) VALUES ($1, $2)"
_, err := db.Exec(query, uuid, uuid2)
dberr := GetError(err)
switch e := dberr.(type) {
case *Error:
test.AssertEquals(t, e.Error(), "Can't save to payments because the account_id (91f47e99-d616-4d8c-9c02-cbd13bceac60) isn't present in the accounts table")
test.AssertEquals(t, e.Column, "")
test.AssertEquals(t, e.Table, "payments")
test.AssertEquals(t, e.Code, CodeForeignKeyViolation)
default:
t.Fail()
}
}
func TestForeignKeyParentDelete(t *testing.T) {
t.Parallel()
err := &pq.Error{
Severity: "ERROR", Code: "23503",
Message: "update or delete on table \"cars\" violates foreign key constraint \"toyota_car_id_fkey\" on table \"toyotas\"",
Detail: "Key (id)=(0e46047b-69af-442d-a1c0-8a71960b7772) is still referenced from table \"toyotas\".", Hint: "", Position: "", InternalPosition: "", InternalQuery: "", Where: "", Schema: "public", Table: "toyotas", Column: "", DataTypeName: "", Constraint: "toyota_car_id_fkey", File: "ri_triggers.c", Line: "3335", Routine: "ri_ReportViolation"}
dberr := GetError(err)
switch e := dberr.(type) {
case *Error:
test.AssertEquals(t, e.Error(), "Can't update or delete cars records because the cars id (0e46047b-69af-442d-a1c0-8a71960b7772) is still referenced by the toyotas table")
test.AssertEquals(t, e.Column, "")
test.AssertEquals(t, e.Table, "toyotas")
test.AssertEquals(t, e.Code, CodeForeignKeyViolation)
default:
t.Fail()
}
}
func TestCapitalize(t *testing.T) {
t.Parallel()
test.AssertEquals(t, capitalize("foo"), "Foo")
test.AssertEquals(t, capitalize("foo bar baz"), "Foo bar baz")
}
func TestColumnFinder(t *testing.T) {
t.Parallel()
test.AssertEquals(t, findColumn("Key (id)=(blah) already exists."), "id")
test.AssertEquals(t, findColumn("Key (foo bar)=(blah) already exists."), "foo bar")
test.AssertEquals(t, findColumn("Unknown detail message"), "")
}
func TestValueFinder(t *testing.T) {
t.Parallel()
test.AssertEquals(t, findValue("Key (id)=(blah) already exists."), "blah")
test.AssertEquals(t, findValue("Key (foo)=(foo blah) already exists."), "foo blah")
test.AssertEquals(t, findValue("Unknown detail message"), "")
}