github.com/prebid/prebid-server/v2@v2.18.0/errortypes/severity_test.go (about)

     1  package errortypes
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  type stubError struct{ severity Severity }
    11  
    12  func (e *stubError) Error() string      { return "anyMessage" }
    13  func (e *stubError) Code() int          { return 42 }
    14  func (e *stubError) Severity() Severity { return e.severity }
    15  
    16  func TestContainsFatalError(t *testing.T) {
    17  	fatalError := &stubError{severity: SeverityFatal}
    18  	notFatalError := &stubError{severity: SeverityWarning}
    19  	unknownSeverityError := errors.New("anyError")
    20  
    21  	testCases := []struct {
    22  		description   string
    23  		errors        []error
    24  		shouldBeFatal bool
    25  	}{
    26  		{
    27  			description:   "None",
    28  			errors:        []error{},
    29  			shouldBeFatal: false,
    30  		},
    31  		{
    32  			description:   "One - Fatal",
    33  			errors:        []error{fatalError},
    34  			shouldBeFatal: true,
    35  		},
    36  		{
    37  			description:   "One - Not Fatal",
    38  			errors:        []error{notFatalError},
    39  			shouldBeFatal: false,
    40  		},
    41  		{
    42  			description:   "One - Unknown Severity Same As Fatal",
    43  			errors:        []error{unknownSeverityError},
    44  			shouldBeFatal: true,
    45  		},
    46  		{
    47  			description:   "Mixed",
    48  			errors:        []error{fatalError, notFatalError, unknownSeverityError},
    49  			shouldBeFatal: true,
    50  		},
    51  	}
    52  
    53  	for _, tc := range testCases {
    54  		result := ContainsFatalError(tc.errors)
    55  		assert.Equal(t, tc.shouldBeFatal, result)
    56  	}
    57  }
    58  
    59  func TestFatalOnly(t *testing.T) {
    60  	fatalError := &stubError{severity: SeverityFatal}
    61  	notFatalError := &stubError{severity: SeverityWarning}
    62  	unknownSeverityError := errors.New("anyError")
    63  
    64  	testCases := []struct {
    65  		description       string
    66  		errs              []error
    67  		errsShouldBeFatal []error
    68  	}{
    69  		{
    70  			description:       "None",
    71  			errs:              []error{},
    72  			errsShouldBeFatal: []error{},
    73  		},
    74  		{
    75  			description:       "One - Fatal",
    76  			errs:              []error{fatalError},
    77  			errsShouldBeFatal: []error{fatalError},
    78  		},
    79  		{
    80  			description:       "One - Not Fatal",
    81  			errs:              []error{notFatalError},
    82  			errsShouldBeFatal: []error{},
    83  		},
    84  		{
    85  			description:       "One - Unknown Severity Same As Fatal",
    86  			errs:              []error{unknownSeverityError},
    87  			errsShouldBeFatal: []error{unknownSeverityError},
    88  		},
    89  		{
    90  			description:       "Mixed",
    91  			errs:              []error{fatalError, notFatalError, unknownSeverityError},
    92  			errsShouldBeFatal: []error{fatalError, unknownSeverityError},
    93  		},
    94  	}
    95  
    96  	for _, tc := range testCases {
    97  		result := FatalOnly(tc.errs)
    98  		assert.ElementsMatch(t, tc.errsShouldBeFatal, result)
    99  	}
   100  }
   101  
   102  func TestWarningOnly(t *testing.T) {
   103  	warningError := &stubError{severity: SeverityWarning}
   104  	notWarningError := &stubError{severity: SeverityFatal}
   105  	unknownSeverityError := errors.New("anyError")
   106  
   107  	testCases := []struct {
   108  		description         string
   109  		errs                []error
   110  		errsShouldBeWarning []error
   111  	}{
   112  		{
   113  			description:         "None",
   114  			errs:                []error{},
   115  			errsShouldBeWarning: []error{},
   116  		},
   117  		{
   118  			description:         "One - Warning",
   119  			errs:                []error{warningError},
   120  			errsShouldBeWarning: []error{warningError},
   121  		},
   122  		{
   123  			description:         "One - Not Warning",
   124  			errs:                []error{notWarningError},
   125  			errsShouldBeWarning: []error{},
   126  		},
   127  		{
   128  			description:         "One - Unknown Severity Not Warning",
   129  			errs:                []error{unknownSeverityError},
   130  			errsShouldBeWarning: []error{},
   131  		},
   132  		{
   133  			description:         "One - Mixed",
   134  			errs:                []error{warningError, notWarningError, unknownSeverityError},
   135  			errsShouldBeWarning: []error{warningError},
   136  		},
   137  	}
   138  
   139  	for _, tc := range testCases {
   140  		result := WarningOnly(tc.errs)
   141  		assert.ElementsMatch(t, tc.errsShouldBeWarning, result)
   142  	}
   143  }