90 lines
2.3 KiB
Go
90 lines
2.3 KiB
Go
package domain_test
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"gitea.dwysokinski.me/Kichiyaki/notificationarr/internal/domain"
|
|
)
|
|
|
|
func TestErrorCode_String(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
assert.Equal(t, domain.ErrorCodeUnknown.String(), "internal-server-error")
|
|
assert.Equal(t, domain.ErrorCodeValidation.String(), "validation-error")
|
|
assert.Equal(t, domain.ErrorCodeIO.String(), "io-error")
|
|
}
|
|
|
|
func TestError(t *testing.T) {
|
|
errToWrap := errors.New("WithErr")
|
|
tests := []struct {
|
|
name string
|
|
options []domain.ErrorOption
|
|
expectedCode domain.ErrorCode
|
|
expectedErr error
|
|
expectedMsg string
|
|
}{
|
|
{
|
|
name: "OK: WithCode(domain.ErrorCodeUnknown), WithMessage",
|
|
options: []domain.ErrorOption{
|
|
domain.WithCode(domain.ErrorCodeUnknown),
|
|
domain.WithMessage("err err"),
|
|
},
|
|
expectedCode: domain.ErrorCodeUnknown,
|
|
expectedErr: nil,
|
|
expectedMsg: "err err",
|
|
},
|
|
{
|
|
name: "OK: WithCode(domain.ErrorCodeValidation), WithMessage",
|
|
options: []domain.ErrorOption{
|
|
domain.WithCode(domain.ErrorCodeValidation),
|
|
domain.WithMessage("err err"),
|
|
},
|
|
expectedCode: domain.ErrorCodeValidation,
|
|
expectedErr: nil,
|
|
expectedMsg: "err err",
|
|
},
|
|
{
|
|
name: "OK: WithCode(domain.ErrorCodeIO), WithMessage",
|
|
options: []domain.ErrorOption{
|
|
domain.WithCode(domain.ErrorCodeIO),
|
|
domain.WithMessage("err err"),
|
|
},
|
|
expectedCode: domain.ErrorCodeIO,
|
|
expectedErr: nil,
|
|
expectedMsg: "err err",
|
|
},
|
|
{
|
|
name: "OK: WithCode(domain.ErrorCodeUnknown), WithMessagef, WithErr",
|
|
options: []domain.ErrorOption{
|
|
domain.WithCode(domain.ErrorCodeUnknown),
|
|
domain.WithMessagef("xxx %d", 25),
|
|
domain.WithErr(errToWrap),
|
|
},
|
|
expectedCode: domain.ErrorCodeUnknown,
|
|
expectedErr: errToWrap,
|
|
expectedMsg: "xxx 25",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
err := domain.NewError(tt.options...)
|
|
assert.Equal(t, tt.expectedCode, err.Code())
|
|
assert.Equal(t, tt.expectedMsg, err.Message())
|
|
assert.ErrorIs(t, tt.expectedErr, err.Unwrap())
|
|
if tt.expectedErr != nil {
|
|
assert.Equal(t, tt.expectedMsg+": "+tt.expectedErr.Error(), err.Error())
|
|
} else {
|
|
assert.Equal(t, tt.expectedMsg, err.Error())
|
|
}
|
|
})
|
|
}
|
|
}
|