Coverage: Service 71.7% → 71.8% - classified_error_test.go (10 tests): error wrapping, Unwrap, errors.Is - stats_test.go (12 tests): user stats, dashboard stats, daysAgo utility
75 lines
2.1 KiB
Go
75 lines
2.1 KiB
Go
package service
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
// =============================================================================
|
|
// ClassifiedError Tests
|
|
// =============================================================================
|
|
|
|
func TestClassifiedError_Error_WithMessage(t *testing.T) {
|
|
err := newValidationError("custom validation message")
|
|
assert.EqualError(t, err, "custom validation message")
|
|
}
|
|
|
|
func TestClassifiedError_Error_WithEmptyMessage(t *testing.T) {
|
|
// Create error with only cause
|
|
err := &classifiedError{cause: ErrValidationFailed}
|
|
assert.EqualError(t, err, "validation failed")
|
|
}
|
|
|
|
func TestClassifiedError_Error_WithNoMessageOrCause(t *testing.T) {
|
|
// Create error with neither message nor cause
|
|
err := &classifiedError{}
|
|
assert.Equal(t, "", err.Error())
|
|
}
|
|
|
|
func TestClassifiedError_Unwrap(t *testing.T) {
|
|
err := newRateLimitError("too many requests")
|
|
|
|
// Unwrap should return the cause
|
|
unwrapped := errors.Unwrap(err)
|
|
assert.Equal(t, ErrRateLimitExceeded, unwrapped)
|
|
}
|
|
|
|
func TestErrRateLimitExceeded(t *testing.T) {
|
|
assert.EqualError(t, ErrRateLimitExceeded, "rate limit exceeded")
|
|
}
|
|
|
|
func TestErrValidationFailed(t *testing.T) {
|
|
assert.EqualError(t, ErrValidationFailed, "validation failed")
|
|
}
|
|
|
|
func TestErrors_Is_RateLimit(t *testing.T) {
|
|
// Test that wrapped errors can be identified using errors.Is
|
|
err := newRateLimitError("too many requests")
|
|
|
|
assert.True(t, errors.Is(err, ErrRateLimitExceeded))
|
|
assert.False(t, errors.Is(err, ErrValidationFailed))
|
|
}
|
|
|
|
func TestErrors_Is_Validation(t *testing.T) {
|
|
err := newValidationError("invalid input")
|
|
|
|
assert.True(t, errors.Is(err, ErrValidationFailed))
|
|
assert.False(t, errors.Is(err, ErrRateLimitExceeded))
|
|
}
|
|
|
|
func TestNewRateLimitError(t *testing.T) {
|
|
err := newRateLimitError("rate limited")
|
|
|
|
assert.EqualError(t, err, "rate limited")
|
|
assert.True(t, errors.Is(err, ErrRateLimitExceeded))
|
|
}
|
|
|
|
func TestNewValidationError(t *testing.T) {
|
|
err := newValidationError("validation failed")
|
|
|
|
assert.EqualError(t, err, "validation failed")
|
|
assert.True(t, errors.Is(err, ErrValidationFailed))
|
|
}
|