Sybren A. Stüvel c3b694ab2a Manager: wrap job/task errors in persistence layer
Avoid users of the persistence layer to have to test against Gorm errors,
by wrapping job/task errors in a new `PersistenceError` struct.

Instead of testing for `gorm.ErrRecordNotFound`, code can now test for
`persistence.ErrJobNotFound` or `persistence.ErrTaskNotFound`.
2022-04-21 11:54:59 +02:00

35 lines
1.0 KiB
Go

// SPDX-License-Identifier: GPL-3.0-or-later
package persistence
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
"gorm.io/gorm"
)
func TestNotFoundErrors(t *testing.T) {
assert.ErrorIs(t, ErrJobNotFound, gorm.ErrRecordNotFound)
assert.ErrorIs(t, ErrTaskNotFound, gorm.ErrRecordNotFound)
assert.Contains(t, ErrJobNotFound.Error(), "job")
assert.Contains(t, ErrTaskNotFound.Error(), "task")
}
func TestTranslateGormJobError(t *testing.T) {
assert.Nil(t, translateGormJobError(nil))
assert.Equal(t, ErrJobNotFound, translateGormJobError(gorm.ErrRecordNotFound))
otherError := errors.New("this error is not special for this function")
assert.Equal(t, otherError, translateGormJobError(otherError))
}
func TestTranslateGormTaskError(t *testing.T) {
assert.Nil(t, translateGormTaskError(nil))
assert.Equal(t, ErrTaskNotFound, translateGormTaskError(gorm.ErrRecordNotFound))
otherError := errors.New("this error is not special for this function")
assert.Equal(t, otherError, translateGormTaskError(otherError))
}