template-monorepo/internal/library/validate/errors_test.go

40 lines
915 B
Go
Raw Permalink Normal View History

2026-05-19 12:56:32 +00:00
package validate
import (
"errors"
"testing"
errs "gateway/internal/library/errors"
"gateway/internal/library/errors/code"
)
func TestValidationErrors_AsErrors(t *testing.T) {
t.Parallel()
ve := ValidationErrors{{Field: "id", Message: "id is required"}}
var target ValidationErrors
if !errors.As(ve, &target) {
t.Fatal("errors.As should match ValidationErrors")
}
got, ok := AsErrors(ve)
if !ok || len(got) != 1 {
t.Fatalf("AsErrors = %v, %v", got, ok)
}
}
func TestValidationErrors_ToBusinessError(t *testing.T) {
t.Parallel()
ve := ValidationErrors{{Field: "email", Message: "invalid email"}}
be := ve.ToBusinessError(code.Facade)
if be == nil {
t.Fatal("expected business error")
}
if be.Category() != code.InputInvalidFormat {
t.Fatalf("category = %d, want %d", be.Category(), code.InputInvalidFormat)
}
if errs.FromError(be) == nil {
t.Fatal("FromError should unwrap")
}
}