46 lines
1.6 KiB
Go
46 lines
1.6 KiB
Go
package postgres
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/jackc/pgx/v5/pgconn"
|
|
|
|
"lincodex/backend/internal/domain"
|
|
"lincodex/backend/internal/port/outbound"
|
|
)
|
|
|
|
func TestMapOutboxErrorRecognizesOutboxConflicts(t *testing.T) {
|
|
for _, constraint := range []string{outboxPrimaryKeyConstraint, outboxAggregateVersionConstraint} {
|
|
err := mapOutboxError(&pgconn.PgError{Code: uniqueViolationCode, ConstraintName: constraint})
|
|
if !errors.Is(err, outbound.ErrOutboxEventConflict) {
|
|
t.Fatalf("mapOutboxError(%s) = %v, want ErrOutboxEventConflict", constraint, err)
|
|
}
|
|
if !strings.Contains(err.Error(), constraint) {
|
|
t.Fatalf("mapOutboxError(%s) = %v, want the constraint name in the message", constraint, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestMapOutboxErrorKeepsOtherErrors(t *testing.T) {
|
|
tests := []error{
|
|
&pgconn.PgError{Code: uniqueViolationCode, ConstraintName: "other_table_key"},
|
|
&pgconn.PgError{Code: "23514", ConstraintName: "outbox_events_schema_version_positive"},
|
|
errors.New("connection reset"),
|
|
}
|
|
for _, original := range tests {
|
|
if err := mapOutboxError(original); err != original { //nolint:errorlint // Identity is the property under test.
|
|
t.Fatalf("mapOutboxError(%v) = %v, want the original error", original, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAppendRejectsZeroValueEventBeforeTheDatabase(t *testing.T) {
|
|
// A nil query set proves that validation happens before any statement.
|
|
err := outboxWriter{}.Append(t.Context(), domain.DomainEvent{})
|
|
if !errors.Is(err, domain.ErrInvalidDomainEvent) {
|
|
t.Fatalf("Append() error = %v, want ErrInvalidDomainEvent", err)
|
|
}
|
|
}
|