79 lines
2.1 KiB
Go
79 lines
2.1 KiB
Go
package errors
|
|
|
|
import (
|
|
"net/http"
|
|
"testing"
|
|
)
|
|
|
|
func TestErrNotFound(t *testing.T) {
|
|
err := ErrNotFound("pool")
|
|
if err.Code != ErrCodeNotFound {
|
|
t.Errorf("expected code %s, got %s", ErrCodeNotFound, err.Code)
|
|
}
|
|
if err.HTTPStatus != http.StatusNotFound {
|
|
t.Errorf("expected status %d, got %d", http.StatusNotFound, err.HTTPStatus)
|
|
}
|
|
}
|
|
|
|
func TestErrBadRequest(t *testing.T) {
|
|
err := ErrBadRequest("invalid request")
|
|
if err.Code != ErrCodeBadRequest {
|
|
t.Errorf("expected code %s, got %s", ErrCodeBadRequest, err.Code)
|
|
}
|
|
if err.HTTPStatus != http.StatusBadRequest {
|
|
t.Errorf("expected status %d, got %d", http.StatusBadRequest, err.HTTPStatus)
|
|
}
|
|
}
|
|
|
|
func TestErrValidation(t *testing.T) {
|
|
err := ErrValidation("validation failed")
|
|
if err.Code != ErrCodeValidation {
|
|
t.Errorf("expected code %s, got %s", ErrCodeValidation, err.Code)
|
|
}
|
|
if err.HTTPStatus != http.StatusBadRequest {
|
|
t.Errorf("expected status %d, got %d", http.StatusBadRequest, err.HTTPStatus)
|
|
}
|
|
}
|
|
|
|
func TestErrInternal(t *testing.T) {
|
|
err := ErrInternal("internal error")
|
|
if err.Code != ErrCodeInternal {
|
|
t.Errorf("expected code %s, got %s", ErrCodeInternal, err.Code)
|
|
}
|
|
if err.HTTPStatus != http.StatusInternalServerError {
|
|
t.Errorf("expected status %d, got %d", http.StatusInternalServerError, err.HTTPStatus)
|
|
}
|
|
}
|
|
|
|
func TestErrConflict(t *testing.T) {
|
|
err := ErrConflict("resource exists")
|
|
if err.Code != ErrCodeConflict {
|
|
t.Errorf("expected code %s, got %s", ErrCodeConflict, err.Code)
|
|
}
|
|
if err.HTTPStatus != http.StatusConflict {
|
|
t.Errorf("expected status %d, got %d", http.StatusConflict, err.HTTPStatus)
|
|
}
|
|
}
|
|
|
|
func TestWithDetails(t *testing.T) {
|
|
err := ErrNotFound("pool").WithDetails("tank")
|
|
if err.Details != "tank" {
|
|
t.Errorf("expected details 'tank', got %s", err.Details)
|
|
}
|
|
}
|
|
|
|
func TestError(t *testing.T) {
|
|
err := ErrNotFound("pool")
|
|
errorStr := err.Error()
|
|
if errorStr == "" {
|
|
t.Error("expected non-empty error string")
|
|
}
|
|
if err.Details != "" {
|
|
errWithDetails := err.WithDetails("tank")
|
|
errorStr = errWithDetails.Error()
|
|
if errorStr == "" {
|
|
t.Error("expected non-empty error string with details")
|
|
}
|
|
}
|
|
}
|