-
Notifications
You must be signed in to change notification settings - Fork 842
Expand file tree
/
Copy pathpostgres_test.go
More file actions
96 lines (85 loc) · 2.42 KB
/
Copy pathpostgres_test.go
File metadata and controls
96 lines (85 loc) · 2.42 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
package database
import (
"errors"
"net"
"testing"
"github.com/jackc/pgx/v5/pgconn"
"github.com/stretchr/testify/assert"
)
type wrappedError struct {
err error
}
func (e *wrappedError) Error() string {
return e.err.Error()
}
func (e *wrappedError) Unwrap() error {
return e.err
}
func TestIsInvalidDBPgError(t *testing.T) {
// wrap error with wrappedError when testing to ensure the function checks the whole error chain
testCases := []struct {
Name string
Err error
ExpectedResult bool
}{
{
Name: "nil error",
Err: nil,
ExpectedResult: false,
},
{
Name: "not a PgError",
Err: &wrappedError{err: &net.OpError{Op: "connect", Err: errors.New("connection refused")}},
ExpectedResult: false,
},
{
Name: "PgError but not invalid DB",
Err: &wrappedError{&pgconn.PgError{Severity: "FATAL", Message: "out of memory", Code: "53200"}},
ExpectedResult: false,
},
{
Name: "PgError and is invalid DB",
Err: &wrappedError{&pgconn.PgError{Severity: "FATAL", Message: "database \"flyte\" does not exist", Code: "3D000"}},
ExpectedResult: true,
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
assert.Equal(t, tc.ExpectedResult, IsPgErrorWithCode(tc.Err, PqInvalidDBCode))
})
}
}
func TestIsPgDbAlreadyExistsError(t *testing.T) {
// wrap error with wrappedError when testing to ensure the function checks the whole error chain
testCases := []struct {
Name string
Err error
ExpectedResult bool
}{
{
Name: "nil error",
Err: nil,
ExpectedResult: false,
},
{
Name: "not a PgError",
Err: &wrappedError{err: &net.OpError{Op: "connect", Err: errors.New("connection refused")}},
ExpectedResult: false,
},
{
Name: "PgError but not already exists",
Err: &wrappedError{&pgconn.PgError{Severity: "FATAL", Message: "out of memory", Code: "53200"}},
ExpectedResult: false,
},
{
Name: "PgError and is already exists",
Err: &wrappedError{&pgconn.PgError{Severity: "FATAL", Message: "database \"flyte\" does not exist", Code: "42P04"}},
ExpectedResult: true,
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
assert.Equal(t, tc.ExpectedResult, IsPgErrorWithCode(tc.Err, PqDbAlreadyExistsCode))
})
}
}