64 lines
2.4 KiB
Go
64 lines
2.4 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)
|
|
}
|
|
}
|