87 lines
3.3 KiB
Go
87 lines
3.3 KiB
Go
package postgres
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/jackc/pgx/v5/pgconn"
|
|
|
|
"lincodex/backend/internal/port/outbound"
|
|
)
|
|
|
|
const privateRowValue = "Private.Recipient@Example.com"
|
|
|
|
func TestMapDeliveryErrorRecognizesDeliveryConstraints(t *testing.T) {
|
|
tests := []struct {
|
|
code, constraint string
|
|
want error
|
|
}{
|
|
{integrityConstraintViolationCode, "deliveries_content_immutable", outbound.ErrDeliveryContentImmutable},
|
|
{integrityConstraintViolationCode, "delivery_attempts_immutable", outbound.ErrDeliveryContentImmutable},
|
|
{integrityConstraintViolationCode, "deliveries_provider_pinned", outbound.ErrDeliveryProviderPinned},
|
|
{foreignKeyViolationCode, "deliveries_recipient_user_id_fkey", outbound.ErrUserNotFound},
|
|
{foreignKeyViolationCode, "mail_requests_recipient_user_id_fkey", outbound.ErrUserNotFound},
|
|
{foreignKeyViolationCode, "delivery_attempts_delivery_id_fkey", outbound.ErrDeliveryNotFound},
|
|
{uniqueViolationCode, "delivery_attempts_pkey", outbound.ErrDeliveryVersionConflict},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.constraint, func(t *testing.T) {
|
|
err := mapDeliveryError(&pgconn.PgError{
|
|
Code: test.code,
|
|
ConstraintName: test.constraint,
|
|
Message: "row " + privateRowValue,
|
|
Detail: "Key (recipient_email)=(" + privateRowValue + ")",
|
|
})
|
|
if !errors.Is(err, test.want) {
|
|
t.Fatalf("mapDeliveryError() = %v, want %v", err, test.want)
|
|
}
|
|
if strings.Contains(err.Error(), privateRowValue) {
|
|
t.Fatalf("mapped error quotes a row value: %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestMapDeliveryErrorDropsTheTextOfOtherPostgreSQLErrors(t *testing.T) {
|
|
err := mapDeliveryError(&pgconn.PgError{
|
|
Code: checkViolationCode,
|
|
ConstraintName: "deliveries_subject_length",
|
|
Message: "new row violates check constraint with " + privateRowValue,
|
|
Detail: "Failing row contains (" + privateRowValue + ")",
|
|
})
|
|
var state sqlStateError
|
|
if !errors.As(err, &state) || state.SQLState() != checkViolationCode {
|
|
t.Fatalf("mapDeliveryError() = %v, want SQLSTATE %s", err, checkViolationCode)
|
|
}
|
|
if strings.Contains(err.Error(), privateRowValue) || strings.Contains(err.Error(), "violates") {
|
|
t.Fatalf("mapped error keeps the PostgreSQL text: %v", err)
|
|
}
|
|
if !strings.Contains(err.Error(), "deliveries_subject_length") {
|
|
t.Fatalf("mapped error lost the constraint name: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestMapDeliveryErrorMarksOnlyDataFaultsAsRowFaults(t *testing.T) {
|
|
for code, wantRowFault := range map[string]bool{
|
|
"22001": true, // string data right truncation
|
|
"23514": true, // check violation
|
|
"23505": true, // unique violation without a known constraint
|
|
"40P01": false, // deadlock
|
|
"40001": false, // serialization failure
|
|
"08006": false, // connection failure
|
|
"53300": false, // too many connections
|
|
"57014": false, // statement timeout (query canceled)
|
|
"57P01": false, // admin shutdown
|
|
"XX000": false, // internal error
|
|
} {
|
|
err := mapDeliveryError(&pgconn.PgError{Code: code, ConstraintName: "some_constraint", Message: privateRowValue})
|
|
if got := errors.Is(err, outbound.ErrRowFault); got != wantRowFault {
|
|
t.Fatalf("SQLSTATE %s: ErrRowFault = %t, want %t", code, got, wantRowFault)
|
|
}
|
|
if strings.Contains(err.Error(), privateRowValue) {
|
|
t.Fatalf("SQLSTATE %s: the error quotes the row", code)
|
|
}
|
|
}
|
|
}
|