github.com/prebid/prebid-server/v2@v2.18.0/hooks/hookexecution/errors_test.go (about)

     1  package hookexecution
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestFindFirstRejectOrNil(t *testing.T) {
    11  	customError := errors.New("error message")
    12  	rejectError := &RejectError{NBR: 123}
    13  
    14  	testCases := []struct {
    15  		description string
    16  		errs        []error
    17  		expectedErr *RejectError
    18  	}{
    19  		{
    20  			description: "Returns reject error",
    21  			errs:        []error{rejectError},
    22  			expectedErr: rejectError,
    23  		},
    24  		{
    25  			description: "Finds reject error in slice of errors and returns it",
    26  			errs:        []error{customError, rejectError},
    27  			expectedErr: rejectError,
    28  		},
    29  		{
    30  			description: "No reject error if there are no errors",
    31  			errs:        []error{},
    32  			expectedErr: nil,
    33  		},
    34  		{
    35  			description: "No reject error if it not found",
    36  			errs:        []error{customError},
    37  			expectedErr: nil,
    38  		},
    39  	}
    40  
    41  	for _, test := range testCases {
    42  		t.Run(test.description, func(t *testing.T) {
    43  			result := FindFirstRejectOrNil(test.errs)
    44  			assert.Equal(t, test.expectedErr, result)
    45  		})
    46  	}
    47  }
    48  
    49  func TestCastRejectErr(t *testing.T) {
    50  	rejectError := &RejectError{NBR: 123}
    51  	testCases := []struct {
    52  		description    string
    53  		err            error
    54  		expectedErr    *RejectError
    55  		expectedResult bool
    56  	}{
    57  		{
    58  			description:    "Returns reject error and true if reject error provided",
    59  			err:            rejectError,
    60  			expectedErr:    rejectError,
    61  			expectedResult: true,
    62  		},
    63  		{
    64  			description:    "Returns nil and false if no error provided",
    65  			err:            nil,
    66  			expectedErr:    nil,
    67  			expectedResult: false,
    68  		},
    69  		{
    70  			description:    "Returns nil and false if custom error type provided",
    71  			err:            errors.New("error message"),
    72  			expectedErr:    nil,
    73  			expectedResult: false,
    74  		},
    75  	}
    76  
    77  	for _, test := range testCases {
    78  		t.Run(test.description, func(t *testing.T) {
    79  			rejectErr, isRejectErr := CastRejectErr(test.err)
    80  			assert.Equal(t, test.expectedErr, rejectErr, "Invalid error returned.")
    81  			assert.Equal(t, test.expectedResult, isRejectErr, "Invalid casting result.")
    82  		})
    83  	}
    84  }