You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
boundary/internal/db/error_test.go

181 lines
3.1 KiB

package db
import (
"context"
"testing"
"github.com/lib/pq"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestError_IsUnique(t *testing.T) {
var tests = []struct {
name string
in error
want bool
}{
{
name: "nil-error",
in: nil,
want: false,
},
{
name: "postgres-not-unique",
in: &pq.Error{
Code: pq.ErrorCode("23503"),
},
want: false,
},
{
name: "postgres-is-unique2",
in: &pq.Error{
Code: pq.ErrorCode("23505"),
},
want: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
assert := assert.New(t)
err := tt.in
got := IsUniqueError(err)
assert.Equal(tt.want, got)
})
}
}
func TestError_IsCheckConstraint(t *testing.T) {
var tests = []struct {
name string
in error
want bool
}{
{
name: "nil-error",
in: nil,
want: false,
},
{
name: "postgres-not-check-constraint",
in: &pq.Error{
Code: pq.ErrorCode("23505"),
},
want: false,
},
{
name: "postgres-is-check-constraint",
in: &pq.Error{
Code: pq.ErrorCode("23514"),
},
want: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
assert := assert.New(t)
err := tt.in
got := IsCheckConstraintError(err)
assert.Equal(tt.want, got)
})
}
}
func TestError_IsNotNullError(t *testing.T) {
var tests = []struct {
name string
in error
want bool
}{
{
name: "nil-error",
in: nil,
want: false,
},
{
name: "postgres-is-unique-not-not-null",
in: &pq.Error{
Code: pq.ErrorCode("23505"),
},
want: false,
},
{
name: "postgres-is-check-constraint-not-not-null",
in: &pq.Error{
Code: pq.ErrorCode("23514"),
},
want: false,
},
{
name: "postgres-is-not-null",
in: &pq.Error{
Code: pq.ErrorCode("23502"),
},
want: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
assert := assert.New(t)
err := tt.in
got := IsNotNullError(err)
assert.Equal(tt.want, got)
})
}
}
func TestError_IsMissingTableError(t *testing.T) {
var tests = []struct {
name string
in error
want bool
}{
{
name: "nil-error",
in: nil,
want: false,
},
{
name: "postgres-is-unique-not-not-null",
in: &pq.Error{
Code: pq.ErrorCode("23505"),
},
want: false,
},
{
name: "postgres-is-check-constraint-not-not-null",
in: &pq.Error{
Code: pq.ErrorCode("23514"),
},
want: false,
},
{
name: "postgres-is-missing-table",
in: &pq.Error{
Code: pq.ErrorCode("42P01"),
},
want: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
assert := assert.New(t)
err := tt.in
got := IsMissingTableError(err)
assert.Equal(tt.want, got)
})
}
t.Run("query-missing-table", func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
conn, _ := TestSetup(t, "postgres")
rw := New(conn)
_, err := rw.Query(context.Background(), "select * from non_existent_table", nil)
require.Error(err)
assert.True(IsMissingTableError(err))
})
}