github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/storage/dberr/errors_test.go (about) 1 package dberr 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 ) 8 9 func TestAppError(t *testing.T) { 10 t.Run("should create error with proper code", func(t *testing.T) { 11 assert.Equal(t, CodeInternal, Internal("error").Code()) 12 assert.Equal(t, CodeNotFound, NotFound("error").Code()) 13 assert.Equal(t, CodeAlreadyExists, AlreadyExists("error").Code()) 14 }) 15 16 t.Run("should create error with simple message", func(t *testing.T) { 17 assert.Equal(t, "error", Internal("error").Error()) 18 assert.Equal(t, "error", NotFound("error").Error()) 19 assert.Equal(t, "error", AlreadyExists("error").Error()) 20 }) 21 22 t.Run("should create error with formatted message", func(t *testing.T) { 23 assert.Equal(t, "code: 1, error: bug", Internal("code: %d, error: %s", 1, "bug").Error()) 24 assert.Equal(t, "code: 1, error: bug", NotFound("code: %d, error: %s", 1, "bug").Error()) 25 assert.Equal(t, "code: 1, error: bug", AlreadyExists("code: %d, error: %s", 1, "bug").Error()) 26 }) 27 28 t.Run("should append apperrors without changing error code", func(t *testing.T) { 29 //given 30 createdInternalErr := Internal("Some Internal apperror, %s", "Some pkg err") 31 createdNotFoundErr := NotFound("Some NotFound apperror, %s", "Some pkg err") 32 createdAlreadyExistsErr := AlreadyExists("Some AlreadyExists apperror, %s", "Some pkg err") 33 34 //when 35 appendedInternalErr := createdInternalErr.Append("Some additional message") 36 appendedNotFoundErr := createdNotFoundErr.Append("Some additional message") 37 appendedAlreadyExistsErr := createdAlreadyExistsErr.Append("Some additional message") 38 39 //then 40 assert.Equal(t, CodeInternal, appendedInternalErr.Code()) 41 assert.Equal(t, CodeNotFound, appendedNotFoundErr.Code()) 42 assert.Equal(t, CodeAlreadyExists, appendedAlreadyExistsErr.Code()) 43 }) 44 45 t.Run("should append apperrors and chain messages correctly", func(t *testing.T) { 46 //given 47 createdInternalErr := Internal("Some Internal apperror, %s", "Some pkg err") 48 createdNotFoundErr := NotFound("Some NotFound apperror, %s", "Some pkg err") 49 createdAlreadyExistsErr := AlreadyExists("Some AlreadyExists apperror, %s", "Some pkg err") 50 51 //when 52 appendedInternalErr := createdInternalErr.Append("Some additional message: %s", "error") 53 appendedNotFoundErr := createdNotFoundErr.Append("Some additional message: %s", "error") 54 appendedAlreadyExistsErr := createdAlreadyExistsErr.Append("Some additional message: %s", "error") 55 56 //then 57 assert.Equal(t, "Some additional message: error, Some Internal apperror, Some pkg err", appendedInternalErr.Error()) 58 assert.Equal(t, "Some additional message: error, Some NotFound apperror, Some pkg err", appendedNotFoundErr.Error()) 59 assert.Equal(t, "Some additional message: error, Some AlreadyExists apperror, Some pkg err", appendedAlreadyExistsErr.Error()) 60 }) 61 62 t.Run("should recognize type of error", func(t *testing.T) { 63 //given 64 internalErr := Internal("Some Internal apperror, %s", "Some pkg err") 65 notFoundErr := NotFound("Some NotFound apperror, %s", "Some pkg err") 66 conflict := Conflict("some conflict %s", "error") 67 68 //when 69 checkOne := IsNotFound(internalErr) 70 checkTwo := IsNotFound(notFoundErr) 71 checkConflict := IsConflict(conflict) 72 73 //then 74 assert.False(t, checkOne) 75 assert.True(t, checkTwo) 76 assert.True(t, checkConflict) 77 }) 78 }