Files
threshold/internal/db/error_test.go
T

126 lines
2.0 KiB
Go
Raw Normal View History

2020-05-27 18:16:43 -04:00
package db
import (
"testing"
"github.com/lib/pq"
"github.com/stretchr/testify/assert"
)
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)
2020-05-27 18:16:43 -04:00
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)
})
}
}
2020-08-18 16:50:26 -04:00
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)
})
}
}