51 lines
1.4 KiB
Go
51 lines
1.4 KiB
Go
package shared
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
|
|
"git.entityseven.com/rilaxik/pllist/internal/domain"
|
|
)
|
|
|
|
func TestUserMessage(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
err error
|
|
want string
|
|
}{
|
|
{name: "validation", err: domain.NewValidationError("Title is too long."), want: "Title is too long."},
|
|
{name: "wrapped validation", err: errors.Join(errors.New("context"), domain.NewValidationError("Bad quantity.")), want: "Bad quantity."},
|
|
{name: "not found", err: domain.ErrNotFound, want: "Not found or no access."},
|
|
{name: "forbidden", err: domain.ErrForbidden, want: "You cannot do that."},
|
|
{name: "conflict", err: domain.ErrConflict, want: "This changed while you were looking. Try again."},
|
|
{name: "bad callback", err: ErrBadCallback, want: "This button is out of date. Use /lists."},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := UserMessage(tt.err); got != tt.want {
|
|
t.Fatalf("UserMessage() = %q, want %q", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestIsExpectedError(t *testing.T) {
|
|
expected := []error{
|
|
domain.NewValidationError("bad"),
|
|
domain.ErrNotFound,
|
|
domain.ErrForbidden,
|
|
domain.ErrConflict,
|
|
domain.ErrInvalidInput,
|
|
ErrBadCallback,
|
|
}
|
|
for _, err := range expected {
|
|
if !IsExpectedError(err) {
|
|
t.Fatalf("IsExpectedError(%v) = false, want true", err)
|
|
}
|
|
}
|
|
if IsExpectedError(errors.New("database down")) {
|
|
t.Fatal("IsExpectedError() = true for an unexpected error")
|
|
}
|
|
}
|